ASP. NET Identity Role-rights Management 9

Source: Internet
Author: User

1.1. Object Mapping

Reference 1:https://github.com/automapper/automapper

Configuration method for reference 2:automapper

Reference 3: Use AutoMapper to achieve free conversion of dtos and model (UP)

To accommodate the change in view, the data is encapsulated in ViewModel to maintain the pure stability of the domain model, where automapper is used to process the mapping between the model and the ViewModel.

1.1.1. Mapping class Profile

Inherit from the profile, each mapping class is a set of inter-object mapping rules, according to the actual needs can be set multiple sets of rules.

[Description ("AutoMapper configuration")]

public class AutoMapperProfile:AutoMapper.Profile

{

protected override void Configure ()

{

Createmap<applicationpermission, permissionviewmodel> ();

Createmap<permissionviewmodel, applicationpermission> ();

Createmap<applicationrole, roleviewmodel> ();

Createmap<roleviewmodel, Applicationrole> ()

. Formember (

Dest = dest. Gdb

Sour =

{

Sour. Mapfrom (s = s.id?? System.Guid.NewGuid (). ToString ());

});

Createmap<applicationuser, edituserviewmodel> ();

Createmap<edituserviewmodel, applicationuser> ();

Createmap<registerviewmodel, applicationuser> ();

}

}

1.1.2. Configuration Classes

Loads a mapping rule that provides a static method for external invocation.

[Description ("AutoMapper match")]

public class Automapperconfig

{

public static void Configure ()

{

AutoMapper.Mapper.Initialize (cfg =

{

Cfg. Addprofile<automapperprofile> ();

});

}

}

1.1.3. Modifying the Global class

Modify GLOBAL.ASAX,MVC to load the AutoMapper configuration at startup.

public Class MvcApplication:System.Web.HttpApplication

{

    protected void Application_Start ()

    {

         Arearegistration.registerallareas ();

        filterconfig.registerglobalfilters ( Globalfilters.filters);

        routeconfig.registerroutes (RouteTable.Routes);

        bundleconfig.registerbundles (BundleTable.Bundles);

       //automapper configuration

        automapperconfig.configure ();

   }

}

1.2. Paging

Reference 1:mvcpager Overview

Reference 2: No nonsense MVC Primer Tutorial VIII [use of mvcpager pagination control]

Paging is a frequently used feature, with the Ajax paging provided by the domestic open source control Mvcpager, which can be installed through NuGet.

1.2.1. Controller

Add reference WEBDIYER.WEBCONTROLS.MVC, increase page index parameter index, return data call Topagedlist (Index,pagesize) method.

Using WEBDIYER.WEBCONTROLS.MVC;

Get:rolesadmin

[Description ("Role list")]

Public ActionResult Index (int index = 1)

{

var roles = _rolemanager.roles;

var views = new list<roleviewmodel> ();

foreach (var role in roles)

{

var view = mapper.map<roleviewmodel> (role);

Views. ADD (view);

}

return View (views. Topagedlist (index, 10));//Show List of roles

}

1.2.2. View

Add a reference, change the model type to Ipagedlist, create a new div with ID named views, add paging parameters, and increase the script reference.

Note: The Pageindexparametername value should be the same as the page index parameter of the action, Updatetargetid should be the same as the Div ID.

@using WEBDIYER.WEBCONTROLS.MVC

@model ipagedlist<aspnetidentity2permission.mvc.models.roleviewmodel>

Omit page code ...

<div id= "views" >

<table class= "Table Table-hover table-striped" >

Omit table code ...

</table>

@Ajax. Pager (Model, new pageroptions {pageindexparametername = "index"}, new Mvcajaxoptions {Updatetargetid = "views", E Nablepartialloading = true})

</div>

@section Scripts{@{html.registermvcpagerscriptresource ();}}

1.2.3. Running effect

Overall effect.

Pagination effect.

1.3. Cache 1.3.1. Application

Because the reflection efficiency is not high, and once the assembly is compiled, the internal permissions information is fixed, so caching this information in application can improve efficiency.

The Assembly permission information cache.

<summary>

Cache key

</summary>

Const string _permissionkey = "permissionsofassembly";

<summary>

Set of permissions in an assembly

</summary>

Protected ienumerable<applicationpermission> _permissionsofassembly

{

Get

{

Read permission information from the cache

var permissions = HttpContext.Application.Get (_permissionkey)

As ienumerable<applicationpermission>;

if (permissions = = null)

{

Take all permissions in a program set

permissions = actionpermissionservice.getallactionbyassembly ();

Add to Cache

HTTPCONTEXT.APPLICATION.ADD (_permissionkey, permissions);

}

return permissions;

}

}

1.3.2. Session

Similarly, authentication requires frequent access to user permissions, and caching of user-permissions also increases efficiency, so this part of the data is stored in the session.

<summary>

Take the current user's permissions list

</summary>

<param name= "Context" ></param>

<returns></returns>

Private ienumerable<applicationpermission> Getuserpermissions (httpcontextbase context)

{

FETCH login Name

var username = context. User.Identity.Name;

Building the Cache key

var key = string. Format ("userpermissions_{0}", username);

Fetching permissions from the cache

var permissions = Httpcontext.current.session[key]

As ienumerable<applicationpermission>;

If not, fetch and write the cache from the DB

if (permissions = = null)

{

Take rolemanager

var rolemanager = context. Getowincontext (). Get<applicationrolemanager> ();

Fetch User Rights Collection

Permissions = rolemanager.getuserpermissions (username);

Write Cache

Context. Session.add (key, permissions);

}

return permissions;

}

1.3.3. Modifying the logout logic

In order to avoid the same machine different user logon when the permissions confusion, the user log out to clear the session cache, modify AccountController.cs in logoff, log out to clear all the cache.

[HttpPost]

[Validateantiforgerytoken]

Public ActionResult LogOff ()

{

Authenticationmanager.signout ();

Remove Cache

Base. HttpContext.Session.RemoveAll ();

Return redirecttoaction ("Index", "Home");

}

ASP. NET Identity Role-rights Management 9

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.