From cd78d98436c1b3209838f929dbea3f58cde522e3 Mon Sep 17 00:00:00 2001 From: Unai Zorrilla Castro Date: Tue, 29 Aug 2017 12:12:45 +0200 Subject: [PATCH] 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 @@ -