MVC 5 & ASP.NET Identity - Implementation Confusion

后端 未结 2 1080
感情败类
感情败类 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(
        new UserStore(new ApplicationDbContext()))
    
    // Find user
    var user = manager.FindById(User.Identity.GetUserId());
    var profileProperty_1 = user.profileProperty_1 
    

提交回复
热议问题