ASP. NET Identity Role-rights Management 6

Source: Internet
Author: User

1.1. Role-permission

Userrole is the association table for user and role, and the code is shown in User-role analysis. Refer to the role-permission design, the code map is as follows, Rolepermission is the role and permission of the association table, the preservation of Roleid and PermissionID.

1.1.1. New Rolepermission

Add the Applicationrolepermission class to the IdentityModels.cs.

public class Applicationrolepermission

{

Public virtual string Roleid {get; set;}

Public virtual string Permisssionid {get; set;}

}

1.1.2. Adding a rolepermission list

Add a list of rolepermission to Applicationrole.

public class Applicationrole:identityrole

{

Public Applicationrole ()

: Base ()

{

Permissions = new list<applicationrolepermission> ();

}

Public Applicationrole (String roleName)

: This ()

{

Base. Name = RoleName;

}

[Display (Name = "role description")]

public string Description {get; set;}

<summary>

Permissions List

</summary>

Public icollection<applicationrolepermission> Permissions {get; Set }

}

Add a list of rolepermission to Applicationpermission.

public class Applicationpermission

{

Public Applicationpermission ()

{

Id = Guid.NewGuid (). ToString ();

Roles = new list<applicationrolepermission> ();

}

<summary>

Primary key

</summary>

public string Id {get; set;}

<summary>

Controller name

</summary>

public string Controller {get; set;}

<summary>

Method name

</summary>

public string Action {get; set;}

<summary>

Parameter string

</summary>

public string Params {get; set;}

<summary>

Function description

</summary>

public string Description {get; set;}

<summary>

List of roles

</summary>

Public icollection<applicationrolepermission> Roles {get; Set }

}

1.1.3. Configuring Role-permission Many-to-many relationships

Rewrite the Applicationdbcontext onmodelcreating, configure the 1-to-many relationship for Role-rolepermission and permission-rolepermission.

public class Applicationdbcontext:identitydbcontext<applicationuser>

{

Public Applicationdbcontext ()

: Base ("DefaultConnection")

{

Initialize the database when you first start the site Add Administrator user credentials and Admin role to the database

Database.setinitializer<applicationdbcontext> (New Applicationdbinitializer ());

}

protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)

{

if (ModelBuilder = = null)

{

throw new ArgumentNullException ("ModelBuilder");

}

Configuring the 1-to-many relationship between permission and Rolepermission

entitytypeconfiguration<applicationpermission> configuration = modelbuilder.entity<applicationpermission > (). ToTable ("Applicationpermissions");

Configuration. hasmany<applicationrolepermission> (U = u.roles). Withrequired (). hasforeignkey (ur = ur). Permisssionid);

Configure the key of the mapping table rolepermission for role and Persmission

Modelbuilder.entity<applicationrolepermission> (). Haskey (r = new {Permisssionid = R.permisssionid, Roleid = R.roleid}). ToTable ("Applicationrolepermissions");

Configuring a 1-to-many relationship between role and rolepermission

entitytypeconfiguration<applicationrole> Configuration2 = modelbuilder.entity<applicationrole> ();

Configuration2. Hasmany<applicationrolepermission> (r = r.permissions). Withrequired (). hasforeignkey (ur = ur). Roleid);

Base. Onmodelcreating (ModelBuilder);

}

public static Applicationdbcontext Create ()

{

return new Applicationdbcontext ();

}

Public new idbset<applicationrole> Roles {get; set;}

Public virtual idbset<applicationpermission> Permissions {get; set;}

}

Note: Because the required type is applicationrole, the attribute roles definition in the parent class is overridden.

1.1.4. Establishing ViewModel

Add the Roleid, RoleName property to the Permissionviewmodel.

public class Permissionviewmodel

{

<summary>

Primary key

</summary>

[Display (Name = "Permission id")]

public string Id {get; set;}

<summary>

Controller name

</summary>

[Required (allowemptystrings = False)]

[Display (name = "Controller name")]

public string Controller {get; set;}

<summary>

Method name

</summary>

[Required (allowemptystrings = False)]

[Display (name = "method Name")]

public string Action {get; set;}

<summary>

Function description

</summary>

[Required (Allowemptystrings = True)]

[Display (Name = "function description")]

public string Description {get; set;}

[Display (Name = "select")]

public bool Selected {get; set;}

[Display (Name = "Role id")]

public string Roleid {get; set;}

[Display (name = "role name")]

public string RoleName {get; set;}

}

1.1.5. Building a Controller

Role-permission management without editing functions, more than permission management of an incoming parameter Roleid, create a new RolePermissionsController.cs, add the appropriate MVC part, here is no longer a reference to the previous section.

Index, using AutoMapper to complete the object mapping.

Public Async task<actionresult> Index (string roleid)

{

Take role list

var roles = _rolemanager.roles.tolist ();

Whether the Roleid is empty

if (Roleid = = null)

{

Take the ID of the first role

Roleid = roles. FirstOrDefault (). Id;

}

Put viewbag, set default value

Viewbag.roleid = new SelectList (roles, "ID", "Description", Roleid);

List of Role permissions

var permissions = await _rolemanager.getrolepermissionsasync (Roleid);

Create ViewModel

var permissionviews = new list<permissionviewmodel> ();

var map = mapper.createmap<applicationpermission, permissionviewmodel> ();

Permissions. each (t =

{

var view = mapper.map<permissionviewmodel> (t);

View. Roleid = Roleid;

Permissionviews.add (view);

});

Sort

Permissionviews.sort (New Permissionviewmodelcomparer ());

Return View (permissionviews);

}

Create of the HttpPost method.

Post:rolepermissions/edit/5

[HttpPost]

[Validateantiforgerytoken]

Public Async task<actionresult> Create (string roleid, ienumerable<permissionviewmodel> data)

{

if (string. Isnullorwhitespace (Roleid))

{

return new Httpstatuscoderesult (httpstatuscode.badrequest);

}

Add permission

foreach (var item in data)

{

var permission = new Applicationrolepermission

{

Roleid = Roleid,

PermissionID = Item. Id

};

Method 1, with set<> (). ADD ()

_db. Set<applicationrolepermission> (). ADD (permission);

}

Save

var records = await _db. Savechangesasync ();

Return redirecttoaction ("Index", new {Roleid = Roleid});

Return message

dictionary<string, bool> response = new dictionary<string, bool> ();

Response. ADD ("Success", true);

return new Jsonresult {Data = response};

}

1.1.6. Running effect

Index

Create

ASP. NET Identity Role-rights Management 6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.