-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
149 lines (121 loc) · 5.53 KB
/
Program.cs
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
using GameStoresBlazor.Models.User;
using GameStoresBlazor.Services.DataBase;
using GameStoresBlazor.Services.Transactions;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System.Security.Claims;
using System.Text.RegularExpressions;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddBootstrapBlazor();
builder.Services.AddServerSideBlazor();
builder.Services.AddControllersWithViews();
builder.Logging.AddFilter("Microsoft.AspNetCore.Authentication", LogLevel.Debug);
builder.Services.AddAuthorization();
builder.Services.AddAuthentication()
.AddSteam(options =>
{
options.ApplicationKey = "";
options.CallbackPath = new PathString("/signin-steam");
options.Events = new AspNet.Security.OpenId.OpenIdAuthenticationEvents
{
OnAuthenticated = async context =>
{
// ��������� �������������� ������������ Steam
var steamId = context.Identity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (steamId == null)
{
// ������ ��������� ������
throw new Exception("������ ��� �������������� ����� Steam");
}
//context.Identity.Name
var normalizeSteamId = Regex.Replace(steamId, @"\D", "");
// ������ ������ ��� �������� ������������ � ����� �������
var userManager = context.HttpContext.RequestServices.GetService<UserManager<SteamIdentityUserModel>>();
var signInManager = context.HttpContext.RequestServices.GetService<SignInManager<SteamIdentityUserModel>>();
var user = await userManager.FindByLoginAsync("Steam", normalizeSteamId);
if (user == null)
{
user = new SteamIdentityUserModel { SteamId = normalizeSteamId, Name = context.Identity.Name };
var createResult = await userManager.CreateAsync(user);
if (!createResult.Succeeded)
{
throw new Exception("�� ������� ������� ������������");
}
var addLoginResult = await userManager.AddLoginAsync(user, new UserLoginInfo("Steam", normalizeSteamId, "Steam"));
if (!addLoginResult.Succeeded)
{
throw new Exception("�� ������� �������� ���� Steam");
}
}
await signInManager.SignInAsync(user, isPersistent: false);
},
};
});
builder.Services.AddDbContext<DataBaseContext>(options =>
{
options.UseNpgsql(builder.Configuration.GetConnectionString("UsersDbConnection"), npgsqlOptionsAction: sqlOptions =>
{
sqlOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(10),
errorCodesToAdd: null);
});
}, ServiceLifetime.Transient);
builder.Services.AddScoped<TransactionService>();
//builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<DataBaseContext>();
//builder.Services.AddIdentity<SteamIdentityUserModel, IdentityRole>()
// .AddEntityFrameworkStores<DataBaseContext>()
// .AddDefaultTokenProviders();
builder.Services.AddDefaultIdentity<SteamIdentityUserModel>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 5;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.SignIn.RequireConfirmedAccount = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;
})
.AddRoles<IdentityRole>()
//.AddDefaultTokenProviders()
.AddEntityFrameworkStores<DataBaseContext>();
builder.Services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromHours(36);
options.LoginPath = "/auth/steam"; // Укажите здесь ваш путь к странице входа
options.AccessDeniedPath = "/auth/steam"; // Укажите здесь ваш путь к странице "Доступ запрещен"
options.SlidingExpiration = true;
});
builder.Services.Configure<SecurityStampValidatorOptions>(options =>
{
options.ValidationInterval = TimeSpan.Zero;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.MapRazorPages();
app.Run();