2017-09-06 18:43:38 +02:00
|
|
|
|
using Autofac;
|
|
|
|
|
using Autofac.Extensions.DependencyInjection;
|
|
|
|
|
using Identity.API.Certificate;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Identity.API.Configuration;
|
|
|
|
|
using Identity.API.Data;
|
|
|
|
|
using Identity.API.Models;
|
|
|
|
|
using Identity.API.Services;
|
|
|
|
|
using IdentityServer4.EntityFramework.DbContexts;
|
|
|
|
|
using IdentityServer4.EntityFramework.Mappers;
|
|
|
|
|
using IdentityServer4.Services;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2017-08-29 18:11:30 +02:00
|
|
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-03-28 16:16:01 +02:00
|
|
|
|
using Microsoft.Extensions.HealthChecks;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2017-06-05 21:54:03 +02:00
|
|
|
|
using System.Reflection;
|
2017-06-08 17:45:07 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
|
|
|
|
namespace eShopOnContainers.Identity
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2017-08-29 18:11:30 +02:00
|
|
|
|
public Startup(IHostingEnvironment env)
|
2016-11-24 15:31:33 +01:00
|
|
|
|
{
|
2017-08-29 18:11:30 +02:00
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.SetBasePath(env.ContentRootPath)
|
|
|
|
|
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
|
|
|
|
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
2017-08-29 18:11:30 +02:00
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
|
|
|
|
|
builder.AddUserSecrets<Startup>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.AddEnvironmentVariables();
|
|
|
|
|
Configuration = builder.Build();
|
|
|
|
|
}
|
2017-08-29 12:12:45 +02:00
|
|
|
|
|
2017-08-29 18:11:30 +02:00
|
|
|
|
public IConfigurationRoot Configuration { get; }
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
2017-05-11 13:44:38 +02:00
|
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
2017-08-29 18:11:30 +02:00
|
|
|
|
{
|
2017-03-28 16:16:01 +02:00
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
// Add framework services.
|
|
|
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
|
|
|
|
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
|
|
|
|
|
|
services.AddIdentity<ApplicationUser, IdentityRole>()
|
|
|
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
|
|
|
.AddDefaultTokenProviders();
|
2016-12-19 10:20:02 +01:00
|
|
|
|
|
|
|
|
|
services.Configure<AppSettings>(Configuration);
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
2017-06-05 21:54:03 +02:00
|
|
|
|
services.AddMvc();
|
|
|
|
|
|
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.identity";
|
|
|
|
|
})
|
|
|
|
|
.PersistKeysToRedis(Configuration["DPConnectionString"]);
|
|
|
|
|
}
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
checks.AddSqlCheck("Identity_Db", Configuration.GetConnectionString("DefaultConnection"), TimeSpan.FromMinutes(minutes));
|
2017-03-28 16:16:01 +02:00
|
|
|
|
});
|
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
services.AddTransient<IEmailSender, AuthMessageSender>();
|
|
|
|
|
services.AddTransient<ISmsSender, AuthMessageSender>();
|
|
|
|
|
services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
|
2016-12-29 16:26:02 +01:00
|
|
|
|
services.AddTransient<IRedirectService, RedirectService>();
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
2017-06-05 21:54:03 +02:00
|
|
|
|
var connectionString = Configuration.GetConnectionString("DefaultConnection");
|
|
|
|
|
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
|
2016-12-07 13:57:31 +01:00
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
// Adds IdentityServer
|
2017-01-10 15:14:05 +01:00
|
|
|
|
services.AddIdentityServer(x => x.IssuerUri = "null")
|
2017-08-29 18:11:30 +02:00
|
|
|
|
.AddSigningCredential(Certificate.Get())
|
2016-11-28 12:58:51 +01:00
|
|
|
|
.AddAspNetIdentity<ApplicationUser>()
|
2017-06-05 21:54:03 +02:00
|
|
|
|
.AddConfigurationStore(builder =>
|
|
|
|
|
builder.UseSqlServer(connectionString, options =>
|
|
|
|
|
options.MigrationsAssembly(migrationsAssembly)))
|
|
|
|
|
.AddOperationalStore(builder =>
|
|
|
|
|
builder.UseSqlServer(connectionString, options =>
|
|
|
|
|
options.MigrationsAssembly(migrationsAssembly)))
|
|
|
|
|
.Services.AddTransient<IProfileService, ProfileService>();
|
2017-05-11 13:44:38 +02:00
|
|
|
|
|
|
|
|
|
var container = new ContainerBuilder();
|
|
|
|
|
container.Populate(services);
|
|
|
|
|
return new AutofacServiceProvider(container.Build());
|
2016-11-24 15:31:33 +01: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)
|
|
|
|
|
{
|
|
|
|
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
|
|
|
|
loggerFactory.AddDebug();
|
|
|
|
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
app.UseDatabaseErrorPage();
|
|
|
|
|
app.UseBrowserLink();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Home/Error");
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
2016-12-20 12:22:28 +01:00
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
});
|
|
|
|
|
|
2017-08-29 18:11:30 +02:00
|
|
|
|
app.UseIdentity();
|
2016-11-24 15:31:33 +01:00
|
|
|
|
|
|
|
|
|
// Adds IdentityServer
|
|
|
|
|
app.UseIdentityServer();
|
2016-11-28 12:58:51 +01:00
|
|
|
|
|
2016-11-24 15:31:33 +01:00
|
|
|
|
app.UseMvc(routes =>
|
|
|
|
|
{
|
|
|
|
|
routes.MapRoute(
|
|
|
|
|
name: "default",
|
|
|
|
|
template: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
});
|
|
|
|
|
|
2017-06-08 17:45:07 +02:00
|
|
|
|
// Store idsrv grant config into db
|
|
|
|
|
InitializeGrantStoreAndConfiguration(app).Wait();
|
2017-06-05 21:54:03 +02:00
|
|
|
|
|
2016-12-14 12:15:39 +01:00
|
|
|
|
//Seed Data
|
|
|
|
|
var hasher = new PasswordHasher<ApplicationUser>();
|
2017-06-20 12:54:32 -07:00
|
|
|
|
new ApplicationContextSeed(hasher).SeedAsync(app, env, loggerFactory).Wait();
|
2016-11-24 15:31:33 +01:00
|
|
|
|
}
|
2017-06-05 21:54:03 +02:00
|
|
|
|
|
2017-06-08 17:45:07 +02:00
|
|
|
|
private async Task InitializeGrantStoreAndConfiguration(IApplicationBuilder app)
|
2017-06-05 21:54:03 +02:00
|
|
|
|
{
|
|
|
|
|
//callbacks urls from config:
|
|
|
|
|
Dictionary<string, string> clientUrls = new Dictionary<string, string>();
|
|
|
|
|
clientUrls.Add("Mvc", Configuration.GetValue<string>("MvcClient"));
|
|
|
|
|
clientUrls.Add("Spa", Configuration.GetValue<string>("SpaClient"));
|
|
|
|
|
clientUrls.Add("Xamarin", Configuration.GetValue<string>("XamarinCallback"));
|
2017-07-12 12:10:10 +02:00
|
|
|
|
clientUrls.Add("LocationsApi", Configuration.GetValue<string>("LocationApiClient"));
|
|
|
|
|
clientUrls.Add("MarketingApi", Configuration.GetValue<string>("MarketingApiClient"));
|
|
|
|
|
clientUrls.Add("BasketApi", Configuration.GetValue<string>("BasketApiClient"));
|
|
|
|
|
clientUrls.Add("OrderingApi", Configuration.GetValue<string>("OrderingApiClient"));
|
2017-06-05 21:54:03 +02:00
|
|
|
|
|
|
|
|
|
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
|
|
|
|
|
{
|
|
|
|
|
serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
|
|
|
|
|
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
|
|
|
|
|
context.Database.Migrate();
|
|
|
|
|
|
|
|
|
|
if (!context.Clients.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var client in Config.GetClients(clientUrls))
|
|
|
|
|
{
|
2017-06-08 17:45:07 +02:00
|
|
|
|
await context.Clients.AddAsync(client.ToEntity());
|
2017-06-05 21:54:03 +02:00
|
|
|
|
}
|
2017-06-08 17:45:07 +02:00
|
|
|
|
await context.SaveChangesAsync();
|
2017-06-05 21:54:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!context.IdentityResources.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var resource in Config.GetResources())
|
|
|
|
|
{
|
2017-06-08 17:45:07 +02:00
|
|
|
|
await context.IdentityResources.AddAsync(resource.ToEntity());
|
2017-06-05 21:54:03 +02:00
|
|
|
|
}
|
2017-06-08 17:45:07 +02:00
|
|
|
|
await context.SaveChangesAsync();
|
2017-06-05 21:54:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!context.ApiResources.Any())
|
|
|
|
|
{
|
|
|
|
|
foreach (var api in Config.GetApis())
|
|
|
|
|
{
|
2017-06-08 17:45:07 +02:00
|
|
|
|
await context.ApiResources.AddAsync(api.ToEntity());
|
2017-06-05 21:54:03 +02:00
|
|
|
|
}
|
2017-06-08 17:45:07 +02:00
|
|
|
|
await context.SaveChangesAsync();
|
2017-06-05 21:54:03 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-24 15:31:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|