SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to open site(FBA enabled with custommembership & customroleprovider) through module/httpmodule. It is throwing error

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.SPSecurity.GetRolesForUser(String roleManagerName, String userName, UInt32& roleCount, String& roles)

Here is the code:

HttpRequest r = HttpContext.Current.Request;
using (SPSite s = new SPSite(r.Url.AbsoluteUri))
                    { ......}

Please provide me if there is any solution


share|improve this question
I dont know your exact usage, but is it possible to run your code under admin privilege like SPSecurity.RunWithElevatedPrivileges ? – Diptarag Oct 10 '12 at 8:30

Diptarag is correct if the user doesnt have access then it will throw that error:

Try the code below instead:

    HttpRequest r = HttpContext.Current.Request; 
    using (SPSite s = new SPSite(r.Url.AbsoluteUri)) 


for further info look here:


it contains a method called RunWithElevatedPrivileges

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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