When the error of calling getauthorizationgroups ()-"attempting to access the uninstalled application domain" (exception from hresult: 0 × 80131014) Solution

Source: Internet
Author: User
Tags samaccountname

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.