active-directory – 使用C#在Active Directory中的特定OU中创建用户

前端之家收集整理的这篇文章主要介绍了active-directory – 使用C#在Active Directory中的特定OU中创建用户前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
非常感谢marc_s以下代码示例,来自我之前的第 Creating user in Active Directory with C# errors
public static string ldapPath = "LDAP://OU=Domain Users,DC=contoso,DC=com";
public static string CreateUserAccount(string userName,string userPassword)
{
    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain,"contoso.com",ldapPath);

    // create a user principal object
    UserPrincipal user = new UserPrincipal(ctx,userName,userPassword,true);

    // assign some properties to the user principal
    user.GivenName = "User";
    user.Surname = "One";

    // force the user to change password at next logon
    user.ExpirePasswordNow();

    // save the user to the directory
    user.Save();

    return user.SamAccountName;
}

现在,我正在尝试将用户帐户转换为特定的OU.保持ldapPath在PrincipalContext中的错误

System.DirectoryServices.AccountManagement.PrincipalOperationException: Unknown error (0x80005000) ---> System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
   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()
   --- End of inner exception stack trace ---
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
   at System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t)
   at System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse()
   at System.DirectoryServices.AccountManagement.Principal.set_SamAccountName(String value)
   at System.DirectoryServices.AccountManagement.UserPrincipal..ctor(PrincipalContext context,String samAccountName,Boolean enabled)
   at ADINtegrationTest.ActiveDirectory.CreateUserAccount(String userName,String userPassword) in D:\_data\ADINtegrationTest\ADINtegrationTest\ActiveDirectoryUtils.cs:line 20
   at ADINtegrationTest.Form1.Form1_Load(Object sender,EventArgs e) in D:\_data\ADINtegrationTest\ADINtegrationTest\Form1.cs:line 32

如果我删除ldapPath,它可以正常工作,但会将用户帐户注入Users OU.我也尝试过像LDAP://contoso.com/OU=Domain Users,DC = contoso,DC = com这样的ldapPath,它不起作用.

解决方法

我认为您的主要上下文构造函数的LDAP路径有点错误 – 如果您查看我给您的MSDN文章链接,您会看到:
// create a context for a domain called Fabrikam pointed
// to the TechWriters OU and using default credentials
PrincipalContext domainContext = 
   new PrincipalContext(ContextType.Domain,"Fabrikam","ou=TechWriters,dc=fabrikam,dc=com");

我也不确定你是否可以使用互联网风格的域名contoso.com – 尝试使用NetBIOS样式CONTOSO.所以在你的情况下,你应该尝试:

public static string ldapPath = "OU=Domain Users,DC=com";

public static string CreateUserAccount(string userName,string userPassword)
{
     // set up domain context
     PrincipalContext ctx = 
         new PrincipalContext(ContextType.Domain,"CONTOSO",ldapPath);

    // create a user principal object
    .... (and the rest of your code as you had it)
}

那对你有用吗??

猜你在找的C#相关文章