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?

有帮助吗?

解决方案

Could this bug be your problem?

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

其他提示

Same issue here.

It appears to be a bug in ADSI that was resolved with a hotfix. Windows 7 SP1 and Windows Server 2008 R2 SP1 don't include the fix, so it will need to be manually deployed on your development machines and server environments.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top