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
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 Microsoft.Extensions.Logging;
using Skoruba.IdentityServer4.Admin.EntityFramework.Shared.DbContexts;
using Skoruba.IdentityServer4.Admin.EntityFramework.Shared.Entities.Identity;
using Skoruba.IdentityServer4.STS.Identity.Configuration.Intefaces;
using Skoruba.IdentityServer4.STS.Identity.Helpers;
namespace Skoruba.IdentityServer4.STS.Identity
{
public class Startup
{
public IConfiguration Configuration { get; }
public IWebHostEnvironment Environment { get; }
public ILogger Logger { get; set; }
public Startup(IWebHostEnvironment environment, ILoggerFactory loggerFactory)
{
var builder = new ConfigurationBuilder()
.SetBasePath(environment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
if (environment.IsDevelopment())
{
builder.AddUserSecrets<Startup>();
}
Configuration = builder.Build();
Environment = environment;
Logger = loggerFactory.CreateLogger<Startup>();
}
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureRootConfiguration(Configuration);
// Register DbContexts for IdentityServer and Identity
services.RegisterDbContexts<AdminIdentityDbContext, IdentityServerConfigurationDbContext, IdentityServerPersistedGrantDbContext>(Environment, Configuration);
// Add email senders which is currently setup for SendGrid and SMTP
services.AddEmailSenders(Configuration);
// Add services for authentication, including Identity model, IdentityServer4 and external providers
services.AddAuthenticationServices<IdentityServerConfigurationDbContext, IdentityServerPersistedGrantDbContext, AdminIdentityDbContext, UserIdentity, UserIdentityRole>(Configuration, Logger);
// 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
var rootConfiguration = services.BuildServiceProvider().GetService<IRootConfiguration>();
services.AddAuthorizationPolicies(rootConfiguration);
services.AddIdSHealthChecks<IdentityServerConfigurationDbContext, IdentityServerPersistedGrantDbContext, AdminIdentityDbContext>(Configuration);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, Microsoft.Extensions.Hosting.IHostApplicationLifetime applicationLifetime)
{
app.AddLogging(loggerFactory, Configuration);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Add custom security headers
app.UseSecurityHeaders();
app.UseStaticFiles();
app.UseIdentityServer();
app.UseMvcLocalizationServices();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoint =>
{
endpoint.MapDefaultControllerRoute();
endpoint.MapHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
});
}
}
}