2017-09-06 18:43:38 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using HealthChecks.UI.Client;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using IdentityServer4.Services;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2017-11-30 11:40:30 +01:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2018-11-14 16:21:50 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-09-13 14:53:06 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Certificates;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Data;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Devspaces;
|
2017-09-13 14:53:06 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Models;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Services;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2019-07-23 12:14:09 +02:00
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-11-30 11:40:30 +01:00
|
|
|
|
using StackExchange.Redis;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using System;
|
2017-06-05 21:54:03 +02:00
|
|
|
|
using System.Reflection;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
2017-09-13 14:53:06 +02:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Identity.API
|
2016-11-24 15:31:33 +01:00
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2017-09-12 13:44:52 +02:00
|
|
|
|
public Startup(IConfiguration configuration)
|
2016-11-24 15:31:33 +01:00
|
|
|
|
{
|
2017-09-12 13:44:52 +02:00
|
|
|
|
Configuration = configuration;
|
2017-08-29 18:11:30 +02:00
|
|
|
|
}
|
2017-08-29 12:12:45 +02:00
|
|
|
|
|
2017-09-12 13:44:52 +02:00
|
|
|
|
public IConfiguration Configuration { get; }
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
2017-05-11 13:44:38 +02:00
|
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
2017-08-29 18:11:30 +02:00
|
|
|
|
{
|
2017-10-13 11:35:26 +02:00
|
|
|
|
RegisterAppInsights(services);
|
2017-10-11 18:53:26 +02:00
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
// Add framework services.
|
|
|
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
2017-10-10 19:56:34 -07:00
|
|
|
|
options.UseSqlServer(Configuration["ConnectionString"],
|
2019-02-18 11:06:46 +00:00
|
|
|
|
sqlServerOptionsAction: sqlOptions =>
|
|
|
|
|
{
|
|
|
|
|
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
|
|
|
|
|
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
|
|
|
|
}));
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
|
|
|
|
services.AddIdentity<ApplicationUser, IdentityRole>()
|
|
|
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
2017-10-08 18:46:05 +01:00
|
|
|
|
.AddDefaultTokenProviders();
|
2016-12-19 10:20:02 +01:00
|
|
|
|
|
|
|
|
|
services.Configure<AppSettings>(Configuration);
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
2018-11-14 16:21:50 +01:00
|
|
|
|
services.AddMvc()
|
|
|
|
|
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
2017-06-05 21:54:03 +02:00
|
|
|
|
|
2017-06-08 17:45:07 +02:00
|
|
|
|
if (Configuration.GetValue<string>("IsClusterEnv") == bool.TrueString)
|
2017-03-15 08:57:01 -07:00
|
|
|
|
{
|
2017-06-08 17:45:07 +02:00
|
|
|
|
services.AddDataProtection(opts =>
|
|
|
|
|
{
|
|
|
|
|
opts.ApplicationDiscriminator = "eshop.identity";
|
|
|
|
|
})
|
2017-11-30 11:40:30 +01:00
|
|
|
|
.PersistKeysToRedis(ConnectionMultiplexer.Connect(Configuration["DPConnectionString"]), "DataProtection-Keys");
|
2017-06-08 17:45:07 +02:00
|
|
|
|
}
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
2018-11-30 17:43:22 +01:00
|
|
|
|
services.AddHealthChecks()
|
2019-01-03 17:11:56 +01:00
|
|
|
|
.AddCheck("self", () => HealthCheckResult.Healthy())
|
2018-11-30 17:43:22 +01:00
|
|
|
|
.AddSqlServer(Configuration["ConnectionString"],
|
|
|
|
|
name: "IdentityDB-check",
|
|
|
|
|
tags: new string[] { "IdentityDB" });
|
2019-07-23 12:14:09 +02:00
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
|
2016-12-29 16:26:02 +01:00
|
|
|
|
services.AddTransient<IRedirectService, RedirectService>();
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
2017-09-08 12:12:49 +02:00
|
|
|
|
var connectionString = Configuration["ConnectionString"];
|
2017-06-05 21:54:03 +02:00
|
|
|
|
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
|
2016-12-07 13:57:31 +01:00
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
// Adds IdentityServer
|
2018-09-07 10:56:38 +02:00
|
|
|
|
services.AddIdentityServer(x =>
|
|
|
|
|
{
|
|
|
|
|
x.IssuerUri = "null";
|
|
|
|
|
x.Authentication.CookieLifetime = TimeSpan.FromHours(2);
|
|
|
|
|
})
|
2019-03-28 18:59:45 +01:00
|
|
|
|
.AddDevspacesIfNeeded(Configuration.GetValue("EnableDevspaces", false))
|
2018-09-07 10:56:38 +02:00
|
|
|
|
.AddSigningCredential(Certificate.Get())
|
|
|
|
|
.AddAspNetIdentity<ApplicationUser>()
|
|
|
|
|
.AddConfigurationStore(options =>
|
|
|
|
|
{
|
|
|
|
|
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
|
2019-02-18 11:06:46 +00:00
|
|
|
|
sqlServerOptionsAction: sqlOptions =>
|
|
|
|
|
{
|
|
|
|
|
sqlOptions.MigrationsAssembly(migrationsAssembly);
|
|
|
|
|
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
|
|
|
|
});
|
2018-09-07 10:56:38 +02:00
|
|
|
|
})
|
|
|
|
|
.AddOperationalStore(options =>
|
2017-09-11 12:43:45 +02:00
|
|
|
|
{
|
2017-10-10 19:56:34 -07:00
|
|
|
|
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
|
2019-02-18 11:06:46 +00:00
|
|
|
|
sqlServerOptionsAction: sqlOptions =>
|
|
|
|
|
{
|
|
|
|
|
sqlOptions.MigrationsAssembly(migrationsAssembly);
|
|
|
|
|
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
|
|
|
|
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
|
|
|
|
|
});
|
2017-09-11 12:43:45 +02:00
|
|
|
|
})
|
2018-09-07 10:56:38 +02:00
|
|
|
|
.Services.AddTransient<IProfileService, ProfileService>();
|
2017-05-11 13:44:38 +02:00
|
|
|
|
|
|
|
|
|
var container = new ContainerBuilder();
|
|
|
|
|
container.Populate(services);
|
2017-09-11 12:43:45 +02:00
|
|
|
|
|
2017-05-11 13:44:38 +02:00
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
2016-11-24 15:31:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
|
|
|
|
{
|
2019-02-18 11:06:46 +00:00
|
|
|
|
//loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
|
|
|
//loggerFactory.AddDebug();
|
|
|
|
|
//loggerFactory.AddAzureWebAppDiagnostics();
|
|
|
|
|
//loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
2017-10-11 16:26:44 +02:00
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
app.UseDatabaseErrorPage();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Home/Error");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 19:18:53 +02:00
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
|
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
|
|
|
{
|
2019-02-22 15:05:28 +00:00
|
|
|
|
loggerFactory.CreateLogger<Startup>().LogDebug("Using PATH BASE '{pathBase}'", pathBase);
|
2017-09-07 19:18:53 +02:00
|
|
|
|
app.UsePathBase(pathBase);
|
2018-01-22 11:46:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 17:43:22 +01:00
|
|
|
|
app.UseHealthChecks("/hc", new HealthCheckOptions()
|
|
|
|
|
{
|
|
|
|
|
Predicate = _ => true,
|
|
|
|
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-03 17:11:56 +01:00
|
|
|
|
app.UseHealthChecks("/liveness", new HealthCheckOptions
|
|
|
|
|
{
|
|
|
|
|
Predicate = r => r.Name.Contains("self")
|
|
|
|
|
});
|
2017-09-07 19:18:53 +02:00
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
2016-12-20 12:22:28 +01:00
|
|
|
|
|
|
|
|
|
// Make work identity server redirections in Edge and lastest versions of browers. WARN: Not valid in a production environment.
|
|
|
|
|
app.Use(async (context, next) =>
|
|
|
|
|
{
|
|
|
|
|
context.Response.Headers.Add("Content-Security-Policy", "script-src 'unsafe-inline'");
|
|
|
|
|
await next();
|
|
|
|
|
});
|
|
|
|
|
|
2018-07-09 14:20:27 +02:00
|
|
|
|
app.UseForwardedHeaders();
|
2016-11-24 15:31:33 +01:00
|
|
|
|
// Adds IdentityServer
|
|
|
|
|
app.UseIdentityServer();
|
2016-11-28 12:58:51 +01:00
|
|
|
|
|
2018-11-14 16:21:50 +01:00
|
|
|
|
app.UseHttpsRedirection();
|
2016-11-24 15:31:33 +01:00
|
|
|
|
app.UseMvc(routes =>
|
|
|
|
|
{
|
|
|
|
|
routes.MapRoute(
|
|
|
|
|
name: "default",
|
|
|
|
|
template: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
});
|
2017-06-05 21:54:03 +02:00
|
|
|
|
}
|
2017-10-13 11:35:26 +02:00
|
|
|
|
|
|
|
|
|
private void RegisterAppInsights(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
services.AddApplicationInsightsTelemetry(Configuration);
|
2019-07-23 12:14:09 +02:00
|
|
|
|
services.AddApplicationInsightsKubernetesEnricher();
|
2017-10-13 11:35:26 +02:00
|
|
|
|
}
|
2016-11-24 15:31:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|