3

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.

    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();

    }

or

    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();
        }
    }
4
  • 2
    "The stub received bad data" - this is unlikely to be the actual error message. Can you give us the errors you are getting and what line of the code is causing the errors? Thanks Commented Aug 31, 2012 at 12:15
  • @hugh-I get the error at process.Start(); line. Commented Aug 31, 2012 at 12:33
  • @hugh- None of the answer helped me. I can't simply accept it right? Commented Sep 14, 2012 at 5:17
  • It is polite to award the answer to the post which "best answers your question". Whether the answerer directly helped or not is more down to you than the answerer. For example, one of your answerers did exactly what you asked in this answer: stackoverflow.com/a/12175838/569662 Commented Sep 14, 2012 at 8:48

2 Answers 2

3

You can use the Process class with different credentials - instead of setting it directly, use ProcessStartInfo.

You can set a UserName and Password on the ProcessStartInfo class for the user you wish to execute as (Password is a SecureString, by the way) - pass this to the Process constructor and you are good to go.

ProcessStartInfo startInfo = new ProcessStartInfo("net.exe");
startInfo.UserName = Administrator;
startInfo.Password = ...;

...

Process.Start(startInfo);
Sign up to request clarification or add additional context in comments.

4 Comments

Hi oded, In my question i mentioned that , i did the same as you are describing. But I got the error "The stub received bad data".
@Saran - Hardly the same. I am using ProcessStartInfo and you are not. Also, as Hugh commented, the error you are getting does not seem related to the code.
I have re-posted the method , with the user name and password. It did not worked.
It will be of great help, if you can post a sample working code tested with the limited user ,
0

I had the same error and found that I needed to specify the Domain parameter in the ProcessStartInfo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.