简体   繁体   English

在ASP.NET 5 Identity 3.0(无EF)中创建自定义角色处理

[英]Create custom role handling in ASP.NET 5 Identity 3.0 (without EF)

I have successfully created a simple MVC 6 application which uses my own ApplicationUser, an ApplicationUserStore (implementing IUserStore and IUserPasswordStore) and ApplicationUserManager (extending UserManager). 我已经成功创建了一个简单的MVC 6应用程序,该应用程序使用了我自己的ApplicationUser,一个ApplicationUserStore(实现IUserStore和IUserPasswordStore)和ApplicationUserManager(扩展了UserManager)。 The login does now work perfectly. 现在登录可以正常工作。 Now I do want to extend my project to support annotations in my controllers like the following: 现在,我确实想扩展项目以在控制器中支持注释,如下所示:

[Authorize(Roles = "TestRole")]
public IActionResult Trips()
{
  ...
}

Therefore I have also created my own ApplicationRole, ApplicationRoleManager, ApplicationRoleStore and registered them in my Startup: 因此,我还创建了自己的ApplicationRole,ApplicationRoleManager,ApplicationRoleStore并将它们注册到我的Startup中:

    services.AddIdentity<ApplicationUser, ApplicationRole>(config =>
    {
        config.User.RequireUniqueEmail = true;
        config.Password.RequiredLength = 8;
        config.Cookies.ApplicationCookie.LoginPath = "/Auth/Login";
        config.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
    }).AddUserStore<ApplicationUserStore<ApplicationUser>>()
    .AddRoleStore<ApplicationRoleStore<ApplicationRole>>()
    .AddUserManager<ApplicationUserManager>()
    .AddRoleManager<ApplicationRoleManager>(); 

My problem is now that the annotation does not work at all. 我的问题是,注释根本不起作用。 Actually I hoped that somehow the Roles method (from IQueryableRoleStore) in my ApplicationRoleStore would be fired. 实际上,我希望可以触发我的ApplicationRoleStore中的Roles方法(来自IQueryableRoleStore)。

Do I miss somewhere I binding or do I have a complete wrong idea of the identity/role concept? 我会错过绑定的地方还是对身份/角色概念有完全错误的想法?

Authorize attribute: 授权属性:

[Authorize(Roles = "TestRole")]
public IActionResult Trips()
{
  ...
}

is not going to invoke any identity stuff. 不会调用任何身份信息。 It is only going to check if the current user is in the role "TestRole" and only allow access if the user is in the role. 它将仅检查当前用户是否具有“ TestRole”角色,并且仅当该用户具有该角色时才允许访问。 This will be a check against the role cookie. 这将是对角色Cookie的检查。

You still need to build your own UI for managing role membership, adding removing users from roles in order to get that role into a user's cookie. 您仍然需要构建自己的UI来管理角色成员身份,并添加从角色中删除用户的功能,以便将该角色添加到用户的Cookie中。

If you need more ideas I have a project here that has role management implemented as well as Identity without entity framework 如果您需要更多想法,我在这里有一个项目,该项目已实现角色管理以及无需实体框架的身份

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM