7

Scenario: WCF Service using SqlRoleProvider for authentication with a Sql Server 2012 Database server. WCF is hosted on a IIS7 webserver.

please see this error:

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.Security.Roles.GetRolesForUser(String username)

RoleManagement is enabled.

On my local development machine (server 2012, iis7) this works fine. When I log in and call the method the roles are retrieved.

On the other server (test environment) it's not working. I can login (user is authenticated with user and pass against sql server database) but when I try and retrieve the roles for that user, I get a nullreferenceexception.

How is this possible, does anyone have any leads on to where this problem might occur?

Best regards.

Obelix
  • 708
  • 11
  • 24

2 Answers2

14

blergh

Googling with the tags Stack Overflow provided I came across this site: http://www.lhotka.net/weblog/CallingRolesGetRolesForUserInAWCFService.aspx

In short: apparently something broke between .net 3.5 and .net 4.

To solve this issue call:

string[] roles = Roles.Provider.GetRolesForUser(ServiceSecurityContext.Current.PrimaryIdentity.Name);

instead of

string[] roles = Roles.GetRolesForUser(ServiceSecurityContext.Current.PrimaryIdentity.Name);

The difference is in the .Provider which is added in the middle. After adding this it worked fine.

Pang
  • 9,564
  • 146
  • 81
  • 122
Obelix
  • 708
  • 11
  • 24
  • 1
    This works for me now, but I had to adapt also all occurrences of the calls to "IsUserInRole()", too. Strangely, the fix is only needed when the solution is started with VS2013. When started on the exact same machine, with VS2010, the calls work without the modification. FYI, I have a .NET 3.5 WCF project, if that matters. – Marcel Nov 03 '14 at 07:14
  • I had the same exact issue. I added following lines in Web.config to user Roles.GetRolesForUser otherwise you have to user Roles.Provider.GetRolesForUser. I am using MVC5 with OWIN middleware enabled. – user357086 Aug 15 '15 at 04:40
1

This issue sounds like this asp.net bug.

connect.microsoft.com

A workaround is to adjust your web server tracing level. For example, adding the follow settings in your web.config file would resolve the issue,

<system.webServer>
    <tracing>
     <traceFailedRequests>
        <remove path="*"/>
        <add path="*">
         <traceAreas>
            <add provider="ASP" verbosity="Verbose" />
            <!-- Note that the verbosity is set to Warning (default value is Verbose)-->
            <add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Warning" />
            <add provider="ISAPI Extension" verbosity="Verbose" />
            <add provider="WWW Server" areas="Authentication,Security,Filter,StaticFile,CGI,Compression,Cache,RequestNotifications,Module,Rewrite,WebSocket" verbosity="Verbose" />
         </traceAreas>
         <failureDefinitions statusCodes="200-999" />
        </add>
     </traceFailedRequests>
    </tracing>
</system.webServer>
Simon
  • 548
  • 7
  • 16