Creating local user account c# and .NET 2.0

前端 未结 3 1077
说谎
说谎 2020-12-01 03:28

How can I create a local user account using .NET 2.0 and c# and also be able to set the \"Password never expires\" to never.

I have tried using \"Net.exe\" using Pr

3条回答
  •  無奈伤痛
    2020-12-01 03:51

    Read this excellent CodeProject article

    Howto: (Almost) Everything In Active Directory via C#

    There is a section "Create User Account" and "Dealing with User Passwords".

    UPDATE:

    To adapt the code for local accounts replace the respective lines with these:

    DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + 
        Environment.MachineName);
    DirectoryEntry newUser = localMachine.Children.Add("localuser", "user");
    

    Here starts the original code snippet for domain accounts:

    public string CreateUserAccount(string ldapPath, string userName, 
        string userPassword)
    {
        string oGUID = string.Empty;
        try
        {          
            string connectionPrefix = "LDAP://" + ldapPath;
            DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
            DirectoryEntry newUser = dirEntry.Children.Add
                ("CN=" + userName, "user");
            newUser.Properties["samAccountName"].Value = userName;
    
            int val = (int)newUser.Properties["userAccountControl"].Value; 
            newUser.Properties["userAccountControl"].Value = val | 0x10000; 
    
            newUser.CommitChanges();
            oGUID = newUser.Guid.ToString();
    
            newUser.Invoke("SetPassword", new object[] { userPassword });
            newUser.CommitChanges();
    
            dirEntry.Close();
            newUser.Close();
        }
        catch (System.DirectoryServices.DirectoryServicesCOMException E)
        {
            //DoSomethingwith --> E.Message.ToString();    
        }
        return oGUID;
    }
    

    There are some specifics to understand when dealing with user passwords and boundaries around passwords such as forcing a user to change their password on the next logon, denying the user the right to change their own passwords, setting passwords to never expire, to when to expire, and these tasks can be accomplished using UserAccountControl flags that are demonstrated in the proceeding sections.

    Please refer to this great MSDN article: Managing User Passwords for examples and documentation regarding these features.

    CONST                          HEX
    ------------------------------------------
    SCRIPT                         0x0001
    ACCOUNTDISABLE                 0x0002
    HOMEDIR_REQUIRED               0x0008
    LOCKOUT                        0x0010
    PASSWD_NOTREQD                 0x0020
    PASSWD_CANT_CHANGE             0x0040
    ENCRYPTED_TEXT_PWD_ALLOWED     0x0080
    TEMP_DUPLICATE_ACCOUNT         0x0100
    NORMAL_ACCOUNT                 0x0200
    INTERDOMAIN_TRUST_ACCOUNT      0x0800
    WORKSTATION_TRUST_ACCOUNT      0x1000
    SERVER_TRUST_ACCOUNT           0x2000
    DONT_EXPIRE_PASSWORD           0x10000
    MNS_LOGON_ACCOUNT              0x20000
    SMARTCARD_REQUIRED             0x40000
    TRUSTED_FOR_DELEGATION         0x80000
    NOT_DELEGATED                  0x100000
    USE_DES_KEY_ONLY               0x200000
    DONT_REQ_PREAUTH               0x400000
    PASSWORD_EXPIRED               0x800000
    TRUSTED_TO_AUTH_FOR_DELEGATION 0x1000000
    

提交回复
热议问题