简体   繁体   English

C#使用目录服务将用户添加到Active Directory,启用属性不起作用

[英]C# add user to Active Directory using Directory Services, Enabled property not working

I'm using C# to create new active directory accounts on the fly, like this: 我正在使用C#快速创建新的活动目录帐户,如下所示:

        public static void CreateUser(string userName, string password){
            UserPrincipal user = new UserPrincipal(ContextType.Domain);
            user.SetPassword(password);
            user.Name = userName;
            user.SamAccountName = userName;
            user.UserPrincipalName = userName;
            user.Enabled = true;
            user.Save();
        }

The trouble is, the user.Enabled = true; 麻烦的是, user.Enabled = true; line doesn't appear to do anything. 行似乎没有任何作用。 The account is created successfully, but I still have to manually go into the account using AD explorer and set it to enabled. 该帐户已成功创建,但是我仍然必须使用AD资源管理器手动进入该帐户并将其设置为启用。

What could be wrong here? 这有什么问题吗?

In order for a user account to be Enabled in Active Directory, it must have a password. 为了在Active Directory中Enabled用户帐户,它必须具有密码。 Set the password and create the account object first, then enable it subsequently: 设置密码并首先创建帐户对象,然后再启用它:

public static void CreateUser(string userName, string password){
    UserPrincipal user = new UserPrincipal(ContextType.Domain);
    user.SetPassword(password);
    user.Name = userName;
    user.SamAccountName = userName;
    user.UserPrincipalName = userName;
    user.Save();

    // Now that the account has been created and has a password, you can enable it
    user.Enabled = true;
    user.Save();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM