2014-09-03 97 views
0

我有下面的代码被称为ASP.NET应用程序的内部:收到COMException(0x80005000):未知的错误 - UserPrincipal.set_GivenName(字符串值)

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有关。

+0

您是否熟悉PrincipalCOntext ..?如果是的话,为什么你需要做下面的'var userPrincipal = new UserPrincipal(principalContext){GivenName = firstName,Surname = lastName};'你应该能够获得所有必要的信息,只需使用PrincipalContext也是它不可能的原因在其他服务器上工作是因为其他服务器可能不在同一个域或AD树中。获得您的管理员以及 – MethodMan 2014-09-03 15:36:07

+0

@DJKRAZE我没有写代码。另外,PrincipalContext没有GivenName或Surname的属性,或EmailAddress(我们用它来搜索不同的方法) – mclaassen 2014-09-03 15:38:48

+0

有趣..因为我正在看一些代码,现在我可以看到..让我澄清你不需要使用新的UserPrincipal我会告诉你我在做什么以及我的意思,它的工作原理非常好。 – MethodMan 2014-09-03 15:40:47

回答

0

这里是我正在做的事情,如果你将鼠标悬停在userFind上,或者做一个QuickWatch,你会看到以下信息。还注意到我正在通过的IdentityType.SamAccountName

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