【问题标题】:COMException (0x80005000): Unknown error - UserPrincipal.set_GivenName(String value)COMException (0x80005000): 未知错误 - UserPrincipal.set_GivenName(String value)
【发布时间】:2014-09-03 15:21:51
【问题描述】:

我有以下在 ASP.NET 应用程序中调用的代码:

public DomainUserInfo GetDomainUserInfoByName(string domain, string firstName, string lastName)
{
    string[] domainArray = domain.Split(',');
    foreach (string d in domainArray)
    {
        var principalContext = new PrincipalContext(ContextType.Domain, d);
        var userPrincipal = new UserPrincipal(principalContext) {GivenName = firstName, Surname = lastName};
        using (var searcher = new PrincipalSearcher(userPrincipal))
        {
            userPrincipal = (UserPrincipal) searcher.FindOne();
        }

        if (userPrincipal != null)
        {

            var domainUserInfo = new DomainUserInfo
            {
                FirstName = userPrincipal.GivenName,
                LastName = userPrincipal.Surname,
                Email = userPrincipal.EmailAddress,
                LanID = userPrincipal.SamAccountName,
                Extension = userPrincipal.VoiceTelephoneNumber,
                DomainName = d,
                NTAccountName = userPrincipal.Sid.Translate(typeof (NTAccount)).ToString()
            };

            return domainUserInfo;
        }
    }
    return null;
}

在某些服务器上部署时它可以工作,但在其他服务器上却不能,它会引发异常:

[COMException (0x80005000): Unknown error (0x80005000)]
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +386081
   System.DirectoryServices.DirectoryEntry.Bind() +36
   System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
   System.DirectoryServices.PropertyValueCollection.PopulateList() +21
   System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +49
   System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +135
   System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer() +288
   System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +37
   System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +118
   System.DirectoryServices.AccountManagement.PrincipalContext.ContextForType(Type t) +34
   System.DirectoryServices.AccountManagement.Principal.GetStoreCtxToUse() +37
   System.DirectoryServices.AccountManagement.UserPrincipal.set_GivenName(String value) +17
   Mfc.Inv.RM.Framework.ActiveDirectory.ActiveDirectoryManager.GetDomainUserInfoByName(String domain, String firstName, String lastName) +167

看起来在线上正在发生这种情况:

var userPrincipal = new UserPrincipal(principalContext) {GivenName = firstName, Surname = lastName};

尝试设置UserPrincipal 对象的GivenName 属性时。

我完全不知道是什么原因导致了这种情况,尤其是因为它适用于某些服务器而不适用于其他服务器。我已经尝试编写一个控制台应用程序,它调用它在所有服务器上运行的相同代码,所以我猜它必须与 IIS 有关。

【问题讨论】:

  • 您熟悉 PrincipalCONtext..吗?如果是这样,为什么您需要执行以下操作 var userPrincipal = new UserPrincipal(principalContext) {GivenName = firstName, Surname = lastName}; 您应该能够仅使用 PrincipalContext 获得所有必要的信息,它可能无法在其他服务器上运行的原因是因为其他服务器可能不在同一个域中或广告树。也与您的管理员联系
  • @DJKRAZE 我没有写代码。此外,PrincipalContext 没有 GivenName 或 Surname 或 EmailAddress 的属性(我们使用不同的方法进行搜索)
  • funny.. 因为我现在正在查看一些代码,我可以看到.. 让我澄清一下你不需要使用新的 UserPrincipal 我会告诉你我在做什么以及我在做什么意思是,它工作得很好..
  • 在 PrincipalContext 类上?我看到的只是名称和用户名
  • @DJKRAZE 另外,刚刚验证所有服务器都是同一个(正确)域的成员。

标签: c# iis active-directory


【解决方案1】:

这就是我正在做的事情,如果您将鼠标悬停在 userFind 上或对其进行快速观察,您将看到以下信息。还要注意我传递的 IdentityType.SamAccountName

var pc = new PrincipalContext(ContextType.Domain, domainName, null, null);
var userFind = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, username);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 2010-12-15
    • 2019-02-18
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    相关资源
    最近更新 更多