Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 14, 2007 09:14 AM by bips_kr
Member
587 Points
149 Posts
Dec 27, 2006 03:20 AM|LINK
How do we create a user with admin rights in C# using DirectoryEntry (In window application),
Following is the code I have written
private void CreateUserAccountOnLocalMachine(string login , string password , string fullName)
{
DirectoryEntry dirEntry =
new DirectoryEntry();
dirEntry =
new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntries entries = dirEntry.Children;
DirectoryEntry newUser =
newUser = entries.Add (login, "Administrator");
newUser.Invoke("SetPassword", password);
newUser.CommitChanges();
}
Do let me know how we make the above user an "Administrator"?
Thanks in advance.
Participant
1139 Points
201 Posts
Dec 27, 2006 12:26 PM|LINK
Once you have created your new user, i think your need to add them to a Group that has Admin rights, some examples can be found here :
http://msdn2.microsoft.com/en-us/library/ms180904.aspx
Hopefully thats what you wanted [:D]
Dec 27, 2006 05:59 PM|LINK
Dec 28, 2006 08:12 AM|LINK
Jan 14, 2007 09:14 AM|LINK
Following is the soultion.
public void CreateUserAccount(string login , string password , string fullName, bool isAdmin) { try { DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); DirectoryEntries entries = dirEntry.Children; DirectoryEntry newUser = entries.Add (login, "user"); newUser.Properties["FullName"].Add(fullName); newUser.Invoke("SetPassword", password); newUser.CommitChanges(); // Remove the if condition along with the else to create user account in "user" group. DirectoryEntry grp; if (isAdmin) { grp = dirEntry.Children.Find("Administrators", "group"); if (grp != null) {grp.Invoke("Add", new object[] {newUser.Path.ToString()});} } else { grp = dirEntry.Children.Find("Guests", "group"); if (grp != null) {grp.Invoke("Add", new object[] {newUser.Path.ToString()});} } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
bips_kr
Member
587 Points
149 Posts
How do we create a user with admin rights in C# using DirectoryEntry
Dec 27, 2006 03:20 AM|LINK
How do we create a user with admin rights in C# using DirectoryEntry (In window application),
Following is the code I have written
private void CreateUserAccountOnLocalMachine(string login , string password , string fullName)
{
DirectoryEntry dirEntry =
new DirectoryEntry();
dirEntry =
new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntries entries = dirEntry.Children;
DirectoryEntry newUser =
new DirectoryEntry();
newUser = entries.Add (login, "Administrator");
newUser.Invoke("SetPassword", password);
newUser.CommitChanges();
}
Do let me know how we make the above user an "Administrator"?
Thanks in advance.
Bipul Kumar
Hope4sun
Participant
1139 Points
201 Posts
Re: How do we create a user with admin rights in C# using DirectoryEntry
Dec 27, 2006 12:26 PM|LINK
Once you have created your new user, i think your need to add them to a Group that has Admin rights, some examples can be found here :
http://msdn2.microsoft.com/en-us/library/ms180904.aspx
Hopefully thats what you wanted [:D]
bips_kr
Member
587 Points
149 Posts
Re: How do we create a user with admin rights in C# using DirectoryEntry
Dec 27, 2006 05:59 PM|LINK
Bipul Kumar
Hope4sun
Participant
1139 Points
201 Posts
Re: How do we create a user with admin rights in C# using DirectoryEntry
Dec 28, 2006 08:12 AM|LINK
bips_kr
Member
587 Points
149 Posts
Re: How do we create a user with admin rights in C# using DirectoryEntry
Jan 14, 2007 09:14 AM|LINK
Following is the soultion.
public void CreateUserAccount(string login , string password , string fullName, bool isAdmin) { try { DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); DirectoryEntries entries = dirEntry.Children; DirectoryEntry newUser = entries.Add (login, "user"); newUser.Properties["FullName"].Add(fullName); newUser.Invoke("SetPassword", password); newUser.CommitChanges(); // Remove the if condition along with the else to create user account in "user" group. DirectoryEntry grp; if (isAdmin) { grp = dirEntry.Children.Find("Administrators", "group"); if (grp != null) {grp.Invoke("Add", new object[] {newUser.Path.ToString()});} } else { grp = dirEntry.Children.Find("Guests", "group"); if (grp != null) {grp.Invoke("Add", new object[] {newUser.Path.ToString()});} } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }Bipul Kumar