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

本文关键字:应用程序 AD 组中 net 用户 是否 | 更新日期: 2023-09-27 18:06:33

我正在尝试确定用户是否是内部ASP的Active Directory (AD)组的成员。NET 4.0应用程序。如果用户不是AD组的成员,下面的代码在最后一行(返回语句)抛出" attempt to access an unloaded 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);
}

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

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

这个bug是你的问题吗?

我用这个方法解决了同样的问题:

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

这里也是一样。

这似乎是ADSI中的一个bug,已经通过hotfix解决了。Windows 7 SP1和Windows Server 2008 R2 SP1不包括修复,因此需要手动部署到您的开发机器和服务器环境中。

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