0

I'm developing full trust web part deployed in GAC that access soap web service on localhost. Web service uses windows authentication. SP 2010 web part impersonates current user while SP 2013 sends app pool identity?

On both SP's NTML authentication is configured.

This is web.config portion for 2010

<authentication mode="Windows">
    <identity impersonate="true">
    <authorization>
       <allow users="*">
    </authorization>

And in 2013 I have this:

<authentication mode="Forms">
    <forms loginUrl="/_login/default.aspx">
</authentication>
<identity impersonate="true">
    <authorization>
         <allow users="*">
    </authorization>
    <add key="aspnet:AllowAnonymousImpersonation" value="false">

It does't help if I change it to windows auth like in 2010?

Impersonating spuser doesn't help olso:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(siteid, SPContext.Current.Web.CurrentUser.UserToken))
    {
        using (SPWeb web = site.OpenWeb(webid))
        {
            SOAPService ws = new SOAPService() { UseDefaultCredentials = true };
            var res = ws.BugGetAllBugs();

            GridView1.DataSource = res;
            GridView1.DataBind(); 
        }
    }
});

2 Answers 2

2

The difference is in "authentication mode" settings.

Windows authentication provides real windows user and then this would allow us to make resource requests from ASP.NET while impersonating the Windows identity of whoever was logged on.

With forms authentication ASP.NET itself makes requests for system resources using the Application Pool account. Look http://www.appetere.com/Blogs/SteveM/October-2011/Identity-impersonation-with-IIS-and-ASP-NET for details.

The RunWithElevatedPrivileges runs the code inside in a new thread. This new thread runs under the account of the current application pool. The usage of new SPSite with user token opens only the SPSite in the context of the defined user and does not start new thread. You can track the current user by calling WindowsIdentity.Current

7
  • When I look at Authentication providers form my app in SP Central Administration there Windows Auht, NTLM is set. Also I'm pretty sure forms auth is not used since I do not need to enter my credentials to log in.
    – malibeg
    Oct 2, 2014 at 6:42
  • 1
    Is your Sharepoint 2010 appcitaion in classic mode authentication? Oct 2, 2014 at 6:49
  • My SP2010 application also uses Windows auth/NTLM
    – malibeg
    Oct 2, 2014 at 6:55
  • 1
    Sorry, not the authentication type, but the core identity type using in Web Application. Check this: blogs.msdn.com/b/psssql/archive/2011/02/22/… Oct 2, 2014 at 6:58
  • 1
    Yes, I think it is a solution. The othe way that can work - using the ServiceSecurityContext.Current.WindowsIdentity.Impersonate() method. Impersonation in a net.tcp WCF-service in a claims based/Kerberos solution Oct 2, 2014 at 7:26
0

At the end I used this code to impersonate current user to local web service:

try
{
    Guid siteid = SPContext.Current.Site.ID;
    Guid webid = SPContext.Current.Web.ID;

    using (SPSecurityContext.GetWindowsIdentity().Impersonate())
    {
        Label1.Text = WindowsIdentity.GetCurrent().Name;
        SOAPService ws = new SOAPService() { UseDefaultCredentials = true };
        var res = ws.GetAllBugs();

        GridView1.DataSource = res;
        GridView1.DataBind(); 
    }
}
catch (Exception ex)
{

    Response.Write("Error received: " + ex.Message);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.