在 ASP.NET 上使用模拟时出现 COM 互操作错误

本文关键字:COM 互操作 错误 模拟 NET ASP | 更新日期: 2023-09-27 18:30:47

我正在尝试编写一个 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 服务器上本地运行时,它可以正常工作。 只有从远程浏览器访问时,我才收到错误。

在 ASP.NET 上使用模拟时出现 COM 互操作错误

所以使用下面的代码对我有用。

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