기본 그룹을 제외한 GetGroups
-
20-12-2019 - |
문제
memberof 매개 변수에서 모든 그룹을 어떻게 얻을 수 있습니까?메소드 getGroups commerof + 기본 그룹을 반환합니다.
public static ArrayList GetUserGroups(string sUserName)
{
ArrayList myItems = new ArrayList();
UserPrincipal oUserPrincipal = GetUser(sUserName);
PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
foreach (Principal oResult in oPrincipalSearchResult)
{
if(NOT_PRIMARY) // <-- check by primary group ID?
// How get group GUID/ CID by ID ?
myItems.Add(oResult.Name);
}
return myItems;
}
. 해결책
해결책이 있습니다 :
public static ArrayList GetUserGroupsExcludingPrimaryGroup(string sUserName)
{
ArrayList myItems = new ArrayList();
UserPrincipalEx oUserPrincipal = GetUser(sUserName);
var objectSid = oUserPrincipal.ObjectSid.ToString();
objectSid = objectSid.Substring(0, objectSid.LastIndexOf("-")) + "-" + oUserPrincipal.PrimaryGroupID;
PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
foreach (Principal oResult in oPrincipalSearchResult)
{
if (objectSid != oResult.Sid.ToString())
myItems.Add(oResult.Name);
}
return myItems;
}
.
계정 관리 - override
namespace System.DirectoryServices.AccountManagement
{
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("User")]
public class UserPrincipalEx : UserPrincipal
{
public UserPrincipalEx(PrincipalContext context) : base(context) { }
public UserPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) { }
...
[DirectoryProperty("primaryGroupID")]
public Int32 PrimaryGroupID
{
get { return (Int32)ExtensionGet("primaryGroupID")[0]; }
set { ExtensionSet("primaryGroupID", value); }
}
[DirectoryProperty("objectSid")]
public SecurityIdentifier ObjectSid
{
get { return new SecurityIdentifier((byte[])ExtensionGet("objectSid")[0], 0); }
set { ExtensionSet("objectSid", value); }
}
}
}
. 제휴하지 않습니다 StackOverflow