0

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

Regards.

Ali Jafer
  • 17,808
  • 1
  • 27
  • 41
buddy
  • 1
  • 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 08:30

1 Answers1

1

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

Try the code below instead:

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

    } 
});

for further info look here:

Microsoft.SharePoint.SPSecurity

it contains a method called RunWithElevatedPrivileges

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

Ali Jafer
  • 17,808
  • 1
  • 27
  • 41