Guest User

Untitled

a guest
Feb 12th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. public void Apply(Section.User.User user, Action<string> status)
  2. {
  3. #region Sanity Checks
  4.  
  5. if (user == null)
  6. {
  7. throw new ArgumentNullException("user");
  8. }
  9.  
  10. if (status == null)
  11. {
  12. throw new ArgumentNullException("status");
  13. }
  14. #endregion
  15. _logger.Debug(string.Format("Starting to apply the user with name {0}", user.UserName));
  16. status(string.Format("Applying User {0} to the system.", user.UserName));
  17.  
  18. using (PrincipalContext pc = new PrincipalContext(ContextType.Machine))
  19. {
  20.  
  21. UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(pc, user.UserName);
  22. try
  23. {
  24. _logger.Debug("Checking if user already exists");
  25. if (userPrincipal == null)
  26. {
  27. userPrincipal = CreateNewUser(user, pc);
  28. }
  29.  
  30. _logger.Debug("Setting user password and applying to the system.");
  31. userPrincipal.SetPassword(user.UserPassword);
  32. userPrincipal.Save();
  33.  
  34. Task<PrincipalSearchResult<Principal>> groups =
  35. Task<PrincipalSearchResult<Principal>>.Factory.StartNew(userPrincipal.GetGroups);
  36.  
  37. _logger.Debug("Adding user to the groups.");
  38. AddUserToGroups(pc, userPrincipal, groups, user.UserType.Equals(UserType.WorkerProcess.ToString()) ? "Administrators" : "Users", "IIS_IUSRS");
  39. AddCurrentUser(user);
  40. }
  41. finally
  42. {
  43. if (userPrincipal != null)
  44. {
  45. userPrincipal.Dispose();
  46. }
  47. }
  48.  
  49.  
  50. }
  51.  
  52. }
  53.  
  54. private UserPrincipal CreateNewUser(Section.User.User user, PrincipalContext principal)
  55. {
  56. _logger.Debug("User did not exist creating now.");
  57. UserPrincipal newUser = new UserPrincipal(principal)
  58. {
  59. Name = user.UserName,
  60. Description = user.UserDescription,
  61. UserCannotChangePassword = false,
  62. PasswordNeverExpires = true,
  63. PasswordNotRequired = false
  64. };
  65. _logger.Debug("User created.");
  66. return newUser;
  67. }
  68.  
  69. private void AddUserToGroups(PrincipalContext principal, UserPrincipal user, Task<PrincipalSearchResult<Principal>> userGroups, params string[] groups)
  70. {
  71. groups.AsParallel().ForAll(s =>
  72. {
  73. using (GroupPrincipal gp = GroupPrincipal.FindByIdentity(principal, s))
  74. {
  75. _logger.Debug(string.Format("Checking if user is alread in the group."));
  76. if (gp != null && !userGroups.Result.Contains(gp))
  77. {
  78. _logger.Debug(string.Format("The user was not a member of {0} adding them now.", gp.Name));
  79. //This is the point that the 7 minute hang starts
  80. gp.Members.Add(user);
  81. gp.Save();
  82.  
  83. _logger.Debug(string.Format("User added to {0}.", gp.Name));
  84. }
  85. }
  86. });
  87. }
  88.  
  89. gp.Members.Add( user );
  90.  
  91. UserPrincipal user = this is your user;
  92. GroupPrincipal group = this is your group;
  93.  
  94. // this is fast
  95. using ( DirectoryEntry groupEntry = group.GetUnderlyingObject() as DirectoryEntry )
  96. using ( DirectoryEntry userEntry = user.GetUnderlyingObject() as DirectoryEntry )
  97. {
  98. groupEntry.Invoke( "Add", new object[] { userEntry.Path } );
  99. }
  100.  
  101. //group.Members.Add(user); // and this is slow!
  102. //group.Save();
Add Comment
Please, Sign In to add comment