0
我需要遞歸搜索Active Directory組以查看用戶帳戶是否在給定組中;但是,用戶所屬的某些組未由GetAuthorizationGroups()方法返回。UserPrincipal.GetAuthorizationGroups()不檢索所有組
功能是:
public static bool IsUserAuthorized(string adGroup, string userName)
{
bool isInRole = false;
using (var ctx = new PrincipalContext(ContextType.Domain, "MyDomain"))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName))
{
isInRole = user != null && user.GetAuthorizationGroups().Any(g => g.SamAccountName == adGroup);
}
}
return isInRole;
}
我調用該函數像這樣:
IsUserAuthorized("myGroup", "MyDomain\MyUser");