前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >当调用GetAuthorizationGroups() 的错误-“试图访问卸载的应用程序域“(Exception from HRESULT: 0x80131014)解决方案

当调用GetAuthorizationGroups() 的错误-“试图访问卸载的应用程序域“(Exception from HRESULT: 0x80131014)解决方案

作者头像
张善友
发布2018-01-30 14:50:19
6580
发布2018-01-30 14:50:19
举报
文章被收录于专栏:张善友的专栏

针对 Windows® 平台,Microsoft 提供了三个主要目录平台:Active Directory® 域服务、每台 Windows 计算机上的本地安全帐户管理器 (SAM) 数据存储,以及比较新的 Active Directory 轻型目录服务或 AD LDS(即您先前已经知道的 Active Directory 应用程序模式或简称 ADAM)。这篇博文不是讲活动目录的使用,具体可以参照MSDN一篇文章专门介绍活动目录的编程http://msdn.microsoft.com/zh-cn/magazine/cc135979.aspx

本文主要是记录使用GetAuthorizationGroups()接口或用用户的时候经常抛出的一个错误:具体可以看Microsoft Connect http://connect.microsoft.com/VisualStudio/feedback/details/566463/attempted-to-access-an-unloaded-appdomain-exception-from-hresult-0x80131014-when-calling-getauthorizationgroups

微软并没有给出解决方案,在stackoverflow 上也有讨论到这个问题 http://stackoverflow.com/questions/5895128/attempted-to-access-an-unloaded-appdomain-when-using-system-directoryservices

在stackoverflow上的讨论中有了一种解决方案,再发生AppDomainUnloadedException 错误的时候,通过休眠一段时间重新调用这个接口:

代码语言:javascript
复制
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);     }} 

这样就会造成一个问题,如果发生了异常,接口就非常的慢了。这可以通过引入缓存机制来解决:

代码语言:javascript
复制
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[];}

上面的代码来自于Active Directory Roles Provider

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2011-08-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据保险箱
数据保险箱(Cloud Data Coffer Service,CDCS)为您提供更高安全系数的企业核心数据存储服务。您可以通过自定义过期天数的方法删除数据,避免误删带来的损害,还可以将数据跨地域存储,防止一些不可抗因素导致的数据丢失。数据保险箱支持通过控制台、API 等多样化方式快速简单接入,实现海量数据的存储管理。您可以使用数据保险箱对文件数据进行上传、下载,最终实现数据的安全存储和提取。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档