yozepi yozepi - 3 months ago 16
ASP.NET (C#) Question

ASP.Net Identity 2.0: User is a System.Web.Security.RolePrincipal, Why?

I'm trying to implement Asp.Net Identity 2.0 (OWIN) in an existing application and I'm having all sorts of trouble when it comes to roles. I created a sample project from the project template and (as far as I can tell) I've copied everything from there into my application. I modified the connection information so the authentication tables come from my own Sql database instead of the default local DB.

Everything seems to work great. The tables are initialized (created) upon start-up and I can create a new user, assign a role to that user, and log in as that user. But when I attempt to check if the user is in a particular role I get an exception indicating a problem locating the local database. Well that would be because I'm not using the local DB. So why would my application be looking for roles in the (non-existent) local DB?

To rule out weirdness in my Sql instance, I changed the connection data of the sample app so that it points my DB and ran it. I can log in using the user I created in my application and can even poll the user for the role in question successfully. I confirmed this by examining the tables directly and verified the user, role, and user-role association were all there.

Here's what I did notice though. When I run the sample app the user is an instance of

System.Security.Claims.ClaimsPrincipal
. But when I run my app the user is an instance of
System.Web.Security.RolePrincipal
.

So, what did I miss? Why is my app returning a
RolePrincipal
instead of
ClaimsPrincipal
? Where could I look for clues?

I'm pulling my hair out on this and I don't have much left! Any help would be greatly appreciated.

Answer

What is happening is your old application is still hooking up to the old membership code. A few checklist items should bring you back...

Make sure FormsAuthenticationModule is removed (Since MVC5 no longer uses it)

<modules>
  <remove name="FormsAuthenticationModule" />
</modules>

Make sure SimpleMembership is turned off (or alternatively just delete it)

<add key="enableSimpleMembership" value="false"/>

And the most important part is to delete references to WebMatrix (no longer used in MVC 5). WebMatrix will automatically register pre-application startup methods that will "attempt" to provide membership services to your project. See here for details