我在构造函数中有一小段代码:
public class ADUser : ADObject
{
public ADUser(PrincipalContext ouContext)
: base(null)
{
ou = ouContext;
// Even after instantiating, user remains null, which is where the problem lies.
user = new UserPrincipal(ouContext);
base.principal = user;
}
}
public abstract class ADObject
{
protected Principal principal;
protected PrincipalContext ou;
public ADObject(Principal principal)
{
this.principal = principal;
}
}
ou
是基类中的受保护变量,用于存储正在修改或创建的AD用户的AD OU的PrincipalContext
。
调试时,参数ouContext
不为空值,但每当我尝试使用它作为参数实例化新的UserPrincipal对象时,user
总是结束为空。
以下是ouContext PrincipalContext对象的输出:
-ou {System.DirectoryServices.AccountManagement.PrincipalContext} System.DirectoryServices.AccountManagement.PrincipalContext
ConnectedServer "CensoredDC02.CensoredDomain.CensoredParentDomain.com" string
Container "OU=Censored,OU=Censored2,DC=CensoredDomain,DC=CensoredParentDomain,DC=com" string
ContextType Domain System.DirectoryServices.AccountManagement.ContextType
Name "CensoredDomain" string
Options Negotiate | Signing | Sealing System.DirectoryServices.AccountManagement.ContextOptions
UserName null string
使用以下代码在另一个类中创建PrincipalContext对象:
/* baseDomain has the FQDN the OU is in, and ouSelection contains the full DN path of the OU. */
ouContext = new PrincipalContext(ContextType.Domain, baseDomain, ouSelection.SelectedValue.ToString());
/* Then a new ADUser object is created... */
ADUser user = new ADUser(ouContext);
/* Shortly after, the null value returned by new UserPrincipal(ouContext) causes an exception to be thrown in another function. */
提前感谢您的帮助!