-
Notifications
You must be signed in to change notification settings - Fork 80
/
Program.cs
89 lines (86 loc) · 3.59 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
using BusinessObjectsLibrary.BusinessObjects;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.DC.Xpo;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.BaseImpl.PermissionPolicy;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
string loginPath = "/Authentication";
JsonResolver resolver = new JsonResolver();
Action<MvcNewtonsoftJsonOptions> JsonOptions =
options => {
options.SerializerSettings.ContractResolver = resolver;
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.AddSingleton<IXpoDataStoreProvider>((serviceProvider) => {
string connectionString = builder.Configuration.GetConnectionString("ConnectionString");
IXpoDataStoreProvider dataStoreProvider = XPObjectSpaceProvider.GetDataStoreProvider(connectionString, null, true);
return dataStoreProvider;
});
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);
security.RegisterXPOAdapterProviders();
return security;
});
builder.Services.AddSingleton<ITypesInfo>((serviceProvider) => {
TypesInfo typesInfo = new TypesInfo();
RegisterEntities(typesInfo);
return 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();
app.Run();
static void RegisterEntities(TypesInfo typesInfo) {
typesInfo.GetOrAddEntityStore(ti => new XpoTypeInfoSource(ti));
typesInfo.RegisterEntity(typeof(Employee));
typesInfo.RegisterEntity(typeof(PermissionPolicyUser));
typesInfo.RegisterEntity(typeof(PermissionPolicyRole));
}