创建本地用户帐户C#和.NET 2.0 [英] Creating local user account c# and .NET 2.0
问题描述
如何创建使用.NET 2.0和C#的本地用户帐户,也可以设置密码永不过期永远不会。
我一直使用的Net.exe使用的Process.Start,并通过它的参数,但它似乎是网用户无法设置密码永不过期试图永远。
阅读这个优秀的$ C $的CProject文章
HOWTO:(几乎)一切在Active Directory中通过C#
有一节创建用户帐户和处理用户密码。
更新:
为了适应code本地帐户与这些替换相应的行:的
的DirectoryEntry LOCALMACHINE =新的DirectoryEntry(WINNT://+
Environment.MachineName);
的DirectoryEntry新用户= localMachine.Children.Add(LocalUser上,用户);
这里开始原来的code段的域帐户:的
公共字符串CreateUserAccount(字符串ldapPath,字符串username,
字符串的userPassword)
{
字符串oGUID =的String.Empty;
尝试
{
字符串连接preFIX =LDAP://+ ldapPath;
的DirectoryEntry dirEntry =新的DirectoryEntry(连接preFIX);
的DirectoryEntry新用户= dirEntry.Children.Add
(CN =+用户名,用户);
。newUser.Properties [的samAccountName]值=用户名;
INT VAL =(int)的newUser.Properties [userAccountControl的]值。
newUser.Properties [userAccountControl的]值= VAL。| 0x10000处;
newUser.CommitChanges();
oGUID = newUser.Guid.ToString();
newUser.Invoke(SetPassword,新的对象[] {userPassword的});
newUser.CommitChanges();
dirEntry.Close();
newUser.Close();
}
赶上(System.DirectoryServices.DirectoryServicesCOMException E)
{
// DoSomethingwith - > E.Message.ToString();
}
返回oGUID;
}
有一些细节理解 当用户密码处理和 周围的密码,如边界 迫使用户能够改变他们的 密码在下次登录时,否认 用户更改自己的权利 密码,设置密码永不 到期,何时到期,并且这些 任务可以使用来实现 UserAccountControl标志是 在诉讼证明 部分。
请参考这个伟大的 MSDN文章:管理用户密码 为例子和文档 关于这些功能。
CONST HEX
------------------------------------------
SCRIPT 0×0001
ACCOUNTDISABLE×0002
HOMEDIR_REQUIRED×0008
封锁×0010
PASSWD_NOTREQD 0×0020
PASSWD_CANT_CHANGE 0x0040
ENCRYPTED_TEXT_PWD_ALLOWED 0x0080
TEMP_DUPLICATE_ACCOUNT 0100
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
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屋!