创建本地用户帐户 c# 和 .NET 2.0 [英] Creating local user account c# and .NET 2.0

查看:26
本文介绍了创建本地用户帐户 c# 和 .NET 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 .NET 2.0 和 c# 创建本地用户帐户,并且还能够将密码永不过期"设置为从不.

我尝试使用 Process.Start 使用Net.exe"并传递其参数,但网络用户"似乎无法将密码永不过期"设置为从不.

解决方案

阅读这篇优秀的 CodeProject 文章

方法:(几乎)通过 C# 实现 Active Directory 中的所有内容

有创建用户帐户"和处理用户密码"部分.

更新:

为了适应本地帐户的代码,将相应的行替换为以下内容:

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

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

public string CreateUserAccount(string ldapPath, string userName,字符串用户密码){string oGUID = string.Empty;尝试{字符串 connectionPrefix = "LDAP://" + ldapPath;DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);DirectoryEntry newUser = dirEntry.Children.Add("CN=" + 用户名, "用户");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();}返回 oGUID;}

<块引用>

有一些细节需要理解在处理用户密码和围绕密码的界限,例如强迫用户改变他们的下次登录时输入密码,拒绝用户有权自行更改密码,将密码设置为从不过期,到什么时候过期,以及这些可以使用完成任务UserAccountControl 标志是在诉讼中证明部分.

请参考这篇很棒的MSDN 文章:管理用户密码示例和文档关于这些功能.

CONST HEX------------------------------------------脚本 0x0001帐户禁用 0x0002HOMEDIR_REQUIRED 0x0008锁定 0x0010PASSWD_NOTREQD 0x0020PASSWD_CANT_CHANGE 0x0040ENCRYPTED_TEXT_PWD_ALLOWED 0x0080TEMP_DUPLICATE_ACCOUNT 0x0100NORMAL_ACCOUNT 0x0200INTERDOMAIN_TRUST_ACCOUNT 0x0800WORKSTATION_TRUST_ACCOUNT 0x1000SERVER_TRUST_ACCOUNT 0x2000DONT_EXPIRE_PASSWORD 0x10000MNS_LOGON_ACCOUNT 0x20000SMARTCARD_REQUIRED 0x40000TRUSTED_FOR_DELEGATION 0x80000NOT_DELEGATED 0x100000USE_DES_KEY_ONLY 0x200000DONT_REQ_PREAUTH 0x400000密码过期 0x800000TRUSTED_TO_AUTH_FOR_DELEGATION 0x1000000

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 Process.Start and passing its parameters but it seems that the "net user" is unable to set the "Password never expires" to never.

解决方案

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

这篇关于创建本地用户帐户 c# 和 .NET 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆