Code
·
50 lines
·
1513 bytes
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
50using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc.Razor;
namespace LoginDing.Extensions;
public static class DefaultExtensions
{
// Adds SMTP server services to the specified IServiceCollection
public static IServiceCollection AddRazorUnderRoot(this IServiceCollection services)
{
services.AddRazorPages(o => o.RootDirectory = "/wwwroot");
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationFormats.Clear();
options.ViewLocationFormats.Add("/wwwroot/{0}.cshtml");
options.ViewLocationFormats.Add("/wwwroot/MasterPages/{0}.cshtml");
options.PageViewLocationFormats.Clear();
options.PageViewLocationFormats.Add("/wwwroot/{0}.cshtml");
options.PageViewLocationFormats.Add("/wwwroot/MasterPages/{0}.cshtml");
});
return services;
}
public static IServiceCollection AddAuthenticationAndAddAuthorization(this IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.SameSite = SameSiteMode.Strict;
});
services.AddAuthorization();
return services;
}
public static IApplicationBuilder UseAuthenticationAndAddAuthorization(this IApplicationBuilder app)
{
app.UseAuthentication();
app.UseAuthorization();
return app;
}
}