ASP.NET Identity “角色-許可權”管理 3

來源:互聯網
上載者:User

標籤:

1.1.       Role管理

參考1:ASP.NET Identity 2.0: Customizing Users and Roles

參考2:asp.net identity 2.2.0 中角色啟用和基本使用(一)

1.1.1.      增加ApplicationRole

建立ApplicationRole,可參考ApplicationUser,過程如下所述。

修改IdentityModel.cs,新增ApplicationRole,繼承自IdentityRole,增加屬性Description。

public class ApplicationRole : IdentityRole

{

    public ApplicationRole(): base(){ }

    public ApplicationRole(string roleName)

        : this()

    {

        base.Name = roleName;

    }

 

    [Display(Name = "角色描述")]

    public string Description { get; set; }

}

 

1.1.2.      新增ApplicationRoleManager

修改IdentityConfig.cs,增加ApplicationRoleManager,繼承自RoleManager,提供靜態方法Create。

public class ApplicationRoleManager : RoleManager<ApplicationRole>

{

    public ApplicationRoleManager(IRoleStore<ApplicationRole, string> roleStore)

        : base(roleStore)

    {

    }

 

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)

    {

        return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));

    }

}

 

1.1.3.      啟用ApplicationRoleManager

修改Startup.Auth.cs,配置角色管理器,本質上是在MVC啟動階段註冊執行個體,供後繼的請求服務使用。

public partial class Startup

{

    // 有關配置身分識別驗證的詳細資料,請訪問 http://go.microsoft.com/fwlink/?LinkId=301864

    public void ConfigureAuth(IAppBuilder app)

    {

        // 設定資料庫上下文、使用者管理員和登入管理器,以便為每個請求使用單個執行個體

        app.CreatePerOwinContext(ApplicationDbContext.Create);

        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);   //添加的角色管理器

        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

 

1.1.4.      新增ViewModel

建立AdminViewModel.cs,添加RoleViewModel、EditUserViewModel。

public class RoleViewModel

{

    public string Id { get; set; }

    [Required(AllowEmptyStrings = false)]

    [Display(Name = "角色名稱")]

    public string Name { get; set; }

    [Display(Name="角色描述")]

    public string Description { get; set; }

}

EditUserViewModel

public class EditUserViewModel

{

    public string Id { get; set; }

    [Display(Name="使用者名稱")]

    [Required]

    public string UserName { get; set; }

 

    [Required(AllowEmptyStrings = false)]

    [Display(Name = "電郵地址")]

    [EmailAddress]

    public string Email { get; set; }

 

    public IEnumerable<SelectListItem> RolesList { get; set; }

}

 

1.1.5.      添加CRUD管理功能

為Role與User管理添加相應的MVC組件,這裡不再累述可參考AccountController等,為了方便可先使用MVC的支架功能,然後修改細節。

1.1.6.      運行效果

運行效果如所示。

1)        使用者管理

 

 

 

2)        角色管理

 

ASP.NET Identity “角色-許可權”管理 3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.