Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate Windows User Using c# code
    primarykey
    data
    text
    <p>I want to create a windows user using c# code. Below methods works fine when the logged in user is having Administratorive privilage. It is not working with the limited user. Note that I can pass the windows user name and password of the administrator user. Basically I want to impersonate. I tried impersonation it did not work. I tried passing user namd and password to the processinfo. I got the error "The stub received bad data". So can any one help me on how to create the windows user using c# code by impersonation.</p> <pre><code> public static void CreateUser(string userName, string password, string description, string adminUserName, string adminPassword) { Process process = new Process(); ProcessStartInfo processInfo = new ProcessStartInfo(); processInfo.WorkingDirectory = Environment.SystemDirectory; processInfo.FileName = "net.exe"; processInfo.UseShellExecute = false; processInfo.RedirectStandardError = true; processInfo.RedirectStandardInput = true; processInfo.RedirectStandardOutput = true; processInfo.WindowStyle = ProcessWindowStyle.Hidden; processInfo.Arguments = @" user " + userName + @" " + password + @" /ADD /ACTIVE:YES " + @"/EXPIRES:NEVER /FULLNAME:" + userName + @" /PASSWORDCHG:NO /PASSWORDREQ:YES"; if (!string.IsNullOrEmpty(adminUserName)) { processInfo.UserName = adminUserName; processInfo.Password = WindowsSecurityHelper.GetSecuredString(adminPassword); } process.StartInfo = processInfo; process.Start(); process.WaitForExit(); process.Close(); } </code></pre> <p>or</p> <pre><code> public static void CreateUser(string userName, string password, string description, string userGroup = "Users") { PrincipalContext pc = new PrincipalContext(ContextType.Machine, null); GroupPrincipal gp = GroupPrincipal.FindByIdentity(pc, userGroup); if (gp != null) { UserPrincipal u = new UserPrincipal(pc); u.SetPassword(password); u.Name = userName; u.Description = description; u.UserCannotChangePassword = true; u.PasswordNeverExpires = true; u.Save(); gp.Members.Add(u); gp.Save(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload