繁体   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