-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathProgram.cs
More file actions
86 lines (82 loc) · 3.6 KB
/
Program.cs
File metadata and controls
86 lines (82 loc) · 3.6 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
using BusinessObjectsLibrary.BusinessObjects;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using MvcApplication;
using Newtonsoft.Json.Serialization;
using Microsoft.EntityFrameworkCore;
using DevExpress.ExpressApp.DC;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
string loginPath = "/Authentication";
Action<MvcNewtonsoftJsonOptions> JsonOptions =
options => {
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
};
builder.Services.AddControllersWithViews()
.AddNewtonsoftJson(JsonOptions);
builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options => {
options.LoginPath = loginPath;
});
builder.Services.AddDbContextFactory<ApplicationDbContext>((serviceProvider, options) => {
string connectionString = builder.Configuration.GetConnectionString("ConnectionString");
options.UseSqlServer(connectionString);
options.UseLazyLoadingProxies();
options.UseChangeTrackingProxies();
ITypesInfo typesInfo = serviceProvider.GetRequiredService<ITypesInfo>();
options.UseSecurity(serviceProvider.GetRequiredService<SecurityStrategyComplex>(), typesInfo);
}, ServiceLifetime.Scoped);
builder.Services.AddScoped<SecurityProvider>();
builder.Services.AddScoped((serviceProvider) => {
AuthenticationMixed authentication = new AuthenticationMixed();
authentication.LogonParametersType = typeof(AuthenticationStandardLogonParameters);
authentication.AddAuthenticationStandardProvider(typeof(PermissionPolicyUser));
authentication.AddIdentityAuthenticationProvider(typeof(PermissionPolicyUser));
ITypesInfo typesInfo = serviceProvider.GetRequiredService<ITypesInfo>();
SecurityStrategyComplex security = new SecurityStrategyComplex(typeof(PermissionPolicyUser), typeof(PermissionPolicyRole), authentication, typesInfo);
return security;
});
builder.Services.AddSingleton<ITypesInfo, TypesInfo>();
var app = builder.Build();
if (app.Environment.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
else {
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseAuthentication();
app.UseDefaultFiles();
app.UseHttpsRedirection();
app.UseStaticFiles(new StaticFileOptions() {
OnPrepareResponse = context => {
if (context.Context.User.Identity.IsAuthenticated) {
return;
}
else {
string referer = context.Context.Request.Headers["Referer"].ToString();
string authenticationPagePath = loginPath;
string vendorString = "vendor.css";
if (context.Context.Request.Path.HasValue && context.Context.Request.Path.StartsWithSegments(authenticationPagePath)
|| referer != null && (referer.Contains(authenticationPagePath) || referer.Contains(vendorString))) {
return;
}
context.Context.Response.Redirect(loginPath);
}
}
});
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.UseDemoData<ApplicationDbContext>(app.Configuration.GetConnectionString("ConnectionString"),
(builder, connectionString) =>
builder.UseSqlServer(connectionString).UseChangeTrackingProxies());
app.Run();