144 lines
6.1 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
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;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.HealthChecks;
using Microsoft.Extensions.Logging;
using System;
using System.IdentityModel.Tokens.Jwt;
namespace Microsoft.eShopOnContainers.WebMVC
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // Settings for the application
.AddEnvironmentVariables(); // override settings with environment variables set in compose.
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
if (Configuration.GetValue<string>("IsClusterEnv") == bool.TrueString)
{
services.AddDataProtection(opts =>
{
opts.ApplicationDiscriminator = "eshop.webmvc";
})
.PersistKeysToRedis(Configuration["DPConnectionString"]);
}
services.Configure<AppSettings>(Configuration);
services.AddHealthChecks(checks =>
{
var minutes = 1;
if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
{
minutes = minutesParsed;
}
2017-05-09 16:59:23 +02:00
checks.AddUrlCheck(Configuration["CatalogUrl"] + "/hc", TimeSpan.FromMinutes(minutes));
checks.AddUrlCheck(Configuration["OrderingUrl"] + "/hc", TimeSpan.FromMinutes(minutes));
checks.AddUrlCheck(Configuration["BasketUrl"] + "/hc", TimeSpan.FromMinutes(minutes));
checks.AddUrlCheck(Configuration["IdentityUrl"] + "/hc", TimeSpan.FromMinutes(minutes));
});
// Add application services.
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>();
services.AddTransient<IIdentityParser<ApplicationUser>, IdentityParser>();
2017-03-17 13:12:34 +01: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>();
services.AddSingleton<IHttpClient, ResilientHttpClient>(sp => sp.GetService<IResilientHttpClientFactory>().CreateResilientHttpClient());
2017-03-17 13:12:34 +01:00
}
else
{
services.AddSingleton<IHttpClient, StandardHttpClient>();
2017-03-17 13:12:34 +01:00
}
2017-06-22 16:55:57 +02:00
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
var callBackUrl = Configuration.GetValue<string>("CallBackUrl");
// Add Authentication services
services.AddCookieAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
services.AddOpenIdConnectAuthentication(OpenIdConnectDefaults.AuthenticationScheme, options =>
2017-06-22 16:55:57 +02:00
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = identityUrl.ToString();
options.PostLogoutRedirectUri = callBackUrl.ToString();
options.ClientId = "mvc";
options.ClientSecret = "secret";
options.ResponseType = "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");
});
services.AddAuthentication(options => {
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
});
}
// 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();
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
2016-10-31 09:25:47 +01:00
app.UseExceptionHandler("/Catalog/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
2017-06-22 16:55:57 +02:00
var log = loggerFactory.CreateLogger("identity");
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
2016-10-31 09:25:47 +01:00
template: "{controller=Catalog}/{action=Index}/{id?}");
});
}
}
}