簡體   English   中英

在ASP.NET上使用模擬時出現Com Interop錯誤

[英]Com Interop error when using impersonation on ASP.NET

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

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服務器上本地運行時,它運行良好。 只有從遠程瀏覽器訪問時,我才收到錯誤。

因此,使用下面的代碼對我有用。

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM