26013

SqlRoleProvider on IIS8 Express

Question:

My web application (a WCF service) uses SqlRoleProvider, which works fine on <strong>Visual Studio Development Server</strong>. Switching it to <strong>IIS8 Express</strong> causes it to throw a NullReferenceException though:

Roles.IsUserInRole(username, role) // neither of them actually null

I could not find a hint for this exception in <a href="http://msdn.microsoft.com/en-us/library/system.web.security.sqlroleprovider.isuserinrole.aspx" rel="nofollow">the IsUserInRole method documentation</a>. Switching back to Visual Studio Development server makes it work. What is the cause of this exception, and how could I fix it properly? The project's <em>target framework</em> is <em>.NET Framework 4</em>.

Here is the configured connection string:

<add name="ConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=DevWeb;Integrated Security=True" providerName="System.Data.SqlClient" />

And this is the roleManager/providersnode:

<clear /> <add connectionStringName="ConnectionString" applicationName="MyApp" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/>

Answer1:

What Vladimir said is true but doesn't actually explain what is happening. What throws the NullReferenceException is the EtwTrace code that's in IsUserInRole and GetRolesForUser. Everything else in the Roles class accounts for the fact that HttpContext.Current can be null. I found this by looking in the Microsoft Reference Source for "NET, Version 4.5" at <a href="http://referencesource.microsoft.com/netframework.aspx" rel="nofollow">http://referencesource.microsoft.com/netframework.aspx</a>

In every other test environment I tried, the tracing level was not sufficient to trigger the NullReferenceException. It was only when I used IIS Express 8 after installing Visual Studio 2013 that I saw the issue and only in IIS Express.

What can you do about it?

One option is to enable "ASP.Net Compatibility Mode" for WCF. First, in the web.config, add the attribute aspNetCompatibilityEnabled="true" to the node <configuration><system.serviceModel><serviceHostingEnvironment>. Then opt your service class into the behavior by adding the attribute [System.ServiceModel.Activation.AspNetCompatibilityRequirements(RequirementsMode = System.ServiceModel.Activation.AspNetCompatibilityRequirementsMode.Allowed)] to the class definition.With this functionality enabled, HttpContext.Current will be populated by the time you go to request roles from the Roles class, assuming you're not working on a background thread like Vladimir mentioned (in which case you'll need to patch up the HttpContext.Current first). You can read more about ASP.NET Compatibility Mode for WCF at <a href="http://blogs.msdn.com/b/wenlong/archive/2006/01/23/516041.aspx" rel="nofollow">http://blogs.msdn.com/b/wenlong/archive/2006/01/23/516041.aspx</a>.

The other option is to bypass the Roles class for these two methods. Instead of calling Roles.IsUserInRole, call Roles.Provider.IsUserInRole. Instead of calling Roles.GetRolesForUser, call Roles.Provider.GetRolesForUser. Each method has the same overloads available. You lose the trace stops and the local role cache but you bypass the null reference exception.

Answer2:

If you calling Roles.IsUserInRole in different Thread then you should check HttpContext. In this case will be empty. So, for fix you should copy HttpContext from main thread to child thread.

var context = HttpContext.Current; Thread thread = new Thread(delegate() { if (HttpContext.Current == null) HttpContext.Current = context; }); thread.Start();

Answer3:

Fairly sure your issue is going to be with the database and the permission of the account accessing it. Try adding your IIS account to the sa priv and see if the matter resolves. If it does then you have a missing permission.

Recommend

  • Turning off only the Role Provider in SimpleMembership
  • IsUserInRole Error with SimpleMembership
  • Custom Membership Provider won't redirect
  • Asp.Net Identity - IdentityDbContext causing problems for fields added to AspNetRoles table
  • how to secure a single aspx page in asp.net website
  • How to get a return value from a stored procedure in VB.NET
  • Local SQL Server installed by other user account, how to access from new user account
  • How to choose from two connection strings?
  • The request sent by the client was syntactically incorrect
  • System.Data.SqlClient Namespace for MySQL?
  • Qwindows.dll Never Found In
  • Accessing the property of a model in ASP.NET MVC View
  • Bootstrap error iOS 5.0 simulator in XCode 4.2
  • Flutter Splash Screen
  • Server.Transfer from ASP to ASP.Net
  • Scala - trait member initialization: use traits to modify class member
  • WiX - How do I get different registry keys for allusers and single user
  • How can I get the parent of the current file?
  • Angular/Ionic 2 - what is a provider and what does `static get parameters()` do?
  • How to communicate between ASPX and WinForms
  • ActionBar with appcompat library v7 (ava.lang.IllegalStateException: You need to use a Theme.AppComp
  • angularjs - ng-show doesn't update class when $interval triggers
  • docker-compose: connection refused between containers, but service accessible from host
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • Android Database Error - getWriteableDatabase
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Assign variable to the value in HTML
  • How to use carriage return with multiple line?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?