2014-09-03 75 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);