ASP. NET Identity Role-rights Management 5

Source: Internet
Author: User

1.1. Permission Management

Reference 1:asp.net Large Project Practice (11)-Permission management based on MVC action granularity

Refer to 2:asp.net MVC three important descriptive objects: Actiondescriptor

Here permission refers to the action, which is the function that is called by the user.

1.1.1. New Applicationpermission

Modify IdentityModel.cs, add applicationpermission, this design attribute ID, Controller, Action, Params, Description.

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;}

}

1.1.2. Establishing ViewModel

Add Permissionviewmodel to AdminViewModel.cs.

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;}

}

1.1.3. Getting permission automatically

Core idea: Use the reflection mechanism to read the meta data of each action, such as: Controller, action name, parameter, function description, for which the attribute description is used.

1) Characteristic Description example

Add a reference System.ComponentModel to add the description attribute to the action.

Using System.ComponentModel;

public class Usersadmincontroller:basecontroller

{

Get:usersadmin

[Description ("User list")]

Public async task<actionresult> Index ()

{

Return View (await _usermanager.users.tolistasync ());

}

Omit part of the code ...

2) Read the action information in the program set

Create a new ActionPermissionService.cs, using Reflectedcontrollerdescriptor and actiondescriptor in MVC to get the meta data.

Internal Static class Actionpermissionservice

{

<summary>

Use descriptor to take the metadata of all the actions in the Assembly

</summary>

<returns></returns>

public static ienumerable<applicationpermission> getallactionbyassembly ()

{

var result = new list<applicationpermission> ();

Take all types in an assembly

var types = Assembly.Load ("Aspnetidentity2permission.mvc"). GetTypes ();

Take the Controller

foreach (var type in types)

{

if (type. BaseType = = typeof (Basecontroller))//If it is Basecontroller

{

Reflection Controller

var controller = new Reflectedcontrollerdescriptor (type);

Take the action of the Controller, a common instance method

var actions = Controller. Getcanonicalactions ();

Build Permissions

foreach (var action in actions)

{

Create permissions

var ap = new Applicationpermission ()

{

Action = action. ActionName,

Controller = Controller. Controllername,

Params = Formatparams (action),

Description = GetDescription (action)

};

Result. ADD (AP);

}

}

}

return result;

}

}

Gets the description information in the description attribute of the action, because Actiondescriptor implements the interface ICustomAttributeProvider, so the incoming parameter type is an interface.

<summary>

Take the description text of the action

</summary>

<param name= "Action" ></param>

<returns></returns>

public static string GetDescription (ICustomAttributeProvider action)

{

Derived from a defined attribute array

var description = action. GetCustomAttributes (typeof (DescriptionAttribute), false);

Remove description, otherwise empty

var result = description. Length > 0? (Description[0] as DescriptionAttribute). Description:null;

return result;

}

Formats the action's arguments.

<summary>

Formatting the argument string of the action

</summary>

<param name= "Action" ></param>

<returns></returns>

public static string Formatparams (Actiondescriptor action)

{

var param = action. GetParameters ();

var result = new StringBuilder ();

if (param. Length > 0)

{

foreach (var item in param)

{

Result. Append (String. Format ("type:{0}, Name:{1};", item. ParameterType, item. ParameterName));

}

return result. ToString ();

}

Else

{

return null;

}

}

1.1.4. CRUD Management Features

Add the appropriate MVC part for permission, which is no longer a reference to the previous section.

1.1.5. Running effect

Index list

Create new

Edit

Delete

ASP. NET Identity Role-rights Management 5

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.