Objectmix
Tags Register Mark Forums Read

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...


Object Mix > Programming Languages > CSharp > Create Windows Account Programmatically C#

Reply

 

LinkBack Thread Tools
  #1  
Old 03-04-2008, 08:23 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Create Windows Account Programmatically C#

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
  #2  
Old 03-04-2008, 08:48 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Create Windows Account Programmatically C#

"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  
Old 03-04-2008, 09:16 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Create Windows Account Programmatically C#

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  
Old 03-04-2008, 09:43 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Create Windows Account Programmatically C#

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  
Old 03-04-2008, 09:46 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Create Windows Account Programmatically C#

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  
Old 03-04-2008, 09:49 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: Create Windows Account Programmatically C#

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.
> > >
> > >

Reply

Thread Tools



All times are GMT -5. The time now is 08:27 AM.

Managed by Infnx Pvt Ltd.