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

2023-09-02 23:43:08 作者:兜兜转转回到原点

我想,以确定如果用户是Active Directory(AD)组内部的ASP.NET 4.0应用程序中的一员。下面的code抛出一个试图访问已卸载的应用程序域的时候,用户不是AD组的成员异常错误在最后一行(return语句)的情况下。

I am trying to determine if a user is a member of an Active Directory (AD) group for an internal ASP.NET 4.0 application. The code below throws an "Attempted to access an unloaded appdomain" exception error on the last line (return statement) in the case when the user is not a member of the AD group.

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

就如何解决或其他解决方法任何想法?

Any ideas on how to fix or other workarounds?

推荐答案

莫非this错误是你的问题?

我已经解决了使用此解决办法同样的问题:

I've solved the same problems using this workaround:

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