Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication and Authorization without Entity Framework in ASP.NET 5 MVC 6

I'm trying to configure my authentication and authorization using my existing database and tables, without using Entity Framework (using Dapper).

I've got the Dapper configured correctly, now I'm trying to hook up the SignInManager, and UserManager to call my database via Dapper, but before that can happen, I'm running into some errors with my custom role store.

Here's the error that I'm receiving when I click the "Register" button on the website (this is just a plain project with all of the pre-defined account etc stuff out of the box)

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNet.Identity.IRoleStore`1[TestAsyncWebsite.Configuration.WrestleStatRole]' while attempting to activate 'Microsoft.AspNet.Identity.RoleManager`1[TestAsyncWebsite.Configuration.WrestleStatRole]'

For now, here's how I have configured my custom user, role, userstore, role store, usermanager, and rolemanager:

    public class WrestleStatUser : ApplicationUser
    {
        public WrestleStatUser() : base()
        {

        }
    }

    public class WrestleStatRole : IdentityRole
    {

    }

public class WrestleStatUserStore : IUserStore<WrestleStatUser>
{
   // all methods implemented
}

public class WrestleStatRoleStore : IRoleStore<WrestleStatRole>
{
   // all methods implemented
}

    public class WrestleStatUserManager : UserManager<WrestleStatUser>
    {
        public WrestleStatUserManager(IUserStore<WrestleStatUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<WrestleStatUser> passwordHasher, IEnumerable<IUserValidator<WrestleStatUser>> userValidators, IEnumerable<IPasswordValidator<WrestleStatUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IEnumerable<IUserTokenProvider<WrestleStatUser>> tokenProviders, ILogger<UserManager<WrestleStatUser>> logger, IHttpContextAccessor contextAccessor)
            : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, tokenProviders, logger, contextAccessor)
        {
        }
    }

public class WrestleStatRoleManager : RoleManager<WrestleStatRole>
{
    public WrestleStatRoleManager(IRoleStore<WrestleStatRole> store, IEnumerable<IRoleValidator<WrestleStatRole>> roleValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, ILogger<RoleManager<WrestleStatRole>> logger, IHttpContextAccessor contextAccessor) : base(store, roleValidators, keyNormalizer, errors, logger, contextAccessor)
    {
    }
}

And here's my startup.cs:

    services.AddIdentity<WrestleStatUser, WrestleStatRole>()
        .AddUserStore<WrestleStatUserStore>()
        .AddUserManager<WrestleStatUserManager>()
        //.AddRoleStore<RoleStore>()
        .AddRoleManager<WrestleStatRoleManager>()
        .AddDefaultTokenProviders();

What am I missing here? The error says something about the RoleManager, I've already defined my custom RoleManager...

like image 331
ganders Avatar asked Mar 23 '16 17:03

ganders


People also ask

What is authentication and authorization in MVC?

Authentication and Authorization in MVC is the process of validating the user as well as checking the rights to access a particular resource.

What is ASP NET identity in MVC?

Asp.net Identity is the new way of authentication for Asp.net projects which is provided by Microsoft. It is a common authentication mechanism used by all the Microsoft framework such as Web forms, MVC, Web API etc. It also provide the functionality for user and role management in the system.

What is authentication Action Filter in MVC 6?

Just like MVC 5, we have an Authentication Action Filter in MVC 6. The following diagram gives an idea of Authentication when the end-user makes a call to an MVC 6 application. When the end-user makes a call to an MVC 6 application requesting a View, a response in the form of a View is returned when the action is executed.

How to create an aspnet5_auth application in Visual Studio 2015?

Step 1: Open Visual Studio 2015 and create a new ASP.NET application of name ASPNET5_Auth as shown in the following Image Click on OK and from the next window select ASP.NET Empty application from ASP.NET 5 templates as shown in the following image


1 Answers

one problem I see is your WrestleStatRole inherits from IdentityRole which may sound like part of Identity but its really part of EntityFramework Identity implementation, if you are trying to do things without EF you should not inherit from that.

You would need your own role class and should not use any classes from EF implementation.

Similarly the ApplicationUser that you inherit from in WrestleStatUser is in the web app project models folder, but make sure it doesn't inherit from IdentityUser which is part of the EntityFramework implementation of identity

To not use Entity Framework you must implement IUserStore and IRoleStore and register those with di services

services.AddScoped<IUserStore<WrestleStatUser>, UserStore<WrestleStatUser>>();
services.AddScoped<IRoleStore<WrestleStatRole>, RoleStore<WrestleStatRole>>();

and as mentioned your user and role classes should not inherit from EF implementations, in fact they do not need to inherit from anything at all as long as you have implemented those stores and they work.

You can use the built in UserManager if you implement the userstore and rolestore, it is not required to implement that yourself unless you have other reasons for doing so.

If you need example code you can look at my cloudscribe project I have implemented a custom multi tenant identity implementation that does not use entity framework. Actually I'm supporting mutlple data layers that can be plugged in and EF is one of them but it is abtsracted away from identity bits and I'm not using anything from the Microsoft.AspNetCore.Identity.EntityFrameworkCore namespace at all.

like image 186
Joe Audette Avatar answered Sep 30 '22 09:09

Joe Audette