private PrincipalContext GetPrincipalContext() { var targetAddress = this.TargetInfo.GetAddress(); var fullyQualifiedUsername = this.TargetInfo.credentials.GetFullyQualifiedUsername(); return(AccManUtils.accManConnect(targetAddress, fullyQualifiedUsername, this.TargetInfo.credentials.GetPassword())); }
/// <summary> /// Gets the groups the user belongs /// </summary> /// <param name="username">The user name in FQDN format. For SYSTEM accounts do not use FQDN.</param> /// <param name="returnType">Defines wether will be returned the group name ou SID.</param> /// <returns>A list of group names or group SIDs according with returnType argument.</returns> public virtual IEnumerable <String> GetUserGroups(string username, AccountSearchReturnType returnType) { var address = this.TargetInfo.GetAddress(); var credentials = this.TargetInfo.credentials; var principalContext = AccManUtils.accManConnect(address, credentials.GetUserName(), credentials.GetPassword()); Principal user = null; var isSystemAccount = !username.Contains("\\"); if (isSystemAccount) { var wqlGetUserSID = GetWqlToGetUserDetails(username); var wqlResult = this.WmiProvider.ExecuteWQL(wqlGetUserSID).FirstOrDefault(); if (wqlResult != null) { var userSID = wqlResult.GetFieldValueAsString("SID"); user = Principal.FindByIdentity(principalContext, IdentityType.Sid, userSID); } } else { user = Principal.FindByIdentity(principalContext, IdentityType.SamAccountName, username); if (user == null) { user = Principal.FindByIdentity(principalContext, IdentityType.Name, username); } } var groups = new List <String>(); if (user == null) { return(groups); } var foundGroups = user.GetGroups(); if (foundGroups == null) { return(groups); } if (returnType.Equals(AccountSearchReturnType.SID)) { return(foundGroups.Select(g => g.Sid.Value)); } return(foundGroups.Select(g => g.Name)); }
/// <summary> /// Gets the groups the user belongs /// </summary> /// <param name="username">The user name in FQDN format. For SYSTEM accounts do not use FQDN.</param> /// <param name="returnType">Defines wether will be returned the group name ou SID.</param> /// <returns>A list of group names or group SIDs according with returnType argument.</returns> public virtual IEnumerable <String> GetUserGroups(string username, AccountSearchReturnType returnType) { var address = this.TargetInfo.GetAddress(); var credentials = this.TargetInfo.credentials; Principal user = null; var isSystemAccount = !username.Contains("\\"); if (isSystemAccount) { return(GetAllGroupsFromUser(username, returnType)); } else { var principalContext = AccManUtils.accManConnect(address, credentials.GetUserName(), credentials.GetPassword()); user = Principal.FindByIdentity(principalContext, IdentityType.SamAccountName, username); if (user == null) { user = Principal.FindByIdentity(principalContext, IdentityType.Name, username); } } var groups = new List <String>(); if (user == null) { return(groups); } var foundGroups = user.GetGroups(); if (foundGroups == null) { return(groups); } if (returnType.Equals(AccountSearchReturnType.SID)) { return(foundGroups.Select(g => g.Sid.Value)); } return(foundGroups.Select(g => g.Name)); }