0

I would like to programmatically add new local user to a computer. I found the code here. But I would like that the user could login without password. I saw that there is an option UserPrincipal.PasswordNotRequired=true but if I don't use SetPassword() it throws me an exception:

The password does not meet the password policy requirements...

Is it possible to make a new user with no password?

EDIT: the current code, which adds a new user successfully, but I have to provide some password.It is a complete copy from the link provided:

PrincipalContext oPrincipalContext = GetPrincipalContext();

UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext);
oUserPrincipal.Name = sUserName;
oUserPrincipal.SetPassword(sPassword);
oUserPrincipal.DisplayName = windowsUsername.Text;
oUserPrincipal.PasswordNeverExpires = true;
oUserPrincipal.PasswordNotRequired = true;
oUserPrincipal.Save();

GroupPrincipal usersGroup = GroupPrincipal.FindByIdentity(oPrincipalContext, "Users");
usersGroup.Members.Add(oUserPrincipal);
usersGroup.Save();
gtu
  • 707
  • 1
  • 10
  • 22
  • You can set up a group account so the users password on the remote PC and the password on the local PC are the same so no password is required when remote connecting. You would need to setup the same Group account on both local and remote PC. Also the user has to be added to the Group. – jdweng May 30 '18 at 10:13
  • can you show a more complete version of your code – Simon Price May 30 '18 at 10:19
  • Hi, thanks for this. The case is, that I don't have any remote PCs. I only want to run a program which would create new user account with no password. I need that to automatically configure some computers. – gtu May 30 '18 at 10:21
  • is your machine that youre trying to add a user too part of a domain? – Simon Price May 30 '18 at 10:59
  • I think so, because the context (domain) is `PrincipalContext(ContextType.Machine);` So basically local machine. – gtu May 30 '18 at 11:02
  • I cant see anything wrong with the code, so suspect that this will be down to the machine being part of a domain and the machine having a group policy applied. – Simon Price May 30 '18 at 11:03
  • I'll show an answer that works on my machine, but I suspect its down to your local machine policies that may be applied – Simon Price May 30 '18 at 11:06

1 Answers1

0

There are two potential reasons why your code isnt working.

  1. Youre not running as admin
  2. Your machine is on a domain with a group policy preventing what you want to do.

The code below has been tested on my machine and is working.

void Main()
{
    PrincipalContext oPrincipalContext = GetPrincipalContext();

    UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext);
    oUserPrincipal.Name = "TestUser";
    oUserPrincipal.SetPassword("");
    oUserPrincipal.DisplayName = "TestUser";
    oUserPrincipal.PasswordNeverExpires = true;
    oUserPrincipal.PasswordNotRequired = true;
    oUserPrincipal.Save();

    GroupPrincipal usersGroup = GroupPrincipal.FindByIdentity(oPrincipalContext, "Users");
    usersGroup.Members.Add(oUserPrincipal);
    usersGroup.Save();
}

PrincipalContext GetPrincipalContext()
{
    var dc = new PrincipalContext(ContextType.Machine);
    return dc;
}

Things for you to try, 1. Try it on a machine that is not on your domain. 2. Try it on a machine that does not have any group policies applied. 3. Run your app as admin on the machine that you're having the issue with

Simon Price
  • 3,011
  • 3
  • 34
  • 98
  • The reason was that I developed on a computer with domain group policy. When I tried to run executable on target PC in worked even without password - as expected. Thank you very much for your help – gtu May 30 '18 at 11:25