Molte grazie per marc_s per il seguente codice di esempio, dal mio precedente numero Creating user in Active Directory with C# errorsCreazione utente in un OU specifica in Active Directory utilizzando C#
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;
}
Ora sto cercando di ottenere l'account utente in una specifica unità organizzativa . Mantenere la ldapPath negli errori 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, 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
E se mi tolgo la ldapPath, funziona benissimo, ma lancia l'account utente in Utenti OU. Ho anche provato il ldapPath come LDAP: //contoso.com/OU=Domain Users, DC = contoso, DC = com, che non ha funzionato.
Questo l'ha fatto !! Ho rimosso il LDAP: // e funziona benissimo! mantenere contoso.com funziona pure. Grazie!! –
@marc_s sembra buono, come posso aggiungere le credenziali dell'amministratore a questo? –
@DavidGidony: per l'utente appena creato? Dovresti solo aggiungere quell'utente al gruppo 'Administrators' –