How to set a privilege inside a role in ASP.NET MVC? -


i have system there user table, role table, , user-roles association table, 1 user can associated multiple roles (like admin, basicuser, etc.). able authorize action methods based on these roles. identity framework.

now want add support privileges action methods can restricted based on well, rather roles. example, in controller, may have httppost action "write" privilege should able perform successfully.

what changes need make can assign privileges roles? i.e., want select "admin" role have "write" , "read" privileges, while "basicuser" role assigned "read" privilege. way, admin can access method allowed write privilege, while basicuser can not.

if create table called "privilege" , association table between , roles, , code set privileges in role, how can use privilege filter? example, below action should allowed performed user in role has "write" privilege attributed it.

[write] public actionresult create() {    return view(); } 

thank you.

the same way authorizeattribute works, can create custom authorization attribute inheriting it:

public class authorizeprivilegeattribute : authorizeattribute {     // custom property     public string privilege { get; set; }      protected override bool authorizecore(httpcontextbase httpcontext)     {         // reusing default authentication.          var isauthorized = base.authorizecore(httpcontext);         if (!isauthorized)         {                             return false;         }          return yourcustomcode.hasprivilege(this.privilege))           } } 

then using on top of method:

[authorizeprivilege(privilege = "write")] public actionresult create() {     return view(); } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -