非常感谢marc_s以下code样品,从我的previous问题Creating用户在使用C#错误的Active Directory
Many thanks to marc_s for the following code sample, from my previous issue 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",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错误
Now I'm trying to get the user account into a specific OU. Keeping the ldapPath in the PrincipalContext errors
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, String password, 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,它工作正常,但抛出的用户帐户到用户OU。我也试过,如LDAP的ldapPath://contoso/OU=Domain用户,DC = CONTOSO,DC = com的,没有工作
And if i remove the ldapPath, it works fine, but throws the user account into the Users OU. I also tried the ldapPath like LDAP://contoso/OU=Domain Users,DC=contoso,DC=com, which didn't work.
推荐答案我觉得你有LDAP路径有点不对您主要的上下文构造 - 如果你看看链接到MSDN文章中,我给了你,你会看到
I think you have the LDAP path a bit wrong for your principal context constructor - if you check out the link to the MSDN article I gave you, you would see:
// 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");
我也不能确定是否可以使用Internet样式的域名 contoso - 尝试使用NetBIOS风格 CONTOSO 代替。所以,你的情况,你应该尝试:
I'm also not sure if you can use the internet-style domain name contoso - try using the NetBIOS style CONTOSO instead. So in your case, you should try:
public static string ldapPath = "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", ldapPath); // create a user principal object .... (and the rest of your code as you had it) }
这是否为你工作?
Does that work for you??
更多推荐
使用C#在Active Directory中的特定OU创建用户
发布评论