2017-10-13 11:35:26 +02:00
|
|
|
|
using Microsoft.ApplicationInsights.Extensibility;
|
|
|
|
|
using Microsoft.ApplicationInsights.ServiceFabric;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2017-06-21 19:03:48 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
2017-11-30 11:40:30 +01:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.eShopOnContainers.WebMVC.Services;
|
|
|
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Microsoft.Extensions.HealthChecks;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-05-16 12:44:32 +02:00
|
|
|
|
using Polly;
|
2018-05-16 20:30:05 -07:00
|
|
|
|
using Polly.Extensions.Http;
|
2017-11-30 11:40:30 +01:00
|
|
|
|
using StackExchange.Redis;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using System;
|
2016-11-28 12:58:51 +01:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2017-06-20 12:54:32 -07:00
|
|
|
|
using WebMVC.Infrastructure;
|
2017-10-23 17:39:45 +02:00
|
|
|
|
using WebMVC.Infrastructure.Middlewares;
|
2017-09-15 13:54:48 +02:00
|
|
|
|
using WebMVC.Services;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
|
2016-09-07 13:52:26 -07:00
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2017-08-29 11:40:23 +02:00
|
|
|
|
public Startup(IConfiguration configuration)
|
2016-09-06 17:09:19 -07:00
|
|
|
|
{
|
2017-08-29 11:40:23 +02:00
|
|
|
|
Configuration = configuration;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-29 11:40:23 +02:00
|
|
|
|
public IConfiguration Configuration { get; }
|
2016-09-06 17:09:19 -07:00
|
|
|
|
|
2018-05-16 20:30:05 -07:00
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the IoC container.
|
2016-09-06 17:09:19 -07:00
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2018-05-16 12:44:32 +02:00
|
|
|
|
services.AddAppInsight(Configuration)
|
2018-05-21 18:11:27 -07:00
|
|
|
|
.AddHealthChecks(Configuration)
|
|
|
|
|
.AddCustomMvc(Configuration)
|
|
|
|
|
.AddHttpClientServices(Configuration)
|
|
|
|
|
//.AddHttpClientLogging(Configuration) //Opt-in HttpClientLogging config
|
|
|
|
|
.AddCustomAuthentication(Configuration);
|
2018-05-16 12:44:32 +02:00
|
|
|
|
}
|
2017-10-11 18:53:26 +02:00
|
|
|
|
|
2018-05-16 12:44:32 +02: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)
|
|
|
|
|
{
|
|
|
|
|
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
2017-11-30 11:40:30 +01:00
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
loggerFactory.AddAzureWebAppDiagnostics();
|
|
|
|
|
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
|
2017-06-05 21:54:03 +02:00
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
if (env.IsDevelopment())
|
2017-03-15 08:57:01 -07:00
|
|
|
|
{
|
2018-05-16 12:44:32 +02:00
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Error");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
|
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
|
|
|
{
|
|
|
|
|
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
|
|
|
|
|
app.UsePathBase(pathBase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
|
|
|
app.Map("/liveness", lapp => lapp.Run(async ctx => ctx.Response.StatusCode = 200));
|
|
|
|
|
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
|
|
|
|
|
|
|
|
|
app.UseSession();
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
|
|
if (Configuration.GetValue<bool>("UseLoadTest"))
|
|
|
|
|
{
|
|
|
|
|
app.UseMiddleware<ByPassAuthMiddleware>();
|
2017-06-08 17:45:07 +02:00
|
|
|
|
}
|
2017-03-15 08:57:01 -07:00
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
|
|
|
|
var log = loggerFactory.CreateLogger("identity");
|
|
|
|
|
|
|
|
|
|
WebContextSeed.Seed(app, env, loggerFactory);
|
2017-03-28 16:16:01 +02:00
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
app.UseMvc(routes =>
|
|
|
|
|
{
|
|
|
|
|
routes.MapRoute(
|
|
|
|
|
name: "default",
|
|
|
|
|
template: "{controller=Catalog}/{action=Index}/{id?}");
|
|
|
|
|
|
|
|
|
|
routes.MapRoute(
|
|
|
|
|
name: "defaultError",
|
|
|
|
|
template: "{controller=Error}/{action=Error}");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class ServiceCollectionExtensions
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddAppInsight(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
services.AddApplicationInsightsTelemetry(configuration);
|
|
|
|
|
var orchestratorType = configuration.GetValue<string>("OrchestratorType");
|
|
|
|
|
|
|
|
|
|
if (orchestratorType?.ToUpper() == "K8S")
|
|
|
|
|
{
|
|
|
|
|
// Enable K8s telemetry initializer
|
|
|
|
|
services.EnableKubernetes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (orchestratorType?.ToUpper() == "SF")
|
|
|
|
|
{
|
|
|
|
|
// Enable SF telemetry initializer
|
|
|
|
|
services.AddSingleton<ITelemetryInitializer>((serviceProvider) =>
|
|
|
|
|
new FabricTelemetryInitializer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
2017-03-28 16:16:01 +02:00
|
|
|
|
services.AddHealthChecks(checks =>
|
|
|
|
|
{
|
2017-05-09 13:54:45 +02:00
|
|
|
|
var minutes = 1;
|
2018-05-16 12:44:32 +02:00
|
|
|
|
if (int.TryParse(configuration["HealthCheck:Timeout"], out var minutesParsed))
|
2017-05-09 13:54:45 +02:00
|
|
|
|
{
|
|
|
|
|
minutes = minutesParsed;
|
|
|
|
|
}
|
2017-09-29 11:45:25 +02:00
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
checks.AddUrlCheck(configuration["CatalogUrlHC"], TimeSpan.FromMinutes(minutes));
|
|
|
|
|
checks.AddUrlCheck(configuration["OrderingUrlHC"], TimeSpan.FromMinutes(minutes));
|
|
|
|
|
checks.AddUrlCheck(configuration["BasketUrlHC"], TimeSpan.Zero); //No cache for this HealthCheck, better just for demos
|
|
|
|
|
checks.AddUrlCheck(configuration["IdentityUrlHC"], TimeSpan.FromMinutes(minutes));
|
|
|
|
|
checks.AddUrlCheck(configuration["MarketingUrlHC"], TimeSpan.FromMinutes(minutes));
|
2017-03-28 16:16:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddCustomMvc(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
2018-05-18 12:36:33 +02:00
|
|
|
|
services.AddOptions();
|
|
|
|
|
services.Configure<AppSettings>(configuration);
|
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
services.AddMvc();
|
|
|
|
|
|
|
|
|
|
services.AddSession();
|
|
|
|
|
|
|
|
|
|
if (configuration.GetValue<string>("IsClusterEnv") == bool.TrueString)
|
|
|
|
|
{
|
|
|
|
|
services.AddDataProtection(opts =>
|
|
|
|
|
{
|
|
|
|
|
opts.ApplicationDiscriminator = "eshop.webmvc";
|
|
|
|
|
})
|
|
|
|
|
.PersistKeysToRedis(ConnectionMultiplexer.Connect(configuration["DPConnectionString"]), "DataProtection-Keys");
|
|
|
|
|
}
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 20:30:05 -07:00
|
|
|
|
// Adds all Http client services (like Service-Agents) using resilient Http requests based on HttpClient factory and Polly's policies
|
|
|
|
|
public static IServiceCollection AddHttpClientServices(this IServiceCollection services, IConfiguration configuration)
|
2018-05-16 12:44:32 +02:00
|
|
|
|
{
|
2017-05-09 13:54:45 +02:00
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
2018-05-16 20:30:05 -07:00
|
|
|
|
|
|
|
|
|
//Using fluent client configuration of Polly policies
|
|
|
|
|
//(CDLTLL) Instead of hardcoded values, use: configuration["HttpClientMaxNumberRetries"], configuration["SecondsBaseForExponentialBackoff"]
|
|
|
|
|
|
2018-05-18 12:36:33 +02:00
|
|
|
|
var retriesWithExponentialBackoff = HttpPolicyExtensions
|
|
|
|
|
.HandleTransientHttpError()
|
|
|
|
|
.WaitAndRetryAsync(7,retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
|
|
|
|
|
|
2018-05-16 20:30:05 -07:00
|
|
|
|
var circuitBreaker = HttpPolicyExtensions
|
2018-05-18 12:36:33 +02:00
|
|
|
|
.HandleTransientHttpError()
|
|
|
|
|
.CircuitBreakerAsync(6, TimeSpan.FromSeconds(30));
|
2018-05-16 20:30:05 -07:00
|
|
|
|
|
2018-05-18 12:36:33 +02:00
|
|
|
|
//register delegating handlers
|
2018-05-16 20:30:05 -07:00
|
|
|
|
services.AddTransient<HttpClientAuthorizationDelegatingHandler>();
|
2018-05-18 12:36:33 +02:00
|
|
|
|
services.AddTransient<HttpClientRequestIdDelegatingHandler>();
|
2018-05-16 20:30:05 -07:00
|
|
|
|
|
2018-05-21 17:04:18 -07:00
|
|
|
|
services.AddHttpClient("extendedhandlerlifetime").SetHandlerLifetime(TimeSpan.FromMinutes(5));
|
|
|
|
|
|
|
|
|
|
//add http client services
|
2018-05-16 12:44:32 +02:00
|
|
|
|
services.AddHttpClient<IBasketService, BasketService>()
|
2018-05-21 17:04:18 -07:00
|
|
|
|
.SetHandlerLifetime(TimeSpan.FromMinutes(5)) //Sample. Default lifetime is 2 minutes
|
2018-05-21 20:06:39 -07:00
|
|
|
|
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
|
2018-05-18 12:36:33 +02:00
|
|
|
|
.AddPolicyHandler(retriesWithExponentialBackoff)
|
|
|
|
|
.AddPolicyHandler(circuitBreaker);
|
|
|
|
|
|
|
|
|
|
services.AddHttpClient<ICatalogService, CatalogService>()
|
|
|
|
|
.AddPolicyHandler(retriesWithExponentialBackoff)
|
|
|
|
|
.AddPolicyHandler(circuitBreaker);
|
|
|
|
|
|
|
|
|
|
services.AddHttpClient<IOrderingService, OrderingService>()
|
|
|
|
|
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
|
|
|
|
|
.AddHttpMessageHandler<HttpClientRequestIdDelegatingHandler>()
|
|
|
|
|
.AddPolicyHandler(retriesWithExponentialBackoff)
|
|
|
|
|
.AddPolicyHandler(circuitBreaker);
|
|
|
|
|
|
|
|
|
|
services.AddHttpClient<ICampaignService, CampaignService>()
|
|
|
|
|
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
|
|
|
|
|
.AddPolicyHandler(retriesWithExponentialBackoff)
|
|
|
|
|
.AddPolicyHandler(circuitBreaker);
|
|
|
|
|
|
|
|
|
|
services.AddHttpClient<ILocationService, LocationService>()
|
|
|
|
|
.AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>()
|
|
|
|
|
.AddPolicyHandler(retriesWithExponentialBackoff)
|
|
|
|
|
.AddPolicyHandler(circuitBreaker);
|
|
|
|
|
|
|
|
|
|
//add custom application services
|
2016-11-28 12:58:51 +01:00
|
|
|
|
services.AddTransient<IIdentityParser<ApplicationUser>, IdentityParser>();
|
2018-05-18 12:36:33 +02:00
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
return services;
|
|
|
|
|
}
|
2018-05-21 18:11:27 -07:00
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddHttpClientLogging(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
services.AddLogging(b =>
|
|
|
|
|
{
|
|
|
|
|
b.AddFilter((category, level) => true); // Spam the world with logs.
|
|
|
|
|
|
|
|
|
|
// Add console logger so we can see all the logging produced by the client by default.
|
|
|
|
|
b.AddConsole(c => c.IncludeScopes = true);
|
|
|
|
|
|
|
|
|
|
// Add console logger
|
|
|
|
|
b.AddDebug();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
var useLoadTest = configuration.GetValue<bool>("UseLoadTest");
|
|
|
|
|
var identityUrl = configuration.GetValue<string>("IdentityUrl");
|
|
|
|
|
var callBackUrl = configuration.GetValue<string>("CallBackUrl");
|
|
|
|
|
|
2017-08-22 22:36:23 +02:00
|
|
|
|
// Add Authentication services
|
2018-05-16 12:44:32 +02:00
|
|
|
|
|
|
|
|
|
services.AddAuthentication(options =>
|
|
|
|
|
{
|
2017-08-29 18:11:30 +02:00
|
|
|
|
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
2017-06-23 18:45:20 +02:00
|
|
|
|
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
|
2017-08-23 16:40:28 +02:00
|
|
|
|
})
|
2017-08-29 18:11:30 +02:00
|
|
|
|
.AddCookie()
|
2018-05-16 12:44:32 +02:00
|
|
|
|
.AddOpenIdConnect(options =>
|
|
|
|
|
{
|
2017-08-23 16:40:28 +02:00
|
|
|
|
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
|
|
|
options.Authority = identityUrl.ToString();
|
|
|
|
|
options.SignedOutRedirectUri = callBackUrl.ToString();
|
|
|
|
|
options.ClientId = useLoadTest ? "mvctest" : "mvc";
|
|
|
|
|
options.ClientSecret = "secret";
|
|
|
|
|
options.ResponseType = useLoadTest ? "code id_token token" : "code id_token";
|
|
|
|
|
options.SaveTokens = true;
|
|
|
|
|
options.GetClaimsFromUserInfoEndpoint = true;
|
|
|
|
|
options.RequireHttpsMetadata = false;
|
|
|
|
|
options.Scope.Add("openid");
|
|
|
|
|
options.Scope.Add("profile");
|
|
|
|
|
options.Scope.Add("orders");
|
|
|
|
|
options.Scope.Add("basket");
|
|
|
|
|
options.Scope.Add("marketing");
|
|
|
|
|
options.Scope.Add("locations");
|
2018-02-27 14:32:25 +01:00
|
|
|
|
options.Scope.Add("webshoppingagg");
|
2018-04-19 00:58:09 +02:00
|
|
|
|
options.Scope.Add("orders.signalrhub");
|
2017-06-23 18:45:20 +02:00
|
|
|
|
});
|
2017-09-07 19:18:53 +02:00
|
|
|
|
|
2018-05-16 12:44:32 +02:00
|
|
|
|
return services;
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2017-10-13 11:35:26 +02:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
2018-05-16 12:44:32 +02:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|