我试图确定用户是否是内部ASP.NET 4.0应用程序的Active Directory(AD)组的成员.在用户不是AD组成员的情况下,下面的代码在最后一行(return语句)上抛出"尝试访问已卸载的appdomain"异常错误.
public static bool IsInADGroup(string userName, string groupName)
{
var principalContext = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, userName);
if (userPrincipal == null)
return false;
GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName);
if (groupPrincipal == null)
return false;
return userPrincipal.IsMemberOf(groupPrincipal);
}
Run Code Online (Sandbox Code Playgroud)
有关如何修复或其他解决方法的任何想法?