From 116d5b55bb142b959e9d7607893b2a21f30fdc0b Mon Sep 17 00:00:00 2001 From: Unai Zorrilla Castro Date: Tue, 29 Aug 2017 11:28:21 +0200 Subject: [PATCH 1/3] Updated Startup and Program to use new .NETCoreApp2.0 templates.Minor Refactorings --- .../WebStatus/Controllers/HomeController.cs | 9 +++----- .../HealthCheckBuilderExtensions.cs | 4 ---- src/Web/WebStatus/Program.cs | 22 ++++++++++++------- src/Web/WebStatus/Startup.cs | 18 +++++---------- .../Viewmodels/HealthStatusViewModel.cs | 9 ++++---- .../WebStatus/Viewmodels/NamedCheckResult.cs | 5 +---- 6 files changed, 29 insertions(+), 38 deletions(-) diff --git a/src/Web/WebStatus/Controllers/HomeController.cs b/src/Web/WebStatus/Controllers/HomeController.cs index cd59c44d2..7f139843a 100644 --- a/src/Web/WebStatus/Controllers/HomeController.cs +++ b/src/Web/WebStatus/Controllers/HomeController.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.HealthChecks; +using System.Threading.Tasks; using WebStatus.Viewmodels; namespace WebStatus.Controllers @@ -29,6 +25,7 @@ namespace WebStatus.Controllers } ViewBag.RefreshSeconds = 60; + return View(data); } diff --git a/src/Web/WebStatus/Extensions/HealthCheckBuilderExtensions.cs b/src/Web/WebStatus/Extensions/HealthCheckBuilderExtensions.cs index c0655b753..a6076df52 100644 --- a/src/Web/WebStatus/Extensions/HealthCheckBuilderExtensions.cs +++ b/src/Web/WebStatus/Extensions/HealthCheckBuilderExtensions.cs @@ -1,8 +1,5 @@ using Microsoft.Extensions.HealthChecks; using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace WebStatus.Extensions { @@ -17,6 +14,5 @@ namespace WebStatus.Extensions return builder; } - } } diff --git a/src/Web/WebStatus/Program.cs b/src/Web/WebStatus/Program.cs index f0df7ee22..6a895f9e7 100644 --- a/src/Web/WebStatus/Program.cs +++ b/src/Web/WebStatus/Program.cs @@ -1,5 +1,7 @@ -using System.IO; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Logging; +using System.IO; namespace WebStatus { @@ -7,14 +9,18 @@ namespace WebStatus { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() .UseStartup() - .Build(); - - host.Run(); - } + .ConfigureLogging((hostingContext, builder) => + { + builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); + builder.AddConsole(); + builder.AddDebug(); + }).Build(); } } diff --git a/src/Web/WebStatus/Startup.cs b/src/Web/WebStatus/Startup.cs index 244f81787..eb559acd9 100644 --- a/src/Web/WebStatus/Startup.cs +++ b/src/Web/WebStatus/Startup.cs @@ -13,22 +13,18 @@ namespace WebStatus { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddOptions(); + // Add framework services. services.AddHealthChecks(checks => { @@ -47,15 +43,13 @@ namespace WebStatus checks.AddUrlCheckIfNotNull(Configuration["mvc"], TimeSpan.FromMinutes(minutes)); checks.AddUrlCheckIfNotNull(Configuration["spa"], TimeSpan.FromMinutes(minutes)); }); + services.AddMvc(); } // 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) + public void Configure(IApplicationBuilder app, IHostingEnvironment env) { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); diff --git a/src/Web/WebStatus/Viewmodels/HealthStatusViewModel.cs b/src/Web/WebStatus/Viewmodels/HealthStatusViewModel.cs index d30acc460..08126ce4f 100644 --- a/src/Web/WebStatus/Viewmodels/HealthStatusViewModel.cs +++ b/src/Web/WebStatus/Viewmodels/HealthStatusViewModel.cs @@ -1,22 +1,23 @@ using Microsoft.Extensions.HealthChecks; -using System; using System.Collections.Generic; using System.Linq; -using System.Threading.Tasks; namespace WebStatus.Viewmodels { public class HealthStatusViewModel { private readonly CheckStatus _overall; + private readonly Dictionary _results; public CheckStatus OverallStatus => _overall; + public IEnumerable Results => _results.Select(kvp => new NamedCheckResult(kvp.Key, kvp.Value)); + private HealthStatusViewModel() => _results = new Dictionary(); + public HealthStatusViewModel(CheckStatus overall) : this() => _overall = overall; + public void AddResult(string name, IHealthCheckResult result) => _results.Add(name, result); - - } } diff --git a/src/Web/WebStatus/Viewmodels/NamedCheckResult.cs b/src/Web/WebStatus/Viewmodels/NamedCheckResult.cs index e856504c7..f6a9cd316 100644 --- a/src/Web/WebStatus/Viewmodels/NamedCheckResult.cs +++ b/src/Web/WebStatus/Viewmodels/NamedCheckResult.cs @@ -1,14 +1,11 @@ using Microsoft.Extensions.HealthChecks; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace WebStatus.Viewmodels { public class NamedCheckResult { public string Name { get; } + public IHealthCheckResult Result { get; } public NamedCheckResult(string name, IHealthCheckResult result) From acefa6446597d732ba547088ac2d4953cd2ca89f Mon Sep 17 00:00:00 2001 From: Unai Zorrilla Castro Date: Tue, 29 Aug 2017 11:40:23 +0200 Subject: [PATCH 2/3] Updated Program and Startup to new .NETCoreApp2.0 templates. Minor refactorings --- src/Web/WebMVC/Program.cs | 24 +++++++++++++++--------- src/Web/WebMVC/Startup.cs | 14 +++++--------- src/Web/WebMVC/WebMVC.csproj | 27 ++++----------------------- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/Web/WebMVC/Program.cs b/src/Web/WebMVC/Program.cs index 654ae7677..ec711b2ae 100644 --- a/src/Web/WebMVC/Program.cs +++ b/src/Web/WebMVC/Program.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Logging; using System.IO; namespace Microsoft.eShopOnContainers.WebMVC @@ -7,15 +9,19 @@ namespace Microsoft.eShopOnContainers.WebMVC { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseHealthChecks("/hc") + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + .UseHealthChecks("/hc") .UseStartup() - .Build(); - - host.Run(); - } + .ConfigureLogging((hostingContext, builder) => + { + builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); + builder.AddConsole(); + builder.AddDebug(); + }).Build(); } } diff --git a/src/Web/WebMVC/Startup.cs b/src/Web/WebMVC/Startup.cs index ebf303f1b..1791e8da1 100644 --- a/src/Web/WebMVC/Startup.cs +++ b/src/Web/WebMVC/Startup.cs @@ -20,17 +20,12 @@ namespace Microsoft.eShopOnContainers.WebMVC { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - 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(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) @@ -135,7 +130,8 @@ namespace Microsoft.eShopOnContainers.WebMVC app.UseAuthentication(); var log = loggerFactory.CreateLogger("identity"); - WebContextSeed.Seed(app, env, loggerFactory); + + WebContextSeed.Seed(app, env, loggerFactory); app.UseMvc(routes => { diff --git a/src/Web/WebMVC/WebMVC.csproj b/src/Web/WebMVC/WebMVC.csproj index e6e11cb99..7f45a64f9 100644 --- a/src/Web/WebMVC/WebMVC.csproj +++ b/src/Web/WebMVC/WebMVC.csproj @@ -36,27 +36,8 @@ --> - - - - - - - - - - - - - - - - - - - - - + + @@ -66,8 +47,8 @@ - - + + From cd78d98436c1b3209838f929dbea3f58cde522e3 Mon Sep 17 00:00:00 2001 From: Unai Zorrilla Castro Date: Tue, 29 Aug 2017 12:12:45 +0200 Subject: [PATCH 3/3] Updated Identity.API to preview version of IdentityServer4, Fix breaking changes. Not well tested --- .../Controllers/AccountController.cs | 28 +++++++------- .../Identity/Identity.API/Identity.API.csproj | 38 ++++--------------- .../Identity.API/Models/ApplicationUser.cs | 6 +-- src/Services/Identity/Identity.API/Program.cs | 24 +++++++----- src/Services/Identity/Identity.API/Startup.cs | 31 +++++---------- src/Web/WebMVC/WebMVC.csproj | 1 - 6 files changed, 47 insertions(+), 81 deletions(-) diff --git a/src/Services/Identity/Identity.API/Controllers/AccountController.cs b/src/Services/Identity/Identity.API/Controllers/AccountController.cs index 641f39522..6b56b3467 100644 --- a/src/Services/Identity/Identity.API/Controllers/AccountController.cs +++ b/src/Services/Identity/Identity.API/Controllers/AccountController.cs @@ -2,26 +2,24 @@ // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. +using Identity.API.Models; +using Identity.API.Models.AccountViewModels; +using Identity.API.Services; using IdentityModel; -using IdentityServer4.Quickstart.UI.Models; +using IdentityServer4.Models; using IdentityServer4.Services; -using Microsoft.AspNetCore.Http.Authentication; +using IdentityServer4.Stores; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using System; -using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Text.Encodings.Web; using System.Threading.Tasks; -using IdentityServer4.Models; -using IdentityServer4.Stores; -using Identity.API.Services; -using Identity.API.Models; -using Microsoft.Extensions.Logging; -using Microsoft.AspNetCore.Authorization; -using Identity.API.Models.AccountViewModels; -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Authentication; + namespace IdentityServer4.Quickstart.UI.Controllers { @@ -194,7 +192,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers try { // hack: try/catch to handle social providers that throw - await HttpContext.Authentication.SignOutAsync(idp, new AuthenticationProperties { RedirectUri = url }); + await HttpContext.SignOutAsync(idp, new AuthenticationProperties { RedirectUri = url }); } catch(Exception ex) { @@ -203,7 +201,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers } // delete authentication cookie - await HttpContext.Authentication.SignOutAsync(); + await HttpContext.SignOutAsync(); // set this so UI rendering sees an anonymous user HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity()); @@ -217,7 +215,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers public async Task DeviceLogOut(string redirectUrl) { // delete authentication cookie - await HttpContext.Authentication.SignOutAsync(); + await HttpContext.SignOutAsync(); // set this so UI rendering sees an anonymous user HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity()); diff --git a/src/Services/Identity/Identity.API/Identity.API.csproj b/src/Services/Identity/Identity.API/Identity.API.csproj index 7c221e858..49f9b4c2e 100644 --- a/src/Services/Identity/Identity.API/Identity.API.csproj +++ b/src/Services/Identity/Identity.API/Identity.API.csproj @@ -1,10 +1,10 @@  - netcoreapp1.1 - 1.1.2 + netcoreapp2.0 + 2.0.0 aspnet-eShopOnContainers.Identity-90487118-103c-4ff0-b9da-e5e26f7ab0c5 - $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; + ..\..\..\..\docker-compose.dcproj @@ -16,30 +16,8 @@ - - - - - - - - - - - - All - - - All - - - - - - - - - + + @@ -52,9 +30,9 @@ - - - + + + diff --git a/src/Services/Identity/Identity.API/Models/ApplicationUser.cs b/src/Services/Identity/Identity.API/Models/ApplicationUser.cs index b520c333b..1c1f7bda1 100644 --- a/src/Services/Identity/Identity.API/Models/ApplicationUser.cs +++ b/src/Services/Identity/Identity.API/Models/ApplicationUser.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.AspNetCore.Identity; using System.ComponentModel.DataAnnotations; namespace Identity.API.Models diff --git a/src/Services/Identity/Identity.API/Program.cs b/src/Services/Identity/Identity.API/Program.cs index 2f731c045..9a035bd93 100644 --- a/src/Services/Identity/Identity.API/Program.cs +++ b/src/Services/Identity/Identity.API/Program.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Logging; using System.IO; namespace eShopOnContainers.Identity @@ -7,15 +9,19 @@ namespace eShopOnContainers.Identity { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseHealthChecks("/hc") + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + .UseHealthChecks("/hc") .UseStartup() - .Build(); - - host.Run(); - } + .ConfigureLogging((hostingContext, builder) => + { + builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); + builder.AddConsole(); + builder.AddDebug(); + }).Build(); } } diff --git a/src/Services/Identity/Identity.API/Startup.cs b/src/Services/Identity/Identity.API/Startup.cs index 5b0a216de..711c4b9c1 100644 --- a/src/Services/Identity/Identity.API/Startup.cs +++ b/src/Services/Identity/Identity.API/Startup.cs @@ -1,4 +1,6 @@ -using Identity.API.Certificate; +using Autofac; +using Autofac.Extensions.DependencyInjection; +using Identity.API.Certificate; using Identity.API.Configuration; using Identity.API.Data; using Identity.API.Models; @@ -9,16 +11,12 @@ using IdentityServer4.Services; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.eShopOnContainers.BuildingBlocks; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.HealthChecks; -using Identity.API.Certificate; -using Autofac.Extensions.DependencyInjection; -using Autofac; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -30,24 +28,13 @@ namespace eShopOnContainers.Identity { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); - - if (env.IsDevelopment()) - { - // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 - builder.AddUserSecrets(); - } - - builder.AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + public IConfiguration Configuration { get; } + // This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) @@ -136,9 +123,11 @@ namespace eShopOnContainers.Identity await next(); }); - app.UseIdentity(); + // Adds IdentityServer + app.UseAuthentication(); + app.UseIdentityServer(); app.UseMvc(routes => diff --git a/src/Web/WebMVC/WebMVC.csproj b/src/Web/WebMVC/WebMVC.csproj index 7f45a64f9..5e73cdfda 100644 --- a/src/Web/WebMVC/WebMVC.csproj +++ b/src/Web/WebMVC/WebMVC.csproj @@ -37,7 +37,6 @@ -