For Windows, Microsoft provides three main directory platforms: Active Directory domain service, and local security account manager (SAM) Data Storage on each Windows computer, and compare the new Active Directory light directory service or ad lDs (that is, the Active Directory Application Mode you know earlier or Adam ). This blog post is not about the use of the Active Directory. For details, refer to the msdn article to introduce the programming of the Active Directory: http://msdn.microsoft.com/zh-cn/magazine/cc135979.aspx.
This article mainly records an error that is often thrown when the getauthorizationgroups () interface is used or when the user is used: For details, refer to Microsoft connect APIs.
Microsoft has not provided a solution. This problem is also discussed in stackoverflow.
In the stackoverflow discussion, there is a solution. When an appdomainunloadedexception error occurs, you can use sleep for a period of time to call this interface again:
private PrincipalSearchResult<Principal> GetAuthorizationGroups(UserPrincipal userPrincipal, int tries)
{
try
{
return userPrincipal.GetAuthorizationGroups();
}
catch (FileNotFoundException ex)
{
if (tries > 5)
throw; tries++;
Thread.Sleep(1000);
return GetAuthorizationGroups(userPrincipal, tries);
}
catch (AppDomainUnloadedException ex)
{
if (tries > 5)
throw; tries++;
Thread.Sleep(1000);
return GetAuthorizationGroups(userPrincipal, tries);
}
}
This will cause a problem. If an exception occurs, the interface will be very slow. This can be solved by introducing the cache mechanism:
public override String[] GetRolesForUser(String username)
{
// If SQL Caching is enabled, try to pull a cached value.
if (_EnableSqlCache)
{
String CachedValue;
CachedValue = GetCacheItem('U', username);
if (CachedValue != "*NotCached")
{
return CachedValue.Split(',');
}
}
ArrayList results = new ArrayList();
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, null, _DomainDN))
{
try
{
UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
var tries = 0;
var groups = GetAuthorizationGroups(p, tries);
foreach (GroupPrincipal group in groups)
{
if (!_GroupsToIgnore.Contains(group.SamAccountName))
{
if (_IsAdditiveGroupMode)
{
if (_GroupsToUse.Contains(group.SamAccountName))
{
results.Add(group.SamAccountName);
}
}
else
{
results.Add(group.SamAccountName);
}
}
}
}
catch (Exception ex)
{
throw new ProviderException("Unable to query Active Directory.", ex);
}
}
// If SQL Caching is enabled, send value to cache
if (_EnableSqlCache)
{
SetCacheItem('U', username, ArrayListToCSString(results));
}
return results.ToArray(typeof(String)) as String[];
}
The above Code comes from Active Directory roles provider.