Create Windows Account Programmatically C#

  • Thread starter Thread starter Nikolay Podkolzin
  • Start date Start date
N

Nikolay Podkolzin

Good afternoon. Could you help me? I need to create Windows Account
Programmatically via C#? How could I do this?

private void CreateUser(string userName, string password)
{
/ how?
}

Thanks in advance!

Nikolay
 
Nikolay Podkolzin said:
Good afternoon. Could you help me? I need to create Windows Account
Programmatically via C#? How could I do this?

private void CreateUser(string userName, string password)
{
/ how?
}

Thanks in advance!

Nikolay


It depends on what Windows Account you are talking about (local, domain),
the domain type, the OS version and the framework version.
On V3.5 of the framework, you can use the
System.DirectoryServices.AccountManagement namespace, while on V2 you can
use System.DirectoryServices.

Willy.
 
The following has been edited out of a bit of code I use but should work;

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
private void CreateUser(string userName, string password)
{
DirectorySearcher dseSearcher = new DirectorySearcher();
string rootDSE = dseSearcher.SearchRoot.Path;
string userDSE = rootDSE.Insert(7, "OU=Users,");
DirectoryEntry userDE = new DirectoryEntry(userDSE);
DirectoryEntry user = userDE.Children.Add("CN=" + userID, "user");
staff.Properties["samAccountName"].Value = userID;
staff.Properties["UserPrincipalName"].Value = userName +
@"@domain";
staff.CommitChanges();
staff.Properties["userAccountControl"].Value =
ActiveDs.ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT |
ActiveDs.ADS_USER_FLAG.ADS_UF_DONT_EXPIRE_PASSWD;
staff.CommitChanges();
staff.Invoke("SetPassword", new Object[] { password });
}

for adding a domain account

Tony
 
Sorry the last 6 lines starting with staff should start with user.

tony lock said:
The following has been edited out of a bit of code I use but should work;

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
private void CreateUser(string userName, string password)
{
DirectorySearcher dseSearcher = new DirectorySearcher();
string rootDSE = dseSearcher.SearchRoot.Path;
string userDSE = rootDSE.Insert(7, "OU=Users,");
DirectoryEntry userDE = new DirectoryEntry(userDSE);
DirectoryEntry user = userDE.Children.Add("CN=" + userID, "user");
staff.Properties["samAccountName"].Value = userID;
staff.Properties["UserPrincipalName"].Value = userName +
@"@domain";
staff.CommitChanges();
staff.Properties["userAccountControl"].Value =
ActiveDs.ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT |
ActiveDs.ADS_USER_FLAG.ADS_UF_DONT_EXPIRE_PASSWD;
staff.CommitChanges();
staff.Invoke("SetPassword", new Object[] { password });
}

for adding a domain account

Tony

Nikolay Podkolzin said:
Could you be so nice and give me a code, how could I do that.

Thank you!
 
Thank you, guys for your replies. I guess I find out how could I Create or
modify user in domain:

PrincipalContext pc = new
PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, "your domain");

UserPrincipal up = UserPrincipal.FindByIdentity(pc,
IdentityType.Sid, WindowsIdentity.GetCurrent().User.Value);

That's regarding the currunt user; if you need to create a new user do this:

UserPrincipal newUser = new UserPrincipal(pc, "Some NEw Name", "password",
true /*Enabled or not*/);
newUser.Save();






tony lock said:
The following has been edited out of a bit of code I use but should work;

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
private void CreateUser(string userName, string password)
{
DirectorySearcher dseSearcher = new DirectorySearcher();
string rootDSE = dseSearcher.SearchRoot.Path;
string userDSE = rootDSE.Insert(7, "OU=Users,");
DirectoryEntry userDE = new DirectoryEntry(userDSE);
DirectoryEntry user = userDE.Children.Add("CN=" + userID, "user");
staff.Properties["samAccountName"].Value = userID;
staff.Properties["UserPrincipalName"].Value = userName +
@"@domain";
staff.CommitChanges();
staff.Properties["userAccountControl"].Value =
ActiveDs.ADS_USER_FLAG.ADS_UF_NORMAL_ACCOUNT |
ActiveDs.ADS_USER_FLAG.ADS_UF_DONT_EXPIRE_PASSWD;
staff.CommitChanges();
staff.Invoke("SetPassword", new Object[] { password });
}

for adding a domain account

Tony

Nikolay Podkolzin said:
Could you be so nice and give me a code, how could I do that.

Thank you!
 
Back
Top