Repository navigation
Expand file tree
/
Copy pathExploreAuthorization.cs
More file actions
119 lines (107 loc) · 3.9 KB
/
Copy pathExploreAuthorization.cs
File metadata and controls
119 lines (107 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Options;
using System.Security.Claims;
namespace Security.WebAppLib.Example
{
public class ExploreAuthorization
{
#region Init
IAuthorizationService _authorizationService;
IAuthorizationHandlerProvider _handlerProvider;
IAuthorizationPolicyProvider _policyProvider;
IAuthorizationHandlerContextFactory _handlerContextFact;
IAuthorizationEvaluator _handleEval;
AuthorizationOptions _authOptions;
/// <summary>
/// Services available from DI container
/// </summary>
public ExploreAuthorization(
IAuthorizationService authorizationService,
IAuthorizationHandlerProvider handlerProvider,
IAuthorizationPolicyProvider policyProvider,
IAuthorizationHandlerContextFactory handlerContextFact,
IAuthorizationEvaluator handleEval,
IOptions<AuthorizationOptions> authOptions
)
{
this._authorizationService = authorizationService;
this._handlerProvider = handlerProvider;
this._policyProvider = policyProvider;
this._handlerContextFact = handlerContextFact;
this._handleEval = handleEval;
this._authOptions = authOptions.Value;
}
#endregion
public async Task Run(int testNo = 0)
{
int? o = 3;
var t = o ?? testNo;
switch (t)
{
case 0:
await ExploreProvider();
break;
case 1:
await AuthorizeUser();
break;
case 2:
await AuthorizeResource();
break;
case 3:
CheckUserClaim();
break;
}
}
public async Task ExploreProvider()
{
AuthorizationPolicy p = await _policyProvider.GetDefaultPolicyAsync();
AuthorizationPolicy? p2 = await _policyProvider.GetFallbackPolicyAsync();
bool b = _policyProvider.AllowsCachingPolicies;
p2 = await _policyProvider.GetPolicyAsync("policy1&2");
}
/// <summary>
/// Check user claims against a policy
/// </summary>
public async Task AuthorizeUser()
{
var user = CreateUser();
AuthorizationResult res =
await _authorizationService.AuthorizeAsync(user, "policy1&2");
bool b = res.Succeeded;
AuthorizationFailure? failure = res.Failure;
}
/// <summary>
/// Check user claims against a requirement/polcy to
/// perform an action on a resource
/// </summary>
public async Task AuthorizeResource()
{
var user = CreateUser();
var resource = new MyAuthorizationService.AppData(1);
AuthorizationResult res =
await _authorizationService.AuthorizeAsync(user, resource, "policy5");
bool b = res.Succeeded;
AuthorizationFailure? failure = res.Failure;
}
/// <summary>
/// Manage access dynamically
/// </summary>
public void CheckUserClaim()
{
var user = CreateUser();
bool b = user.HasClaim(ClaimTypes.Name, "Joe Smith");
b = user.IsInRole("Admin1");
Claim? c = user.FindFirst(ClaimTypes.Role);
}
public ClaimsPrincipal CreateUser(string? authenticationType = null)
{
var claims = new List<Claim>
{
new(ClaimTypes.Name, "Joe Smith"),
new(ClaimTypes.Role, "Admin1"),
};
var identity = new ClaimsIdentity(claims, authenticationType);
return new ClaimsPrincipal(identity);
}
}
}