2016-09-06 17:09:19 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-09-07 13:52:26 -07:00
|
|
|
|
using Microsoft.eShopOnContainers.WebMVC.Models;
|
|
|
|
|
using Microsoft.eShopOnContainers.WebMVC.Services;
|
2016-11-28 12:58:51 +01:00
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2016-11-29 15:10:16 +01:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2016-12-07 13:57:31 +01:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
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
|
|
|
|
|
{
|
|
|
|
|
public Startup(IHostingEnvironment env)
|
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
2016-12-07 13:57:31 +01:00
|
|
|
|
.SetBasePath(env.ContentRootPath)
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // Settings for the application
|
|
|
|
|
.AddEnvironmentVariables(); // override settings with environment variables set in compose.
|
2016-09-06 17:09:19 -07:00
|
|
|
|
|
2017-01-10 11:37:36 +01:00
|
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
|
|
|
|
|
builder.AddUserSecrets();
|
|
|
|
|
}
|
2016-12-07 13:57:31 +01:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
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();
|
2016-12-07 13:57:31 +01:00
|
|
|
|
services.Configure<AppSettings>(Configuration);
|
2016-11-02 20:41:12 +01:00
|
|
|
|
|
2016-09-06 17:09:19 -07:00
|
|
|
|
// Add application services.
|
2016-11-29 15:10:16 +01:00
|
|
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
|
2016-11-10 09:13:08 +01:00
|
|
|
|
services.AddTransient<ICatalogService, CatalogService>();
|
2016-12-22 13:20:12 +01:00
|
|
|
|
services.AddTransient<IOrderingService, OrderingService>();
|
2016-11-02 20:41:12 +01:00
|
|
|
|
services.AddTransient<IBasketService, BasketService>();
|
2016-11-28 12:58:51 +01:00
|
|
|
|
services.AddTransient<IIdentityParser<ApplicationUser>, IdentityParser>();
|
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
|
|
|
|
|
{
|
2016-10-31 09:25:47 +01:00
|
|
|
|
app.UseExceptionHandler("/Catalog/Error");
|
2016-09-06 17:09:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
app.UseCookieAuthentication(new CookieAuthenticationOptions
|
|
|
|
|
{
|
2016-12-07 13:57:31 +01:00
|
|
|
|
AuthenticationScheme = "Cookies",
|
2016-11-28 12:58:51 +01:00
|
|
|
|
AutomaticAuthenticate = true,
|
|
|
|
|
});
|
|
|
|
|
|
2016-12-09 09:18:18 +01:00
|
|
|
|
var identityUrl = Configuration.GetValue<string>("IdentityUrl");
|
|
|
|
|
var callBackUrl = Configuration.GetValue<string>("CallBackUrl");
|
2016-12-07 13:57:31 +01:00
|
|
|
|
var log = loggerFactory.CreateLogger("identity");
|
|
|
|
|
|
2016-11-28 12:58:51 +01:00
|
|
|
|
var oidcOptions = new OpenIdConnectOptions
|
|
|
|
|
{
|
|
|
|
|
AuthenticationScheme = "oidc",
|
2016-12-07 13:57:31 +01:00
|
|
|
|
SignInScheme = "Cookies",
|
|
|
|
|
Authority = identityUrl.ToString(),
|
|
|
|
|
PostLogoutRedirectUri = callBackUrl.ToString(),
|
2016-11-28 12:58:51 +01:00
|
|
|
|
ClientId = "mvc",
|
|
|
|
|
ClientSecret = "secret",
|
2016-12-09 09:18:18 +01:00
|
|
|
|
ResponseType = "code id_token",
|
2016-11-28 12:58:51 +01:00
|
|
|
|
SaveTokens = true,
|
|
|
|
|
GetClaimsFromUserInfoEndpoint = true,
|
2016-11-29 15:10:16 +01:00
|
|
|
|
RequireHttpsMetadata = false,
|
2016-11-28 12:58:51 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
oidcOptions.Scope.Clear();
|
|
|
|
|
oidcOptions.Scope.Add("openid");
|
|
|
|
|
oidcOptions.Scope.Add("profile");
|
|
|
|
|
oidcOptions.Scope.Add("orders");
|
2016-11-29 15:10:16 +01:00
|
|
|
|
oidcOptions.Scope.Add("basket");
|
2016-11-28 12:58:51 +01:00
|
|
|
|
|
2016-12-07 13:57:31 +01:00
|
|
|
|
//Wait untill identity service is ready on compose.
|
2016-11-28 12:58:51 +01:00
|
|
|
|
app.UseOpenIdConnectAuthentication(oidcOptions);
|
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?}");
|
2016-09-06 17:09:19 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|