-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
97 lines (74 loc) · 3.57 KB
/
Program.cs
File metadata and controls
97 lines (74 loc) · 3.57 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
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using SimpleExampleAuth.Controllers;
using SimpleExampleAuth.Models;
namespace SimpleExampleAuth
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
string connectionCar = builder.Configuration.GetConnectionString("DefaultConnectionCar");
string connectionUser = builder.Configuration.GetConnectionString("DefaultConnectionUser");
builder.Services.AddDbContext<CarContext>(options => options.UseSqlServer(connectionCar));
builder.Services.AddDbContext<UserContext>(options => options.UseSqlServer(connectionUser));
builder.Services.AddTransient<AccountController>();
builder.Services.AddMvc();
builder.Services.AddSingleton<IRedisCacheService, RedisCacheService>();
builder.Services.AddAuthorization(options =>
{
var defaultPolicy = new AuthorizationPolicyBuilder(CookieAuthenticationDefaults.AuthenticationScheme, "JwtBearer")
.RequireAuthenticatedUser()
.Build();
options.DefaultPolicy = defaultPolicy;
});
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddJwtBearer("JwtBearer", options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = JWTAuthOptions.ISSUER,
ValidateAudience = true,
ValidAudience = JWTAuthOptions.AUDIENCE,
ValidateLifetime = true,
IssuerSigningKey = JWTAuthOptions.GetSymmetricSecurityKey(),
ValidateIssuerSigningKey = true,
};
})
.AddGitHub(options =>
{
options.ClientId = builder.Configuration["Authentication:GitHub:ClientId"];
options.ClientSecret = builder.Configuration["Authentication:GitHub:ClientSecret"];
options.CallbackPath = "/signin-github";
});
builder.Services.AddControllersWithViews();
var app = builder.Build();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
using var serviceScope = app.Services.CreateScope();
#region Çàïîëíèòü ÁÄ
//var contextCar = serviceScope.ServiceProvider.GetRequiredService<CarContext>();
//await CarsGenerator.GenerateRandomCarsAsync(contextCar, 1000);
//var contextUser = serviceScope.ServiceProvider.GetRequiredService<UserContext>();
//await UsersGenerator.GenerateRandomUsersAsync(contextUser, 1000);
#endregion
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
var accountController = serviceScope.ServiceProvider.GetRequiredService<AccountController>();
app.MapPost("/Account/LoginAccses", accountController.LoginAccses);
app.Map("/data", [Authorize] () => new { message = "Àâòîðèçàöèÿ ïðîéäåíà!" });
app.Run();
}
}
}