2013-10-11 136 views
3

我正在嘗試從本地Intranet上提交ASP.NET表單的用戶獲取電子郵件地址。當在我的本地機器上測試它時,它工作正常。但是,當我發佈並開始生產測試它,它不喜歡的線74System.DirectoryServices.DirectoryServicesCOMException:發生操作錯誤。

Server Error in '/' Application. 
-------------------------------------------------------------------------------- 

An operations error occurred. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred. 


Source Error: 


Line 71:   adSearcher.SearchScope = SearchScope.Subtree; 
Line 72:   adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))"; 
Line 73:   SearchResult userObject = adSearcher.FindOne(); 
Line 74:   if (userObject != null) 
Line 75:   { 

Source File: c:\Web\Support-t\Content\Default.aspx.cs Line: 73 

Stack Trace: 


[DirectoryServicesCOMException (0x80072020): An operations error occurred. 
] 
    System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +628309 
    System.DirectoryServices.DirectoryEntry.Bind() +44 
    System.DirectoryServices.DirectoryEntry.get_AdsObject() +42 
    System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +98 
    System.DirectoryServices.DirectorySearcher.FindOne() +44 
    _Default.Page_Load(Object sender, EventArgs e) in c:\Web\Support-t\Content\Default.aspx.cs:73 
    System.Web.UI.Control.LoadRecursive() +71 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178 



-------------------------------------------------------------------------------- 
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18033 

的代碼塊在我的網頁加載只是爲了測試目的所以可以得到一個直接結果....但它從來沒有到頁面的加載在生產,但能正常工作本地計算機上的VS調試時...

IIdentity id = WindowsIdentity.GetCurrent(); 
     WindowsIdentity winId = id as WindowsIdentity; 

     if (id == null) 
     { 
      txtDetailedProblem.Text = "Identity is not a windows identity"; 
      return; 
     } 

     string userInQuestion = winId.Name.Split('\\')[1]; 
     string myDomain = winId.Name.Split('\\')[0]; // this is the domain that the user is in 
     // the account that this program runs in should be authenticated in there      
     DirectoryEntry entry = new DirectoryEntry("LDAP://" + myDomain); 
     DirectorySearcher adSearcher = new DirectorySearcher(entry); 

     adSearcher.SearchScope = SearchScope.Subtree; 
     adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))"; 
     SearchResult userObject = adSearcher.FindOne(); 
     if (userObject != null) 
     { 
      string[] props = new string[] {"mail"}; 
      foreach (string prop in props) 
      { 
       txtTEST.Text = prop.ToString() + " " + userObject.Properties[prop][0].ToString(); 
      } 
     } 
+0

我假設你有權限訪問AD,其中對生產網絡服務器的身份不。您的生產Web服務器的應用程序池中配置了哪種身份? – rene

+0

可能重複[System.DirectoryServices.DirectoryServicesCOMException:發生操作錯誤](http://stackoverflow.com/questions/13688031/system-directoryservices-directoryservicescomexception-an-operations-error-occu) – rene

+0

它設置爲自定義 – KBriz

回答

3
IIdentity id = WindowsIdentity.GetCurrent(); 
      WindowsIdentity winId = id as WindowsIdentity; 

      if (id == null) 
      { 
       CurrentUserEmail = "identity is not a windows identity"; 
       return; 
      } 

      var name = winId.Name; 

      string userInQuestion = name.Split('\\')[1]; 
      string myDomain = name.Split('\\')[0]; // this is the domain that the user is in 
      // the account that this program runs in should be authenticated in there      

      using (HostingEnvironment.Impersonate()) 
      { 
       DirectoryEntry entry = new DirectoryEntry("LDAP://" + myDomain); 
       DirectorySearcher adSearcher = new DirectorySearcher(entry); 

       adSearcher.SearchScope = SearchScope.Subtree; 
       adSearcher.Filter = "(&(objectClass=user)(samaccountname=" + userInQuestion + "))"; 
       SearchResult userObject = adSearcher.FindOne(); 
       if (userObject != null) 
       { 
        string[] props = new string[] {"mail"}; 
        foreach (string prop in props) 
        { //when it works set variable to CurrentUserEmail instead of txtDetailPrblem textbox 
         CurrentUserEmail = userObject.Properties[prop][0].ToString(); 
        } 
       } 

      } 
相關問題