Repository navigation
Expand file tree
/
Copy pathAppService.cs
More file actions
176 lines (149 loc) · 6.06 KB
/
Copy pathAppService.cs
File metadata and controls
176 lines (149 loc) · 6.06 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;
using Security.WebAppLib.Example;
using Security.WebAppLib.IdentityCore;
namespace Security.WebAppLib
{
/// <summary>
/// Configures and registers services for ASP.NET Core Identity,
/// authentication, and authorization.
/// </summary>
public class AppService
{
IServiceCollection _services;
IConfiguration _config;
public AppService(IServiceCollection services, IConfiguration config)
{
this._services = services;
this._config = config;
}
#region Identity
/// <summary>
/// Registers ASP.NET Core Identity services with custom user
/// and role stores and authentication configurations.
/// </summary>
public virtual void AddMyIdentity()
{
_services.AddScoped<MyUserStore>();
_services.AddScoped<IUserStore<IMyUser>, MyUserStore>();
_services.AddScoped<MyRoleStore>();
_services.AddScoped<IRoleStore<IMyRole>, MyRoleStore>();
// Register Identity services with 4 cookie authentication schemes with names
// from the IdentityConstants enum : ApplicationScheme, ExternalScheme,
// TwoFactorRememberMeScheme, TwoFactorUserIdScheme
IdentityBuilder b1 = _services
.AddIdentity<IMyUser, IMyRole>((IdentityOptions setupOption) =>
{
_config.Bind("IdentityOptions", setupOption);
})
.AddSignInManager()
.AddUserStore<MyUserStore>()
.AddRoleStore<MyRoleStore>()
.AddDefaultTokenProviders();
}
#endregion
#region Authentication
/// <summary>
/// Registers authentication schemes and configurations.
/// </summary>
public virtual AuthenticationBuilder AddMyAuthentication(
string? cfgAuthenOptions = null, string? cfgAuthenCookieOpt = null)
{
// Configure default authentication schemes for various sign-in scenarios.
AuthenticationBuilder b = _services.AddAuthentication((AuthenticationOptions options) =>
{
if (!string.IsNullOrWhiteSpace(cfgAuthenOptions))
_config.Bind(cfgAuthenOptions, options);
else
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ApplicationScheme;
}
});
// Configure default options for the cookie authentication scheme
// as IOptionsMonitor<CookieAuthenticationOptions>.
_services.ConfigureApplicationCookie((CookieAuthenticationOptions options) =>
{
if (!string.IsNullOrWhiteSpace(cfgAuthenCookieOpt))
_config.Bind(cfgAuthenCookieOpt, options);
options.Events = new MyAuthenticationEvent();
});
return b;
}
#endregion
#region JWT
/// <summary>
/// Configure JWT Authenticaiton Scheme
/// </summary>
public virtual AuthenticationBuilder AddMyAuthenticationJwtBearer(
AuthenticationBuilder builder,
string cfgJwtOptions,
string? schemeName = null)
{
_services.Configure<JwtConfig>(_config.GetSection(cfgJwtOptions));
JwtConfig cfg = new();
_config.Bind(cfgJwtOptions, cfg);
TokenValidationParameters validation = JwtUtil.GetValidatOption(cfg);
string scheme = schemeName ?? JwtBearerDefaults.AuthenticationScheme;
var b = builder.AddJwtBearer(scheme, (JwtBearerOptions options) =>
{
options.TokenValidationParameters = validation;
});
return b;
}
#endregion
#region Cookie
/// <summary>
/// Configure Cookie Authentication Scheme
/// </summary>
public virtual AuthenticationBuilder AddMyAuthenticationCookie(
AuthenticationBuilder builder,
string? schemeName = null)
{
string scheme = schemeName ?? CookieAuthenticationDefaults.AuthenticationScheme;
var b = builder.AddCookie(scheme, (CookieAuthenticationOptions options) =>
{
});
return b;
}
#endregion
public virtual void AddMyAuthorization(string? cfgAuthorOptions = null)
{
MyAuthorizationService.ConfigureServices(_services, _config);
_services.AddAuthorization((AuthorizationOptions options) =>
{
if (!string.IsNullOrWhiteSpace(cfgAuthorOptions))
_config.Bind(cfgAuthorOptions, options);
// add authorization policies
MyAuthorizationService.ConfigureAuthorizationOptions(options);
});
}
/// <summary>
/// Registers examples
/// </summary>
public virtual void AddMyAuthExamples()
{
_services.AddScoped<ExploreIdentity>();
_services.AddScoped<ExploreAuthentication>();
_services.AddScoped<ExploreAuthorization>();
}
/// <summary>
/// Registers a custom implementation of
/// IHttpContextAccessor for unit testing purposes.
/// </summary>
public virtual void AddMyTestHttpContextAccessor()
{
_services.AddSingleton<IHttpContextAccessor, MyHttpContextAccessor>();
}
public virtual void AddMyAuthMiddlewares(IApplicationBuilder builder)
{
builder.UseAuthentication();
builder.UseAuthorization();
}
}
}