Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Create Windows Account Programmatically C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nikolay Podkolzin - 04 Mar 2008 13:23 GMT
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
Willy Denoyette [MVP] - 04 Mar 2008 13:48 GMT
> Good afternoon. Could you help me? I need to create Windows Account
> Programmatically via C#? How could I do this?
[quoted text clipped - 7 lines]
>
> 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.
Nikolay Podkolzin - 04 Mar 2008 14:16 GMT
Could you be so nice and give me a code, how could I do that.

Thank you!

> > Good afternoon. Could you help me? I need to create Windows Account
> > Programmatically via C#? How could I do this?
[quoted text clipped - 15 lines]
>
> Willy.
tony lock - 04 Mar 2008 14:43 GMT
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

> Could you be so nice and give me a code, how could I do that.
>
[quoted text clipped - 19 lines]
> >
> > Willy.
tony lock - 04 Mar 2008 14:46 GMT
Sorry the last 6 lines starting with staff should start with user.

> The following has been edited out of a bit of code I use but should work;
>
[quoted text clipped - 45 lines]
> > >
> > > Willy.
Nikolay Podkolzin - 04 Mar 2008 14:49 GMT
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();

> The following has been edited out of a bit of code I use but should work;
>
[quoted text clipped - 45 lines]
> > >
> > > Willy.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.