SqlRoleProvider: NullReferenceException thrown when calling Roles.GetRolesForUser

Scenario: WCF service using SqlRoleProvider to authenticate with the Sql Server 2012 database server. WCF is hosted on the IIS7 web server.

see this error:

System.NullReferenceException: An object reference is not set to an instance of the object.
in System.Web.Security.Roles.GetRolesForUser (username String)

Included RoleManagement.

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

On another server (test environment) it does not work. I can log in (the user is authenticated with the user and passes the sql server to the server), but when I try to get roles for this user, I get a nullreference exception.

How is this possible, can anyone lead to where this problem might arise?

Sincerely.

+7
authentication sql-server-2012 iis-7 wcf
source share
2 answers

blergh

A google search with StackOverflow tags, provided I stumbled upon entering the code here on this site: http://www.lhotka.net/weblog/CallingRolesGetRolesForUserInAWCFService.aspx

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

To solve this problem, call:

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

instead

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

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

+14
source share

This issue sounds like an asp.net error.

connect.microsoft.com

The workaround is to set the tracking level of your web server. For example, adding the following parameters to the web.config file will solve the problem,

 <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> 
+1
source share

All Articles