【发布时间】:2011-09-30 03:28:24
【问题描述】:
我在 MVC Web 应用程序中使用 .Net 4 上的 c# 时遇到问题,当我查询 Active Directory 时,我经常收到错误:尝试访问未加载的应用程序域。 (HRESULT 的例外情况:0x80131014)。
奇怪的是,它会在一段时间内完美无缺地工作,然后它会开始发生,然后又消失了。
我对该函数进行了一些修改以使其正常工作,但它们似乎都失败了。我想知道我是否做错了什么,或者是否有更好的方法。
这是我当前的函数,它将接受一个 loginId 和一个 PrincipalContext。 loginId 可以是用户 DisplayName ,即“John Smith”,也可以是 DOMAINNAME\josmi。默认是使用他们名字的前 2 个字母,然后是他们姓氏的前 3 个字母。如果不是这种情况,那里有一个检查。这部分如果没问题。
public List<ADGroup> GetMemberGroups(string loginId, PrincipalContext principalContext, int tries = 0)
{
var result = new List<ADGroup>();
try
{
var samAccountName = "";
if (loginId.Contains(" "))
{
var fName = loginId.Split(Char.Parse(" "))[0].ToLower();
var sName = loginId.Split(Char.Parse(" "))[1].ToLower();
if (sName.Trim().Length == 2)
samAccountName = string.Format("{0}{1}", fName.StartsWith(".") ? fName.Substring(0, 4) : fName.Substring(0, 3), sName.Substring(0, 2));
else
samAccountName = string.Format("{0}{1}", fName.StartsWith(".") ? fName.Substring(0, 3) : fName.Substring(0, 2), sName.Substring(0, 3));
}
else
samAccountName = loginId.Substring(loginId.IndexOf(@"\") + 1);
var authPrincipal = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, samAccountName);
if (authPrincipal == null)
throw new Exception(string.Format("authPrincipal is null for loginId - {0}", loginId));
var firstLevelGroups = authPrincipal.GetGroups();
AddGroups(firstLevelGroups, ref result);
}
catch
{
if (tries > 5)
throw;
tries += 1;
System.Threading.Thread.Sleep(1000);
GetMemberGroups(loginId, principalContext, tries);
}
return result;
}
private void AddGroups(PrincipalSearchResult<Principal> principal, ref List<ADGroup> returnList)
{
foreach (var item in principal)
{
if (item.GetGroups().Count() > 0)
AddGroups(item.GetGroups(), ref returnList);
returnList.Add(new ADGroup(item.SamAccountName, item.Sid.Value));
}
}
这个函数是这样调用的:
MembershipGroups = ad.GetMemberGroups(user.SamAccountName, new PrincipalContext(ContextType.Domain));
我有时得到的错误是:
System.AppDomainUnloadedException: 试图访问一个已卸载的 应用程序域。 (HRESULT 的例外情况: 0x80131014) 在 System.StubHelpers.StubHelpers.InternalGetCOMHRExceptionObject(Int32 hr,IntPtr pCPCMD,对象 pThis) 在 System.StubHelpers.StubHelpers.GetCOMHRExceptionObject(Int32 hr,IntPtr pCPCMD,对象 pThis) 在 System.DirectoryServices.AccountManagement.UnsafeNativeMethods.IADsPathname.Retrieve(Int32 lnFormatType) 在 System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo() 在 System.DirectoryServices.AccountManagement.ADStoreCtx.get_UserSuppliedServerName() 在 System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet.BuildPathFromDN(字符串 dn) 在 System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet.MoveNextPrimaryGroupDN() 在 System.DirectoryServices.AccountManagement.ADDNLinkedAttrSet.MoveNext() 在 System.DirectoryServices.AccountManagement.FindResultEnumerator
1.MoveNext() at System.DirectoryServices.AccountManagement.FindResultEnumerator1.System.Collections.IEnumerator.MoveNext()
【问题讨论】:
-
我在拨打
S.DS.AM时遇到了与随机AppDomainUnloadedExceptions 相同的问题,但找不到解决方案。希望附加赏金会引起一些关注... -
至少对我有用的一个技巧是当您初始化 PrincipalContext 时,在构造函数中传递域名。 PrincipalContext domain = new PrincipalContext(ContextType.Domain,"AD");
-
@Reza 的评论似乎对我有用。我遇到了同样的错误,并且在域中传递似乎已经成功了。
标签: c# asp.net-mvc model-view-controller active-directory