forked from Soluto/oidc-server-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
103 lines (84 loc) · 3.3 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
using Duende.IdentityServer.Hosting;
using Microsoft.Extensions.FileProviders;
using OpenIdConnectServer;
using OpenIdConnectServer.Helpers;
using OpenIdConnectServer.JsonConverters;
using OpenIdConnectServer.Middlewares;
using OpenIdConnectServer.Services;
using OpenIdConnectServer.Validation;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}", theme: AnsiConsoleTheme.Code)
.CreateLogger();
var builder = WebApplication.CreateBuilder(args);
// Configure Serilog
builder.Host.UseSerilog();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services
.AddControllersWithViews()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new ClaimJsonConverter());
});
builder.Services
.AddIdentityServer(options =>
{
var configuredOptions = Config.GetServerOptions();
MergeHelper.Merge(configuredOptions, options);
})
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryApiScopes(Config.GetApiScopes())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetUsers())
.AddRedirectUriValidator<RedirectUriValidator>()
.AddProfileService<ProfileService>()
.AddCorsPolicyService<CorsPolicyService>();
var app = builder.Build();
var aspNetServicesOptions = Config.GetAspNetServicesOptions();
AspNetServicesHelper.ConfigureAspNetServices(builder.Services, aspNetServicesOptions);
AspNetServicesHelper.UseAspNetServices(app, aspNetServicesOptions);
Config.ConfigureOptions<IdentityServerHost.Pages.Login.LoginOptions>("LOGIN");
Config.ConfigureOptions<IdentityServerHost.Pages.Logout.LogoutOptions>("LOGOUT");
app.UseDeveloperExceptionPage();
var corsOptions = Config.GetServerCorsAllowedOrigins();
app.UseCors(builder => builder
.WithOrigins(corsOptions.ToArray())
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseIdentityServer();
var basePath = Config.GetAspNetServicesOptions().BasePath;
if (!string.IsNullOrEmpty(basePath))
{
app.UseWhen(ctx => ctx.Request.Path.StartsWithSegments(basePath), appBuilder =>
{
appBuilder.UseMiddleware<BasePathMiddleware>();
appBuilder.UseMiddleware<BaseAuthMiddleWare>();
appBuilder.UseMiddleware<IdentityServerMiddleware>();
});
}
app.UseHttpsRedirection();
var manifestEmbeddedProvider = new ManifestEmbeddedFileProvider(typeof(Program).Assembly, "wwwroot");
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = manifestEmbeddedProvider
});
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
});
app.MapRazorPages();
app.Run();