确定用户是否在.NET 4.0应用程序的AD组中

我试图确定用户是否是内部ASP.NET 4.0应用程序的Active Directory(AD)组的成员。 在用户不是AD组成员的情况下,下面的代码在最后一行(return语句)上抛出“尝试访问已卸载的appdomain”exception错误。

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); } 

有关如何修复或其他解决方法的任何想法?

这个 bug可能是你的问题吗?

我使用此解决方法解决了相同的问题:

  using (DirectoryEntry rootDse = new DirectoryEntry("LDAP://rootdse")) { var dnsName = rootDse.Properties["dnsHostName"].Value.ToString(); using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, dnsName)) {} 

同样的问题在这里

它似乎是ADSI中的一个错误,通过修补程序解决。 Windows 7 SP1和Windows Server 2008 R2 SP1不包含此修复程序,因此需要在开发计算机和服务器环境中手动部署它。

http://support.microsoft.com/kb/2683913