繁体   English   中英

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

[英]Determine if user is in AD group for .NET 4.0 application

我试图确定用户是否是内部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);
}

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

这个 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM