@@ -11,7 +11,7 @@ public class AdministrationController : Controller
1111
1212But what if you don't want hardcode roles on the ` Authorize `  attribute or create roles later and specify in which controller and action it has access without touching source code?
1313
14- ** DynamicAuthorization**  helps you authorize users without hardcoding role(s) on the  ` Authorize `  attribute with minimum effort. DynamicAuthorization is built at the top of ASP.NET Core Identity and use  identity mechanism for managing roles and authorizing users.
14+ ** DynamicAuthorization**  helps you authorize users without hardcoding role(s) on the  ` Authorize `  attribute with minimum effort. DynamicAuthorization is built at the top of ASP.NET Core Identity and uses  identity mechanism for managing roles and authorizing users.
1515
1616Install the _ DynamicAuthorization.Mvc.Core_  [ NuGet package] ( https://www.nuget.org/packages/DynamicAuthorization.Mvc.Core )  and _ DynamicAuthorization.Mvc.JsonStore_  [ NuGet package] ( https://www.nuget.org/packages/DynamicAuthorization.Mvc.JsonStore ) 
1717
@@ -45,6 +45,95 @@ You can set default admin username via `DefaultAdminUser` config to access every
4545
4646Role  access  will  be  saved  in  JSON  file  and  you  can  specify  the  file  path  `FilePath ` config .
4747
48+ You  can  decorate  controllers  and  actions  with  `DisplayName ` attribute  to  show  user  a  more  meaningful  name  instead  of  controller  and  action  name .
49+ ```c #
50+ [DisplayName (" Access Management"  )]
51+ public  class  AccessController  :  Controller 
52+ {
53+ 
54+     //  GET: Access 
55+      [DisplayName (" Access List"  )]
56+     public  async  Task < ActionResult >  Index ()
57+ }
58+ ```
59+ 
60+ You  can  also  use  default  UI  to  for  managing  roles  and  assigning  roles  to  users  if  you  don 't want to implement them by yourself.  
61+ 
62+ Install  the  _DynamicAuthorization .Mvc .Ui_  [NuGet  package ](https :// www.nuget.org/packages/DynamicAuthorization.Mvc.Ui)
63+ 
64+ ```powershell 
65+ Install - Package  DynamicAuthorization .Mvc .Ui 
66+ ```
67+ 
68+ Then  `AddUi ` to  DynamicAuthorization  registration :
69+ ```
70+ services 
71+         .AddDynamicAuthorization <ApplicationDbContext >(options  =>  options .DefaultAdminUser  =  " UserName"  )
72+         .AddJsonStore (options  =>  options .FilePath  =  " FilePath"  )
73+         .AddUi ();
74+ ```
75+ 
76+ Use  `/ role ` url  to  manage  roles  and  `/ userrole ` url  to  assign  roles  to  users .
77+ 
78+ ! [create  project ](https :// raw.githubusercontent.com/mo-esmp/DynamicRoleBasedAuthorizationNETCore/dev/assets/create-role-2.jpg)
79+ 
80+ You  can  also  use  a  custom  `TageHelper ` to  check  whether  the  user  has  access  to  view  content  or  not . create  a  custom  tag  helper  that  inherits  from  `SecureContentTagHelper `
81+ 
82+ ```c #
83+ [HtmlTargetElement (" secure-content"  )]
84+ public  class  MySecureContentTagHelper  : SecureContentTagHelper < ApplicationDbContext > 
85+ {
86+     public  MySecureContentTagHelper (
87+         ApplicationDbContext  dbContext ,
88+         DynamicAuthorizationOptions  authorizationOptions ,
89+         IRoleAccessStore  roleAccessStore 
90+         )
91+         :  base (dbContext , authorizationOptions , roleAccessStore )
92+     {
93+     }
94+ }
95+ ```
96+ 
97+ In  each  view  wrap  a  content  or  an  anchor  tag  inside  `secure - content ` tag :
98+ 
99+ ```html 
100+ < ul  class = " nav navbar-nav" > 
101+     < li >< a  asp - area = " "   asp - controller = " Home"   asp - action = " Index" > Home < / a >< / li > 
102+     < li >< a  asp - area = " "   asp - controller = " Home"   asp - action = " About" > About < / a >< / li > 
103+     < li >< a  asp - area = " "   asp - controller = " Home"   asp - action = " Contact" > Contact < / a >< / li > 
104+     
105+     < secure - content  asp - area = " "   asp - controller = " Role"   asp - action = " Index" > 
106+         < li >< a  asp - area = " "   asp - controller = " Role"   asp - action = " Index" > Role < / a >< / li > 
107+     < / secure - content > 
108+     < secure - content  asp - area = " "   asp - controller = " Access"   asp - action = " Index" > 
109+         < li >< a  asp - area = " "   asp - controller = " Access"   asp - action = " Index" > Access < / a >< / li > 
110+     < / secure - content > 
111+ < / ul > 
112+ ```
113+ 
114+ Don 't forget to add your tag halper namespace to `_ViewImports.cshtml`:  
115+ ```cshtml 
116+ @using  SampleMvcWebApp 
117+ @addTagHelper  * , Microsoft .AspNetCore .Mvc .TagHelpers 
118+ @addTagHelper  * , SampleMvcWebApp 
119+ ```
120+ 
121+ If  you  extended  `IdentityUser ` or  you  changed  user  and  role  key , you  should  pass  user  and  role  type  too . for  example :
122+ 
123+ ```c #
124+ public  class  ApplicationDbContext  : IdentityDbContext < ApplicationUser >  { .. . }
125+ public  class  MySecureContentTagHelper  : SecureContentTagHelper < ApplicationDbContext , ApplicationUser >  { .. . }
126+ ```
127+ 
128+ or 
129+ 
130+ ```c #
131+ public  class  ApplicationDbContext  : IdentityDbContext < ApplicationUser , ApplicationRole , int >  { .. . }
132+ public  class  MySecureContentTagHelper  : SecureContentTagHelper < ApplicationDbContext , ApplicationUser , ApplicationRole , int >  { .. . }
133+ ```
134+ #
135+ 
136+ If  you  don 't want to use the default UI, follow the below steps to discover controller and actions and give access to role and then assign role to user.  
48137The  next  step  is  discovering  controllers  and  actions . `IMvcControllerDiscovery ` return  all  controllers  and  actions  that  decorated  with  `[Authorize ]` attribute . `IMvcControllerDiscovery .GetControllers ()` method  returns  list  of   `MvcControllerInfo `: 
49138
50139```c #
@@ -73,7 +162,7 @@ public class MvcActionInfo
73162}
74163```
75164
76- The  next  step  is  creating  a  role  assign  acccess  to  it . Use  `RoleManager <>` to  create  role  and  `IRoleAccessStore ` to  store  role  access .
165+ The  next  step  is  creating  a  role  to   assign  access  to  it . Use  `RoleManager <>` to  create  role  and  `IRoleAccessStore ` to  store  role  access .
77166
78167```c #
79168var  role  =  new  IdentityRole  { Name  =  " RoleName"   };
@@ -87,7 +176,8 @@ var roleAccess = new RoleAccess
87176};
88177await  _roleAccessStore .AddRoleAccessAsync (roleAccess );
89178```
90- The  final  step  is  assigning  created  role  to  a  user : 
179+ 
180+ The  final  step  is  assigning  a  created  role  to  a  user : 
91181
92182```c #
93183var  user  =  await  _userManager .FindByIdAsync (" someId"  );
@@ -96,8 +186,7 @@ await _userManager.AddToRolesAsync(user, new [] { "RoleName" });
96186
97187And  now  the  user  only  can  access  those  controllers  and  actions  that  his  roles  can  access .
98188
99- Here  is  an  example  to  create  a  role  and  assign  access  to  the  role . Check  out  samples  to  view  full  implementation .
100- 
189+ Here  is  an  example  to  create  a  role  and  assign  access  to  the  role .
101190```c #
102191public  class  RoleViewModel 
103192{
@@ -176,93 +265,8 @@ public class RoleController : Controller
176265    }
177266}
178267```
268+ Checkout  samples  to  view  full  implementation .
179269
180- You can decorate controllers and actions with ` DisplayName `  attribute to show user a more meaningful name instead of controller and action name.
181- ``` c# 
182- [DisplayName (" Access Management"  )]
183- public  class  AccessController  : Controller 
184- {
185- 
186-     //  GET: Access 
187-      [DisplayName (" Access List"  )]
188-     public  async  Task <ActionResult > Index ()
189- }
190- ```
191270#
192- You can also use default UI to for managing roles and assigning roles to users if you don't want to implement them by yourself.
193271
194- Install the _DynamicAuthorization.Mvc.Ui_ [NuGet  package ](https:// www.nuget.org/packages/DynamicAuthorization.Mvc.Ui)
195- 
196- ```powershell
197- Install-Package DynamicAuthorization.Mvc.Ui
198- ```
199- 
200- Then `AddUi` to DynamicAuthorization registration:
201- ```
202- services
203-         .AddDynamicAuthorization<ApplicationDbContext>(options => options.DefaultAdminUser =  " UserName"  )
204-         .AddJsonStore (options => options.FilePath =  " FilePath"  )
205-         .AddUi ();
206- ```
207- 
208- Use `/role` url and to manage roles and `/userrole` to assing roles to users.
209- 
210- 
211- 
212- #
213- You can also use a custom `TageHelper` to check whether user has access to view a content or not. create a cutome tag helper that inherits from `SecureContentTagHelper`
214- 
215- ```c#
216- [HtmlTargetElement (" secure-content"  )]
217- public  class  MySecureContentTagHelper  : SecureContentTagHelper <ApplicationDbContext >
218- {
219-     public  MySecureContentTagHelper (
220-         ApplicationDbContext  dbContext ,
221-         DynamicAuthorizationOptions  authorizationOptions ,
222-         IRoleAccessStore  roleAccessStore 
223-         )
224-         : base (dbContext , authorizationOptions , roleAccessStore )
225-     {
226-     }
227- }
228- ```
229- 
230- In each view wrap a content or an anchor tag inside `secure-content` tag:
231- 
232- ```html
233- <ul  class = " nav navbar-nav" > 
234-     < li >< a  asp - area = " "   asp - controller = " Home"   asp - action = " Index" > Home < / a >< / li > 
235-     < li >< a  asp - area = " "   asp - controller = " Home"   asp - action = " About" > About < / a >< / li > 
236-     < li >< a  asp - area = " "   asp - controller = " Home"   asp - action = " Contact" > Contact < / a >< / li > 
237-     
238-     < secure - content  asp - area = " "   asp - controller = " Role"   asp - action = " Index" > 
239-         < li >< a  asp - area = " "   asp - controller = " Role"   asp - action = " Index" > Role < / a >< / li > 
240-     < / secure - content > 
241-     < secure - content  asp - area = " "   asp - controller = " Access"   asp - action = " Index" > 
242-         < li >< a  asp - area = " "   asp - controller = " Access"   asp - action = " Index" > Access < / a >< / li > 
243-     < / secure - content > 
244- < / ul > 
245- ```
246- 
247- Don 't forget to add your tag halper namespace to `_ViewImports.cshtml`  
248- ```cshtml 
249- @using  SampleMvcWebApp 
250- @addTagHelper  * , Microsoft .AspNetCore .Mvc .TagHelpers 
251- @addTagHelper  *, SampleMvcWebApp 
252- ```
253- 
254- If  you  extended  `IdentityUser ` or  you  changed  user  and  role  key , you  should  pass  user  and  role  type  too . for  example :
255- 
256- ```c #
257- public  class  ApplicationDbContext  : IdentityDbContext <ApplicationUser > { ... }
258- public  class  MySecureContentTagHelper  : SecureContentTagHelper <ApplicationDbContext , ApplicationUser > { ... }
259- ```
260- 
261- or 
262- 
263- ```c #
264- public  class  ApplicationDbContext  : IdentityDbContext <ApplicationUser , ApplicationRole , int > { ... }
265- public  class  MySecureContentTagHelper  : SecureContentTagHelper <ApplicationDbContext , ApplicationUser , ApplicationRole , int > { ... }
266- ```
267- #
268272To  implement  DynamicAuthorization  step  by  step  by  yourself  checkout  [manual  branch ](https :// github.com/mo-esmp/DynamicRoleBasedAuthorizationNETCore/tree/manual).
0 commit comments