public bool IsUserGroupMember(string user, string unit)
{
bool member = false;
try
{
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
string[] groups = unit.Split(',');
foreach (string word in groups)
{
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, word);
if (grp != null)
{
foreach (Principal p in grp.GetMembers(true))
{
if (p.SamAccountName == user)
{
member = true;
grp.Dispose();
ctx.Dispose();
return member;
}
}
}
else
{
grp.Dispose();
ctx.Dispose();
return member;
}
}
}
catch (COMException)
{
return member;
}
return member;
}
我正在使用上面的方法,以遞歸方式查找用戶是否是Active Directory中組的成員。它運作良好。儘管有時我會遇到一個奇怪的例外。Active Directory異常asp .net
不支持指定的方法。 foreach(grp.GetMembers(true)中的Principal p)是紅色的(抱歉,我無法上傳該例外的圖片)。最奇怪的是,它似乎是隨機拋出,如果我刷新頁面效果很好..
我試着在網上找,但現在沒有好消息就解決..
謝謝你的回覆@marc_s。我瞭解你的代碼。現在唯一的問題是我得到這個exeption:'up.GetAuthorizationGroups()'拋出了類型'System.AppDomainUnloadedException'的異常System.DirectoryServices.AccountManagement.PrincipalSearchResult {系統。 AppDomainUnloadedException}。我如何解決它? :) –
Sarah
2014-11-25 08:54:00
嗨@marc_s,你能給我任何新的提示嗎?我仍然堅持我的方法,這也需要很長的時間來處理;結果:初始頁面需要永久加載.. :( – Sarah 2014-12-09 10:51:48