-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
171 lines (126 loc) · 5.74 KB
/
Copy pathProgram.cs
File metadata and controls
171 lines (126 loc) · 5.74 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
using Microsoft.Extensions.FileProviders;
using SlowDown.Hubs;
using SlowDown.Services;
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.Localization;
using System.Globalization;
using Azure.Security.KeyVault.Secrets;
using Azure.Identity;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.Cookies;
namespace SlowDown
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseWebRoot("wwwroot");
builder.Services.AddControllers();
builder.Services.AddSignalR();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromHours(4);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});
builder.Services.AddAuthentication("AdminCookie")
.AddCookie("AdminCookie", options =>
{
options.LoginPath = "/Opettajalogin";
options.AccessDeniedPath = "/Opettajalogin";
options.Cookie.HttpOnly = true;
options.Cookie.SameSite = SameSiteMode.Strict;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy =>
policy.RequireClaim("IsAdmin", "true"));
});
builder.Services.AddSingleton<SessionService>();
builder.Services.AddHostedService<SessionCleanupService>();
builder.Services.AddSingleton<SlowDownService>();
builder.Services.AddSingleton<IAdminPasswordStore, KeyVaultAdminPasswordStore>();
builder.Services.AddSingleton<IAdminPasswordService, AdminPasswordService>();
builder.Services.AddHttpClient();
builder.Services.AddHttpClient("backend", (sp, client) =>
{
var cfg = sp.GetRequiredService<IConfiguration>();
var baseUrl = cfg["AppBaseUrl"];
if (string.IsNullOrWhiteSpace(baseUrl))
throw new InvalidOperationException("AppBaseUrl missing.");
client.BaseAddress = new Uri(baseUrl);
});
var blobConnectionString = builder.Configuration["Azure:BlobConnectionString"];
if (string.IsNullOrWhiteSpace(blobConnectionString))
throw new InvalidOperationException("Azure:BlobConnectionString missing.");
builder.Services.AddSingleton(new BlobServiceClient(blobConnectionString));
var blobContainerName = builder.Configuration["Azure:BlobContainerName"];
if (string.IsNullOrWhiteSpace(blobContainerName))
throw new InvalidOperationException("Azure:BlobContainerName missing.");
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
builder.Services
.AddRazorPages()
.AddViewLocalization()
.AddDataAnnotationsLocalization();
var app = builder.Build();
var supportedCultures = new[]
{
new CultureInfo("fi"),
new CultureInfo("sv"),
new CultureInfo("en"),
new CultureInfo("ru"),
new CultureInfo("uk")
};
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fi"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
localizationOptions.RequestCultureProviders = new IRequestCultureProvider[]
{
new CookieRequestCultureProvider(),
new QueryStringRequestCultureProvider(),
new AcceptLanguageHeaderRequestCultureProvider()
};
app.Lifetime.ApplicationStarted.Register(async () =>
{
try
{
var svc = app.Services.GetRequiredService<BlobServiceClient>();
var container = svc.GetBlobContainerClient(blobContainerName);
await container.CreateIfNotExistsAsync();
Console.WriteLine($"Blob Storage OK, container '{blobContainerName}' käytössä.");
}
catch (Exception ex)
{
Console.WriteLine($"Blob Storage virhe: {ex.Message}");
}
});
app.UseHttpsRedirection();
var webRoot = Path.Combine(app.Environment.ContentRootPath, "wwwroot");
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(webRoot),
RequestPath = ""
});
app.UseRequestLocalization(localizationOptions);
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.MapRazorPages();
app.MapControllers();
app.MapHub<ClassHub>("/hub", options =>
{
options.Transports = HttpTransportType.LongPolling;
});
app.Run();
}
}
}