无法在 ActiveDirectory 中创建 PrincipalContext

Can't create PrincipalContext in ActiveDirectory

我正在使用 .NET Forms 应用程序中的这段代码来尝试创建 PrincipalContext 对象。

var oPrincipalContext = new PrincipalContext(
                ContextType.Domain, 
                "mydomain.workgroup",
                "CN=users,CN=mydomain,CN=workgroup", 
                ContextOptions.Bind,                    
                "ADAdmin", 
                "ADAdminPassword");

我也试过这个但是得到了同样的错误。

var oPrincipalContext = new PrincipalContext(
                ContextType.Domain, 
                "mydomain.workgroup",
                "CN=users,CN=mydomain,CN=workgroup", 
                ContextOptions.Negotiate,                    
                "ADAdmin", 
                "ADAdminPassword");

我也试过ADAdmin@mydomain.workgroup作为用户id。

我遇到这个错误

Message=An operations error occurred.

StackTrace=   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
   at System.DirectoryServices.AccountManagement.PrincipalContext.get_ConnectedServer()
   at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e)

InnerException=An operations error occurred.

StackTrace=   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_SchemaEntry()
   at System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de)
   at System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase, Boolean ownCtxBase, String username, String password, ContextOptions options)
   at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()

我在 AD 中以具有 DomainAdmin 权限的用户身份登录。

我是 运行 的计算机在 AD 中具有 DomainAdmin 权限。

This link 有一个解决方案,但它适用于 IIS,但对我不起作用。

This 解决方案也没有用。

既然你提到用户已经有管理员权限,你只需要指定ContextType和你的Domain

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourdomain.com");