简体   繁体   中英

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). 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:

    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.

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. This will be a check against the role 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.

If you need more ideas I have a project here that has role management implemented as well as Identity without entity framework

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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