How to Create Local User Account

  • Thread starter Thread starter James
  • Start date Start date
J

James

Hi,

I'm new to .net development and need some help urgently.

I need to write an app that creates a new local user account on a machine
that does not have Active Directory installed.

In old vb6 world I could probably have achieved this with a few API calls,
but where do I start in vb.net?

I guess my starting point is in the System.Security namespace, but I'm
confused by the relationship between Identities and Principals. This is new
terminology to me.

Any links to helpful web pages, source code or examples would be much
appreciated.

Thanks,

James
 
I'm new to .net development and need some help urgently.
I need to write an app that creates a new local user account on a machine
that does not have Active Directory installed.

Use the System.DirectoryServices namespace - it contains a class
called "DirectoryEntry", which you can use to create a local account.
Sample code in C#.

You need to do the following steps:

0) Add a reference to the System.DirectoryServices assembly to your
project (in the solution explorer window), and add the "using
System.DirectoryServices" line to your code file.

1) Bind to your computer
DirectoryEntry deComputer = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");

2) Create a new user
deComputer.Children.Add("MyUser", "user");

3) Commit the changes
deComputer.CommitChanges();

Hope that helps
Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Hi James,

I while ago I posted the code below to adsi.general here at MSDN. Should do
the trick.

Niels

public static bool CreateLocalUser(string userName, string password, string
description, string defaultGroup)
{
using (DirectoryEntry AD = new
DirectoryEntry("WinNT://"+Environment.MachineName + ",computer"))
{
bool created = false;
bool found = false;

try
{
found = AD.Children.Find(userName,"user")!=null;
}
catch
{
found = false;
}

if (!found)
{
using (DirectoryEntry NewUser =
AD.Children.Add(userName, "user"))
{
NewUser.Invoke("SetPassword", new object[]
{password});
NewUser.Invoke("Put", new object[] {"Description",
description});
NewUser.CommitChanges();

created = true;

if (defaultGroup!=null)
{
DirectoryEntry grp = null;

try
{
using ( grp = AD.Children.Find(defaultGroup,
"group"))
{
if (grp != null)
{
grp.Invoke("Add", new object[]
{NewUser.Path.ToString()});
}
}
}
catch
{
// absorb?
}
}

}
}
return created;
}
}
 
Thanks,

This is a great help.

James
Marc Scheuner said:
Use the System.DirectoryServices namespace - it contains a class
called "DirectoryEntry", which you can use to create a local account.
Sample code in C#.

You need to do the following steps:

0) Add a reference to the System.DirectoryServices assembly to your
project (in the solution explorer window), and add the "using
System.DirectoryServices" line to your code file.

1) Bind to your computer
DirectoryEntry deComputer = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");

2) Create a new user
deComputer.Children.Add("MyUser", "user");

3) Commit the changes
deComputer.CommitChanges();

Hope that helps
Marc

================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Thanks,

This is a great help

James
Niels Flensted-Jensen said:
Hi James,

I while ago I posted the code below to adsi.general here at MSDN. Should
do
the trick.

Niels

public static bool CreateLocalUser(string userName, string password,
string
description, string defaultGroup)
{
using (DirectoryEntry AD = new
DirectoryEntry("WinNT://"+Environment.MachineName + ",computer"))
{
bool created = false;
bool found = false;

try
{
found = AD.Children.Find(userName,"user")!=null;
}
catch
{
found = false;
}

if (!found)
{
using (DirectoryEntry NewUser =
AD.Children.Add(userName, "user"))
{
NewUser.Invoke("SetPassword", new object[]
{password});
NewUser.Invoke("Put", new object[] {"Description",
description});
NewUser.CommitChanges();

created = true;

if (defaultGroup!=null)
{
DirectoryEntry grp = null;

try
{
using ( grp =
AD.Children.Find(defaultGroup,
"group"))
{
if (grp != null)
{
grp.Invoke("Add", new object[]
{NewUser.Path.ToString()});
}
}
}
catch
{
// absorb?
}
}

}
}
return created;
}
}



James said:
Hi,

I'm new to .net development and need some help urgently.

I need to write an app that creates a new local user account on a machine
that does not have Active Directory installed.

In old vb6 world I could probably have achieved this with a few API
calls,
but where do I start in vb.net?

I guess my starting point is in the System.Security namespace, but I'm
confused by the relationship between Identities and Principals. This is
new
terminology to me.

Any links to helpful web pages, source code or examples would be much
appreciated.

Thanks,

James
 
Back
Top