2017-06-21 19:03:48 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
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.BuildingBlocks;
|
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http;
|
|
|
|
|
using Microsoft.eShopOnContainers.WebMVC.Infrastructure;
|
|
|
|
|
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;
|
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-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
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2017-07-14 14:53:17 +02:00
|
|
|
|
services.AddMvc();
|
|
|
|
|
services.AddSession();
|
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.webmvc";
|
|
|
|
|
})
|
|
|
|
|
.PersistKeysToRedis(Configuration["DPConnectionString"]);
|
|
|
|
|
}
|
2017-03-15 08:57:01 -07:00
|
|
|
|
|
2016-12-07 13:57:31 +01:00
|
|
|
|
services.Configure<AppSettings>(Configuration);
|
2017-03-28 16:16:01 +02:00
|
|
|
|
|
|
|
|
|
services.AddHealthChecks(checks =>
|
|
|
|
|
{
|
2017-05-09 13:54:45 +02:00
|
|
|
|
var minutes = 1;
|
|
|
|
|
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
|
|
|
|
|
{
|
|
|
|
|
minutes = minutesParsed;
|
|
|
|
|
}
|
2017-09-29 11:45:25 +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
|
|
|
|
});
|
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
// Add application services.
|
2017-05-09 13:54:45 +02:00
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
services.AddTransient<ICatalogService, CatalogService>();
|
|
|
|
|
services.AddTransient<IOrderingService, OrderingService>();
|
2016-11-02 20:41:12 +01:00
|
|
|
|
services.AddTransient<IBasketService, BasketService>();
|
2017-06-15 02:20:02 +02:00
|
|
|
|
services.AddTransient<ICampaignService, CampaignService>();
|
2017-09-15 13:54:48 +02:00
|
|
|
|
services.AddTransient<ILocationService, LocationService>();
|
2016-11-28 12:58:51 +01:00
|
|
|
|
services.AddTransient<IIdentityParser<ApplicationUser>, IdentityParser>();
|
2017-03-17 13:12:34 +01:00
|
|
|
|
|
2017-04-04 13:19:22 -07:00
|
|
|
|
if (Configuration.GetValue<string>("UseResilientHttp") == bool.TrueString)
|
2017-03-17 13:12:34 +01:00
|
|
|
|
{
|
2017-05-09 20:27:00 +02:00
|
|
|
|
services.AddSingleton<IResilientHttpClientFactory, ResilientHttpClientFactory>();
|
2017-05-04 13:01:14 +02:00
|
|
|
|
services.AddSingleton<IHttpClient, ResilientHttpClient>(sp => sp.GetService<IResilientHttpClientFactory>().CreateResilientHttpClient());
|
2017-03-17 13:12:34 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-05-04 13:01:14 +02:00
|
|
|
|
services.AddSingleton<IHttpClient, StandardHttpClient>();
|
2017-03-17 13:12:34 +01:00
|
|
|
|
}
|
2017-07-11 12:01:45 +02:00
|
|
|
|
var useLoadTest = Configuration.GetValue<bool>("UseLoadTest");
|
2017-06-22 16:55:57 +02:00
|
|
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
|
|
|
|
var callBackUrl = Configuration.GetValue<string>("CallBackUrl");
|
2017-08-22 22:36:23 +02:00
|
|
|
|
|
|
|
|
|
// Add Authentication services
|
|
|
|
|
|
2017-06-23 18:45:20 +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()
|
2017-08-23 16:40:28 +02:00
|
|
|
|
.AddOpenIdConnect(options => {
|
|
|
|
|
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");
|
2017-06-23 18:45:20 +02:00
|
|
|
|
});
|
2017-05-09 13:54:45 +02:00
|
|
|
|
}
|
2016-09-06 17:09:19 -07: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)
|
|
|
|
|
{
|
2016-11-28 12:58:51 +01:00
|
|
|
|
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
|
|
|
loggerFactory.AddDebug();
|
|
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
app.UseBrowserLink();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-22 14:58:56 +03:00
|
|
|
|
app.UseExceptionHandler("/Error");
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-07 19:18:53 +02:00
|
|
|
|
var pathBase = Configuration["PATH_BASE"];
|
|
|
|
|
if (!string.IsNullOrEmpty(pathBase))
|
|
|
|
|
{
|
|
|
|
|
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
|
|
|
|
|
app.UsePathBase(pathBase);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 14:53:17 +02:00
|
|
|
|
app.UseSession();
|
2016-09-06 17:09:19 -07:00
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
2017-06-23 18:45:20 +02:00
|
|
|
|
app.UseAuthentication();
|
2016-12-07 13:57:31 +01:00
|
|
|
|
|
2017-06-22 16:55:57 +02:00
|
|
|
|
var log = loggerFactory.CreateLogger("identity");
|
2017-08-29 11:40:23 +02:00
|
|
|
|
|
|
|
|
|
WebContextSeed.Seed(app, env, loggerFactory);
|
2016-09-06 17:09:19 -07:00
|
|
|
|
|
|
|
|
|
app.UseMvc(routes =>
|
|
|
|
|
{
|
|
|
|
|
routes.MapRoute(
|
|
|
|
|
name: "default",
|
2016-10-31 09:25:47 +01:00
|
|
|
|
template: "{controller=Catalog}/{action=Index}/{id?}");
|
2017-06-22 14:58:56 +03:00
|
|
|
|
|
|
|
|
|
routes.MapRoute(
|
|
|
|
|
name: "defaultError",
|
|
|
|
|
template: "{controller=Error}/{action=Error}");
|
2016-09-06 17:09:19 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|