2013-09-10 41 views
0

我正在嘗試編寫一個ASP.NET頁面,該頁面查看用於授權站點的組成員身份。我有在本地調試器中運行時以及在本地登錄到Web服務器本身時工作的代碼。但是,當我嘗試從遠程Web瀏覽器訪問該頁面時,出現錯誤。在ASP.NET上使用模擬時出現Com Interop錯誤

System.Runtime.InteropServices.COMException: An operations error occurred. 

我有模擬打開,我把它設置爲只打開Windows身份驗證。有什麼我失蹤?

這裏的堆棧跟蹤:

[COMException (0x80072020): An operations error occurred.] 
System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +387793 
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() +1124 
System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() +37 
System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +118 
System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +31 
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +14 
System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue) +73 
System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +25 
WebConfigValue.GroupForms.GroupSearchForm1.GetGroupNames(String userName) in C:\dev\code\Projects\Web\WebConfigValue\WebConfigValue\default.aspx.cs:224 
WebConfigValue.GroupForms.GroupSearchForm1.Page_Load(Object sender, EventArgs e) in C:\dev\code\Projects\Web\WebConfigValue\WebConfigValue\default.aspx.cs:45 
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 
System.Web.UI.Control.OnLoad(EventArgs e) +92 
System.Web.UI.Control.LoadRecursive() +54 
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772 

它出現在的GetGroupNames代碼我寫的FindByIdentity部分發生。

public List<string> GetGroupNames(string userName) 
    { 
     var result = new List<string>(); 
     using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "NPC")) 
     { 
      using (PrincipalSearchResult<Principal> src = UserPrincipal.FindByIdentity(pc, userName).GetGroups(pc)) 
      { 
       src.ToList().ForEach(sr => result.Add(sr.SamAccountName)); 
      } 
     } 
     return result; 
    } 

就像我說的,它在我的開發箱或Web服務器上本地運行時工作正常。只有從遠程瀏覽器訪問時,我纔會收到錯誤信息。

+0

是否存在內部異常?或更多的錯誤?也許你應該發佈你的整個堆棧跟蹤,以更好地瞭解發生了什麼。如果您發佈了給出錯誤的代碼,它也會有所幫助。 – paqogomez

回答

0

因此,使用下面的代碼爲我工作。

using System.Web.Hosting; 

public List<string> GetGroupNames(string userName) 
{ 
    var result = new List<string>(); 
    using (HostingEnvironment.Impersonate()) 
    { 
     using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "NPC")) 
     { 
      using (PrincipalSearchResult<Principal> src = UserPrincipal.FindByIdentity(pc, userName).GetGroups(pc)) 
      { 
       src.ToList().ForEach(sr => result.Add(sr.SamAccountName)); 
      } 
     } 
     return result; 
    } 
} 
相關問題