Skip to content

Commit c3f11bd

Browse files
committed
2 parents 1fe3677 + 77b8ad5 commit c3f11bd

File tree

1 file changed

+90
-5
lines changed

1 file changed

+90
-5
lines changed

README.md

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# Dynamic Role-Based Authorization in ASP.NET Core 2.0
2-
To view code sample for below description switch to [master branch](https://github.com/mo-esmp/DynamicRoleBasedAuthorizationNETCore/tree/master).
1+
# Dynamic Role-Based Authorization in ASP.NET Core 2.2 and 3.x [![NuGet](http://img.shields.io/nuget/v/DynamicAuthorization.Mvc.Core.svg?style=flat)](https://www.nuget.org/packages/DynamicAuthorization.Mvc.Core)
32

4-
You already know how role-based authorization wokrs in ASP.NET Core.
3+
You already know how role-based authorization works in ASP.NET Core.
54

65
```c#
76
[Authorize(Roles = "Administrator")]
@@ -10,9 +9,95 @@ public class AdministrationController : Controller
109
}
1110
```
1211

13-
But what if you don't want hardcode roles in authorization attribute or create roles later and specify in which controller and action it has access without touching source code ?
12+
But 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?
13+
14+
**DynamicAuthorization** helps you authorize users without hardcoding role(s) on the `Authorize` attribute with minimum effort. Keep in mind that DynamicAuthorization is built at the top of ASP.NET Core Identity and use identity mechanism formanaging roles and authorizing users.
15+
16+
Install 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)
17+
18+
```powershell
19+
Install-Package DynamicAuthorization.Mvc.Core
20+
Install-Package DynamicAuthorization.Mvc.JsonStore
21+
```
22+
or
23+
```shell
24+
dotnet add package DynamicAuthorization.Mvc.Core
25+
dotnet add package DynamicAuthorization.Mvc.JsonStore
26+
```
27+
28+
Then, add `AddDynamicAuthorization()` to `IServiceCollection` in `ConfigureServices` method:
29+
30+
```csharp
31+
public void ConfigureServices(IServiceCollection services)
32+
{
33+
...
34+
services
35+
.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = false)
36+
.AddEntityFrameworkStores<ApplicationDbContext>()
37+
.AddDefaultTokenProviders();
38+
39+
services
40+
.AddDynamicAuthorization<ApplicationDbContext>(options => options.DefaultAdminUser = "UserName")
41+
.AddJsonStore(options => options.FilePath = "FilePath");
42+
```
43+
44+
You can set default admin username via `DefaultAdminUser` config to access everywhere and wihtout needing create default admin role and it's access.
45+
46+
Role access will be saved in json file and you can specify the file path `FilePath` config.
47+
48+
The 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`:
49+
50+
```c#
51+
public class MvcControllerInfo
52+
{
53+
public string Id => $"{AreaName}:{Name}";
54+
55+
public string Name { get; set; }
56+
57+
public string DisplayName { get; set; }
58+
59+
public string AreaName { get; set; }
60+
61+
public IEnumerable<MvcActionInfo> Actions { get; set; }
62+
}
63+
64+
public class MvcActionInfo
65+
{
66+
public string Id => $"{ControllerId}:{Name}";
67+
68+
public string Name { get; set; }
69+
70+
public string DisplayName { get; set; }
71+
72+
public string ControllerId { get; set; }
73+
}
74+
```
75+
76+
```
77+
78+
`Get
79+
and creating role and assing access to role. In `Controller` folder create `RoleController` then add `Create` action:
80+
81+
```
82+
public class RoleController : Controller
83+
{
84+
private readonly IMvcControllerDiscovery _mvcControllerDiscovery;
85+
86+
public RoleController(IMvcControllerDiscovery mvcControllerDiscovery)
87+
{
88+
_mvcControllerDiscovery = mvcControllerDiscovery;
89+
}
90+
91+
// GET: Role/Create
92+
public ActionResult Create()
93+
{
94+
ViewData["Controllers"] = _mvcControllerDiscovery.GetControllers();
95+
96+
return View();
97+
}
98+
}
99+
```
14100

15-
Create ASP.NET Core Web Application project and change authentication to Individual User Accounts.
16101

17102
![create project](https://raw.githubusercontent.com/mo-esmp/DynamicRoleBasedAuthorizationNETCore/master/assets/create-project.jpg)
18103

0 commit comments

Comments
 (0)