ASP.NET 5 MVC 6中没有实体框架的身份验证和授权 [英] Authentication and Authorization without Entity Framework in ASP.NET 5 MVC 6

查看:194
本文介绍了ASP.NET 5 MVC 6中没有实体框架的身份验证和授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用现有数据库和表配置身份验证和授权,而不使用Entity Framework(使用Dapper).

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

我已经正确配置了Dapper,现在我试图连接SignInManager和UserManager以通过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]'

现在,这是我配置自定义用户,角色,用户存储,角色存储,usermanager和rolemanager的方式:

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)
    {
    }
}

这是我的startup.cs:

And here's my startup.cs:

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

我在这里想念什么?该错误说明了有关RoleManager的信息,我已经定义了自定义RoleManager ...

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

推荐答案

我看到的一个问题是您的WrestleStatRole继承自

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.

您将需要自己的角色类,并且不应使用EF实施中的任何类.

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

类似地,您在WrestleStatUser中继承的ApplicationUser在Web应用程序项目models文件夹中,但请确保它不继承自

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

要不使用实体框架,必须实现IUserStore和IRoleStore并向di服务注册它们.

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>>();

并且如上所述,您的用户和角色类不应继承自EF实现,实际上,只要您实现了这些存储并且它们可以工作,它们就根本不需要继承任何东西.

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.

如果实现用户存储和角色存储,则可以使用内置的UserManager,除非您有其他原因,否则不需要自己实现.

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.

如果您需要示例代码,可以查看我的 cloudscribe项目我已经实现了自定义的多租户身份不使用实体框架的实现.实际上,我正在支持可以插入的多个数据层,而EF是其中之一,但它与身份位无关,我根本不使用Microsoft.AspNetCore.Identity.EntityFrameworkCore命名空间中的任何内容.

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.

这篇关于ASP.NET 5 MVC 6中没有实体框架的身份验证和授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆