Determine if a user is in an AD group for a .NET 4.0 application

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 displays the error message "Attempting to access an unloaded appdomain" on the last line (return statement) 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 use other workarounds?

+13
c # directoryservices userprincipal
Aug 23 2018-11-23T00:
source share
2 answers

Could this error be your problem?

I 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)) {} 
+15
Oct 26 '11 at 10:44
source share

Here is the problem.

It seems that the error in ADSI was resolved with a fix. Windows 7 Service Pack 1 (SP1) and Windows Server 2008 R2 Service Pack 1 (SP1) do not include the hotfix, so you will need to manually deploy it to the development computers and server environments.

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

+7
Nov 27 '12 at 22:25
source share



All Articles