1

Though the web is elevated, I get the following exception

'elevatedWeb.AllowAnonymousAccess' threw an exception of type 'System.UnauthorizedAccessException'

when I try to access the elevated web properties. Here is the code:

       string urlmp = ConfigurationManager.AppSettings["urlmp"];
            SPSite spsite = new SPSite(urlmp);

            SPSecurity.RunWithElevatedPrivileges(delegate()
                    {


                        SPSite elevatedSite = new SPSite(spsite.ID);

                        SPWeb elevatedWeb = elevatedSite.OpenWeb();
                        ht = elevatedWeb.AllProperties; // HERE the exception



                    });

Any idea about what's wrong?

EDIT: The code runs in a membership provider.

  • Where do you run this code? Console app? Web part?... Also, why do you first open "normal" spsite and spweb objects? You actually don't need them here. – Evariste Feb 2 '16 at 15:53
  • I've removed the web (just a rudiment). I used the first spsite to get the ID. I've edited for more info – Gyonder Feb 2 '16 at 15:58
  • No need to even get the first spsite: you can simply open your elevatedSite from the URL: SPSite elevatedSite = new SPSite(urlmp);. Opening one SPSite instead of two will save some CPU/RAM/Network... – Evariste Feb 2 '16 at 16:00
  • It may happen your membership provider is not loaded by the application pool of your SharePoint Web app. In that case, the authentication provider app pool's identity may not have access to SharePoint DB. How do you test your provider? – Evariste Feb 2 '16 at 16:07
  • 1
    That's my point: the security token service may run under a different identity that cannot access the site. – Evariste Feb 2 '16 at 16:50
1

Your membership provider may be loaded by the SharePoint Web application itself, running in the SharePoint pool's identity. This identity has obviously full access to all site collections in the Web app.
But the membership provider can also be loaded in the Security Token service, a dedicated Web application in SharePoint, loaded in a dedicated app pool. That app pool may (should) have another service's identity.
The SPSecurity.RunWithElevatedPrivileges only reverts context from current user to app pool's identity. It works great when used in code running in the SharePoint application pool (that's the way it's expected to be used).
If you "RunWithElevatedPrivileges" from another pool, it reverts to that other pool's identity. And it has then no effect on the privileges gained to access the SharePoint DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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