# Conflicts SOLVED: # .gitignore # src/BuildingBlocks/WebHostCustomization/WebHost.Customization/WebHostExtensions.cs # src/Services/Basket/Basket.API/Basket.API.csproj # src/Services/Basket/Basket.API/Program.cs # src/Services/Basket/Basket.API/Startup.cs # src/Services/Catalog/Catalog.API/Catalog.API.csproj # src/Services/Catalog/Catalog.API/Program.cs # src/Services/Catalog/Catalog.API/Startup.cs # src/Services/Identity/Identity.API/Identity.API.csproj # src/Services/Identity/Identity.API/Program.cs # src/Services/Identity/Identity.API/Startup.cs # src/Services/Location/Locations.API/Locations.API.csproj # src/Services/Location/Locations.API/Program.cs # src/Services/Location/Locations.API/Startup.cs # src/Services/Marketing/Marketing.API/Marketing.API.csproj # src/Services/Marketing/Marketing.API/Program.cs # src/Services/Marketing/Marketing.API/Startup.cs # src/Services/Ordering/Ordering.API/Ordering.API.csproj # src/Services/Ordering/Ordering.API/Program.cs # src/Services/Ordering/Ordering.API/Startup.cs # src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj # src/Services/Ordering/Ordering.SignalrHub/Program.cs # src/Services/Ordering/Ordering.SignalrHub/Startup.cs # src/Services/Payment/Payment.API/Payment.API.csproj # src/Services/Payment/Payment.API/Program.cs # src/Services/Payment/Payment.API/Startup.cs # src/Web/WebMVC/Program.cs # src/Web/WebMVC/Startup.cs # src/Web/WebStatus/Program.cs # src/Web/WebStatus/Startup.cs # src/Web/WebStatus/WebStatus.csproj
196 lines
8.1 KiB
C#
196 lines
8.1 KiB
C#
using Autofac;
|
|
using Autofac.Extensions.DependencyInjection;
|
|
using IdentityServer4.Services;
|
|
using Microsoft.ApplicationInsights.Extensibility;
|
|
using Microsoft.ApplicationInsights.ServiceFabric;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Certificates;
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Data;
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Models;
|
|
using Microsoft.eShopOnContainers.Services.Identity.API.Services;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Reflection;
|
|
using HealthChecks.UI.Client;
|
|
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Identity.API
|
|
{
|
|
public class Startup
|
|
{
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
|
{
|
|
RegisterAppInsights(services);
|
|
|
|
// Add framework services.
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseSqlServer(Configuration["ConnectionString"],
|
|
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);
|
|
}));
|
|
|
|
services.AddIdentity<ApplicationUser, IdentityRole>()
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
services.Configure<AppSettings>(Configuration);
|
|
|
|
services.AddMvc()
|
|
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
|
|
|
|
if (Configuration.GetValue<string>("IsClusterEnv") == bool.TrueString)
|
|
{
|
|
services.AddDataProtection(opts =>
|
|
{
|
|
opts.ApplicationDiscriminator = "eshop.identity";
|
|
})
|
|
.PersistKeysToRedis(ConnectionMultiplexer.Connect(Configuration["DPConnectionString"]), "DataProtection-Keys");
|
|
}
|
|
|
|
services.AddHealthChecks()
|
|
.AddCheck("self", () => HealthCheckResult.Healthy())
|
|
.AddSqlServer(Configuration["ConnectionString"],
|
|
name: "IdentityDB-check",
|
|
tags: new string[] { "IdentityDB" });
|
|
|
|
services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
|
|
services.AddTransient<IRedirectService, RedirectService>();
|
|
|
|
var connectionString = Configuration["ConnectionString"];
|
|
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
|
|
|
|
// Adds IdentityServer
|
|
services.AddIdentityServer(x =>
|
|
{
|
|
x.IssuerUri = "null";
|
|
x.Authentication.CookieLifetime = TimeSpan.FromHours(2);
|
|
})
|
|
.AddSigningCredential(Certificate.Get())
|
|
.AddAspNetIdentity<ApplicationUser>()
|
|
.AddConfigurationStore(options =>
|
|
{
|
|
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
|
|
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);
|
|
});
|
|
})
|
|
.AddOperationalStore(options =>
|
|
{
|
|
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
|
|
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);
|
|
});
|
|
})
|
|
.Services.AddTransient<IProfileService, ProfileService>();
|
|
|
|
var container = new ContainerBuilder();
|
|
container.Populate(services);
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
//loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
//loggerFactory.AddDebug();
|
|
//loggerFactory.AddAzureWebAppDiagnostics();
|
|
//loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
|
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
app.UseDatabaseErrorPage();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Home/Error");
|
|
}
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
{
|
|
loggerFactory.CreateLogger<Startup>().LogDebug("Using PATH BASE '{pathBase}'", pathBase);
|
|
app.UsePathBase(pathBase);
|
|
}
|
|
|
|
app.UseHealthChecks("/hc", new HealthCheckOptions()
|
|
{
|
|
Predicate = _ => true,
|
|
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
|
|
});
|
|
|
|
app.UseHealthChecks("/liveness", new HealthCheckOptions
|
|
{
|
|
Predicate = r => r.Name.Contains("self")
|
|
});
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
// 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();
|
|
});
|
|
|
|
app.UseForwardedHeaders();
|
|
// Adds IdentityServer
|
|
app.UseIdentityServer();
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseMvc(routes =>
|
|
{
|
|
routes.MapRoute(
|
|
name: "default",
|
|
template: "{controller=Home}/{action=Index}/{id?}");
|
|
});
|
|
}
|
|
|
|
private void RegisterAppInsights(IServiceCollection services)
|
|
{
|
|
services.AddApplicationInsightsTelemetry(Configuration);
|
|
var orchestratorType = Configuration.GetValue<string>("OrchestratorType");
|
|
|
|
if (orchestratorType?.ToUpper() == "K8S")
|
|
{
|
|
// Enable K8s telemetry initializer
|
|
services.AddApplicationInsightsKubernetesEnricher();
|
|
}
|
|
if (orchestratorType?.ToUpper() == "SF")
|
|
{
|
|
// Enable SF telemetry initializer
|
|
services.AddSingleton<ITelemetryInitializer>((serviceProvider) =>
|
|
new FabricTelemetryInitializer());
|
|
}
|
|
}
|
|
}
|
|
}
|