Com Interop error when using impersonation on ASP.NET

0

I am trying to write a ASP.NET page that looks at group membership for authorization of a site. I have code that is working when run in the local debugger and when locally logged into the web server itself. However, when i try to access the page from a remote web browser, i get an error.

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

I have impersonation turned on and I have it set to only Windows Authentication turned on. Is there something I am missing?

Here's the stack trace:

[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

It appears to be happening in the FindByIdentity portion of the GetGroupNames code i wrote.

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

Like i said, it works fine when run locally on my dev box or on the web server. It's only when accessing from a remote brower that i get the error.

c#
asp.net
com-interop
impersonation
asked on Stack Overflow Sep 10, 2013 by SpaceCowboy74 • edited Sep 11, 2013 by SpaceCowboy74

1 Answer

0

So using the code below worked for me.

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;
    }
}
answered on Stack Overflow Sep 12, 2013 by SpaceCowboy74

User contributions licensed under CC BY-SA 3.0