This repository was archived by the owner on Feb 2, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathStartup.cs
More file actions
135 lines (112 loc) · 5.33 KB
/
Copy pathStartup.cs
File metadata and controls
135 lines (112 loc) · 5.33 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
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Skoruba.IdentityServer4.Admin.EntityFramework.Shared.DbContexts;
using Skoruba.IdentityServer4.Admin.EntityFramework.Shared.Entities.Identity;
using Skoruba.IdentityServer4.STS.Identity.Configuration;
using Skoruba.IdentityServer4.STS.Identity.Configuration.Constants;
using Skoruba.IdentityServer4.STS.Identity.Configuration.Interfaces;
using Skoruba.IdentityServer4.STS.Identity.Helpers;
using System;
using Skoruba.IdentityServer4.Shared.Configuration.Helpers;
namespace Skoruba.IdentityServer4.STS.Identity
{
public class Startup
{
public IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
public Startup(IWebHostEnvironment environment, IConfiguration configuration)
{
Configuration = configuration;
Environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
var rootConfiguration = CreateRootConfiguration();
services.AddSingleton(rootConfiguration);
// Register DbContexts for IdentityServer and Identity
RegisterDbContexts(services);
// Save data protection keys to db, using a common application name shared between Admin and STS
services.AddDataProtection<IdentityServerDataProtectionDbContext>(Configuration);
// Add email senders which is currently setup for SendGrid and SMTP
services.AddEmailSenders(Configuration);
// Add services for authentication, including Identity model and external providers
RegisterAuthentication(services);
// Add HSTS options
RegisterHstsOptions(services);
// Add all dependencies for Asp.Net Core Identity in MVC - these dependencies are injected into generic Controllers
// Including settings for MVC and Localization
// If you want to change primary keys or use another db model for Asp.Net Core Identity:
services.AddMvcWithLocalization<UserIdentity, string>(Configuration);
// Add authorization policies for MVC
RegisterAuthorization(services);
services.AddIdSHealthChecks<IdentityServerConfigurationDbContext, IdentityServerPersistedGrantDbContext, AdminIdentityDbContext, IdentityServerDataProtectionDbContext>(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCookiePolicy();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UsePathBase(Configuration.GetValue<string>("BasePath"));
app.UseStaticFiles();
UseAuthentication(app);
// Add custom security headers
app.UseSecurityHeaders(Configuration);
app.UseMvcLocalizationServices();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoint =>
{
endpoint.MapDefaultControllerRoute();
endpoint.MapHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
});
}
public virtual void RegisterDbContexts(IServiceCollection services)
{
services.RegisterDbContexts<AdminIdentityDbContext, IdentityServerConfigurationDbContext, IdentityServerPersistedGrantDbContext, IdentityServerDataProtectionDbContext>(Configuration);
}
public virtual void RegisterAuthentication(IServiceCollection services)
{
services.AddAuthenticationServices<AdminIdentityDbContext, UserIdentity, UserIdentityRole>(Configuration);
services.AddIdentityServer<IdentityServerConfigurationDbContext, IdentityServerPersistedGrantDbContext, UserIdentity>(Environment, Configuration);
}
public virtual void RegisterAuthorization(IServiceCollection services)
{
var rootConfiguration = CreateRootConfiguration();
services.AddAuthorizationPolicies(rootConfiguration);
}
public virtual void UseAuthentication(IApplicationBuilder app)
{
app.UseIdentityServer();
}
public virtual void RegisterHstsOptions(IServiceCollection services)
{
services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(365);
});
}
protected IRootConfiguration CreateRootConfiguration()
{
var rootConfiguration = new RootConfiguration();
Configuration.GetSection(ConfigurationConsts.AdminConfigurationKey).Bind(rootConfiguration.AdminConfiguration);
Configuration.GetSection(ConfigurationConsts.RegisterConfigurationKey).Bind(rootConfiguration.RegisterConfiguration);
return rootConfiguration;
}
}
}