ASP. NET Identity Role-rights Management 8

Source: Internet
Author: User

1. Typical application Scenario 1.1. EF data Storage

The core of EF is the data context DbContext, which provides a basic method of data storage operations.

1.1.1. New

Take the method of adding objects.

Create permissions

var permission = new Applicationpermission

{

Id = Item. Gdb

Action = Item. Action,

Controller = Item. Controller,

Description = Item. Description

};

_db. Permissions.add (permission);

Save

Await _db. Savechangesasync ();

1.1.2. modifying

Adopt the method of modifying entity state.

_db. Entry (applicationpermission). state = entitystate.modified;

_db. SaveChanges ();

1.1.3. deleting

The way to remove objects is used.

Applicationpermission applicationpermission = _db. Permissions.find (ID);

_db. Permissions.remove (applicationpermission);

_db. SaveChanges ();

Adopt the method of modifying entity state.

Delete permission

var entity = new Applicationrolepermission {Roleid = Roleid, PermissionID = PermissionID};

_db. Set<applicationrolepermission> (). Attach (entity);

_db. Entry (entity). state = entitystate.deleted;

var result = await _db. Savechangesasync ();

1.1.4. Query

Part of the code in the sample project.

Fetch Data context

var context = HttpContext.Current.GetOwinContext (). Get<applicationdbcontext> ();

Take a role

var role = context. Roles.include (r = r.permissions). FirstOrDefault (t = t.id = = Roleid);

Take a list of permission IDs

var rolepermissionids = role. Permissions.select (t = t.permissionid);

Fetch permission List

Permissions = context. Permissions.where (p = rolepermissionids.contains (p.id)). ToList ();

var permissions = await _db. Permissions.tolistasync ();

Applicationpermission applicationpermission = _db. Permissions.find (ID);

1.2. Custom Comparator 1.2.1. Equality comparison IEqualityComparer

Applicationpermission objects need to compare the Controller, action, and description in order, which is a custom rule, for which the comparer implements the equality comparison interface IEqualityComparer.

public class Applicationpermissionequalitycomparer:iequalitycomparer<applicationpermission>

{

public bool Equals (applicationpermission x, applicationpermission y)

{

Compare ID First

if (String.Compare (X.id, y.id, true) = = 0)

{

return true;

}

Then compare controller,action,description and params

if (X.controller = = Y.controller | | x.action = Y.action | | x.description = y.description)

{

return true;

}

Else

{

return false;

}

}

public int GetHashCode (Applicationpermission obj)

{

var str = string. Format ("{0}-{1}-{2}", obj. Controller, obj. Action, obj. Description);

Return str. GetHashCode ();

}

}

Use the comparer in contains.

is authorized

if (Rolepermissions.contains (Action, New Applicationpermissionequalitycomparer ()))

{

return true;

}

Else

{

return false;

}

Use the comparer in except.

Fetching Assembly permissions

var allpermissions = _permissionsofassembly;

Fetch database already has permissions

var dbpermissions = _db. Permissions.tolist ();

Take the difference between the two sets

var permissions = allpermissions.except (dbpermissions, New Applicationpermissionequalitycomparer ());

1.2.2. Size comparison IComparer

Permissionviewmodel needs to be sorted by controller, action, and a custom rule, which is to implement the size comparison interface IComparer.

public class Permissionviewmodelcomparer:icomparer<permissionviewmodel>

{

public int Compare (Permissionviewmodel x, Permissionviewmodel y)

{

Equal if the ID is the same

if (String.Compare (X.id, y.id, true) = = 0)

{

return 0;

}

Controller comparison

var controllercompareresult = String.Compare (X.controller, Y.controller, true);

Action comparison

var actioncompareresult = String.Compare (X.action, y.action, true);

Compare controllers first, then compare action

if (Controllercompareresult! = 0)

{

return controllercompareresult;

}

Else

{

return actioncompareresult;

}

}

}

Use the comparer.

Sort

Permissionviews.sort (New Permissionviewmodelcomparer ());

ASP. NET Identity Role-rights Management 8

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.