Create Windows Account Programmatically C# : CSharp
This is a discussion on Create Windows Account Programmatically C# within the CSharp forums in Programming Languages category; 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...
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| Programmatically via C#? How could I do this? private void CreateUser(string userName, string password) { / how? } Thanks in advance! Nikolay |
|
#2
| |||
| |||
| "Nikolay Podkolzin" <NikolayPodkolzin@discussions.microsoft.com> wrote in message news:E3F73A95-1BBD-4959-8C5F-8868C3040DD7@microsoft.com... > 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. |
|
#3
| |||
| |||
| Could you be so nice and give me a code, how could I do that. Thank you! "Willy Denoyette [MVP]" wrote: > "Nikolay Podkolzin" <NikolayPodkolzin@discussions.microsoft.com> wrote in > message news:E3F73A95-1BBD-4959-8C5F-8868C3040DD7@microsoft.com... > > 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. > > |
|
#4
| |||
| |||
| 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" wrote: > Could you be so nice and give me a code, how could I do that. > > Thank you! > > "Willy Denoyette [MVP]" wrote: > > > "Nikolay Podkolzin" <NikolayPodkolzin@discussions.microsoft.com> wrote in > > message news:E3F73A95-1BBD-4959-8C5F-8868C3040DD7@microsoft.com... > > > 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. > > > > |
|
#5
| |||
| |||
| Sorry the last 6 lines starting with staff should start with user. "tony lock" wrote: > 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" wrote: > > > Could you be so nice and give me a code, how could I do that. > > > > Thank you! > > > > "Willy Denoyette [MVP]" wrote: > > > > > "Nikolay Podkolzin" <NikolayPodkolzin@discussions.microsoft.com> wrote in > > > message news:E3F73A95-1BBD-4959-8C5F-8868C3040DD7@microsoft.com... > > > > 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. > > > > > > |
|
#6
| |||
| |||
| 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" wrote: > 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" wrote: > > > Could you be so nice and give me a code, how could I do that. > > > > Thank you! > > > > "Willy Denoyette [MVP]" wrote: > > > > > "Nikolay Podkolzin" <NikolayPodkolzin@discussions.microsoft.com> wrote in > > > message news:E3F73A95-1BBD-4959-8C5F-8868C3040DD7@microsoft.com... > > > > 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. > > > > > > |

