如何创建使用.NET 2.0和C#本地用户帐户,也可以设置“密码永不过期”永远不会。

我已使用“的Net.exe”使用的Process.Start并传递它的参数,但它似乎是“网用户”是无法设置“密码永不过期”试图从未

有帮助吗?

解决方案

阅读本优良CodeProject上的文章

HOWTO:(几乎)一切在Active Directory中通过C#

有一个部分“创建用户帐户”和“使用用户密码处理”。

<强>更新

为了适应代码本地帐户与这些替换相应的行:

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

这里开始的原始代码段域帐户:

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;
}
  

有一些细节理解   与用户密码打交道时和   周围的密码,如边界   强制用户更改他们的   密码在下次登录时,否认   用户更改自己的权利   密码,设置密码永不   到期,何时到期,而这些   任务可以通过完成   UserAccountControl标志是   证实该诉讼   部分。

     

请参考这个伟大    MSDN文章:管理用户密码   为实例和文档   关于这些功能。

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

其他提示

这个代码将创建与所述密码的本地帐户永不过期选项集:

        using System.DirectoryServices;

        DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://localhost");
        DirectoryEntries entries = hostMachineDirectory.Children;
        bool userExists = false;
        foreach (DirectoryEntry each in entries)
        {
            userExists = each.Name.Equals("NewUser",  
            StringComparison.CurrentCultureIgnoreCase);
            if (systemtestUserExists)
                break;
        }

        if (false == userExists)
        {
            DirectoryEntry obUser = entries.Add("NewUser", "User");
            obUser.Properties["FullName"].Add("Local user");
            obUser.Invoke("SetPassword", "abcdefg12345@");
            obUser.Invoke("Put", new object[] {"UserFlags", 0x10000});
            obUser.CommitChanges();
        }

在0x10000的标志意味着PasswordNeverExpires。

我花了很长的时间来掌握如何创建设置为不到期密码的本地用户帐户。看来,当您尝试使用:

int val = (int)newUser.Properties["userAccountControl"].Value; 
newUser.Properties["userAccountControl"].Value = val | 0x10000

从Active Directory权限来发挥作用。如果您有Active Directory权限一切正常。如果你不那么得到userAccountControl的属性将总是导致一个空值。试图设置userAccountControl的将导致异常“的目录属性不能在高速缓存中找到”。

然而多狩猎后围绕我发现需要使用调用设置另一个属性“UserFlags”。您可以使用此设置标志上的本地帐户。我试过这个代码和它的工作在Windows Server 2008。

希望这有助于

使用的System.DirectoryServices;

    DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://localhost");
    DirectoryEntries entries = hostMachineDirectory.Children;
    bool userExists = false;
    foreach (DirectoryEntry each in entries)
    {
        userExists = each.Name.Equals("NewUser",  
        StringComparison.CurrentCultureIgnoreCase);
        if (systemtestUserExists)
            break;
    }

    if (false == userExists)
    {
        DirectoryEntry obUser = entries.Add("NewUser", "User");
        obUser.Properties["FullName"].Add("Local user");
        obUser.Invoke("SetPassword", "abcdefg12345@");
        obUser.Invoke("Put", new object[] {"UserFlags", 0x10000});
        obUser.CommitChanges();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top