MVC 5 & ASP.NET Identity - Implementation Confusion

后端 未结 2 1074
感情败类
感情败类 2020-12-08 23:19

I\'m creating a new web application that will be written using MVC 5 and Entity Framework Database First Approach. I would also like to use

相关标签:
2条回答
  • 2020-12-08 23:32

    Here is the solution

    To speed things up you can add sample app to your project and start by modifying the sample app, Samples app includes confirmation email, password recovery, roles admin and user role management etc. NuGet package is at:

    Install-Package Microsoft.AspNet.Identity.Samples -Pre 
    

    See full details on sample app here: ASP.NET Identity 2.0: Customizing Users and Roles

    Controll access to controller or Action by using below attributes

    [Authorize] //Anyone with authorization
    [Authorize(Roles="Administrator")] //Admin role only
    

    Check if user is in role by

    HttpContext.User.IsInRole("Administrator")
    UserManager.IsInRole(userID, "Administrator")
    

    Get profile data by

    // Create manager
     var manager = new UserManager<ApplicationUser>(
        new UserStore<ApplicationUser>(new ApplicationDbContext()))
    
    // Find user
    var user = manager.FindById(User.Identity.GetUserId());
    var profileProperty_1 = user.profileProperty_1 
    
    0 讨论(0)
  • 2020-12-08 23:56

    1) The new Katana Cookie middleware supports claims. This is what makes this better than forms auth cookie; claims model any key/value pair and those can be stored in the authentication cookie. See this post for more details:

    http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/

    2 & 3) As far as your storage for identity data, if you need to work with an existing table then you might not be able to use Microsoft's EF provided classes. Instead you'd be left on your own to implement IUserStore and all the other store interfaces your app needs. I'm not certain it's worth changing what you're already using to store the user data.

    Keep in mind that the OWIN/Katana part is separate from the identity storage.

    0 讨论(0)
提交回复
热议问题