Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. public void CreateLocalWindowsAccount(string username, string password, string displayName, string description, bool canChangePwd, bool pwdExpires)
  2. {
  3. try
  4. {
  5. PrincipalContext context = new PrincipalContext(ContextType.Machine, "127.0.0.1");
  6. UserPrincipal user = new UserPrincipal(context);
  7. user.SetPassword(password);
  8. user.DisplayName = displayName;
  9. user.Name = username;
  10. user.Description = description;
  11. user.UserCannotChangePassword = canChangePwd;
  12. user.PasswordNeverExpires = pwdExpires;
  13. user.Save();
  14.  
  15. //now add user to "Users" group so it displays in Control Panel
  16. GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "Remote Desktop Users");
  17. group.Members.Add(user);
  18. group.Save();
  19. }
  20. catch (Exception ex)
  21. {
  22. Response.Write(ex.ToString());
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement