From aed97fdc99cba51210438c46efa4cf3271f0a6fd Mon Sep 17 00:00:00 2001 From: Unai Zorrilla Castro Date: Mon, 11 Sep 2017 12:43:45 +0200 Subject: [PATCH] Migrated Identity.API to Identity Server 4 on dotnetcore2 --- .../Controllers/AccountController.cs | 43 +++++++++++-------- .../Identity.API/Data/ApplicationDbContext.cs | 6 +-- .../Identity/Identity.API/Identity.API.csproj | 41 ++++-------------- .../Identity.API/Models/ApplicationUser.cs | 6 +-- src/Services/Identity/Identity.API/Startup.cs | 21 +++++---- 5 files changed, 48 insertions(+), 69 deletions(-) diff --git a/src/Services/Identity/Identity.API/Controllers/AccountController.cs b/src/Services/Identity/Identity.API/Controllers/AccountController.cs index 02b50129f..51e582ed7 100644 --- a/src/Services/Identity/Identity.API/Controllers/AccountController.cs +++ b/src/Services/Identity/Identity.API/Controllers/AccountController.cs @@ -2,26 +2,23 @@ // 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 { @@ -36,7 +33,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers private readonly ILoginService _loginService; private readonly IIdentityServerInteractionService _interaction; private readonly IClientStore _clientStore; - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly UserManager _userManager; public AccountController( @@ -45,13 +42,13 @@ namespace IdentityServer4.Quickstart.UI.Controllers ILoginService loginService, IIdentityServerInteractionService interaction, IClientStore clientStore, - ILoggerFactory loggerFactory, + ILogger logger, UserManager userManager) { _loginService = loginService; _interaction = interaction; _clientStore = clientStore; - _logger = loggerFactory.CreateLogger(); + _logger = logger; _userManager = userManager; } @@ -69,6 +66,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers } var vm = await BuildLoginViewModelAsync(returnUrl, context); + ViewData["ReturnUrl"] = returnUrl; return View(vm); @@ -97,6 +95,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers }; await _loginService.SignIn(user); + // make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint if (_interaction.IsValidReturnUrl(model.ReturnUrl)) { @@ -111,7 +110,9 @@ namespace IdentityServer4.Quickstart.UI.Controllers // something went wrong, show form with error var vm = await BuildLoginViewModelAsync(model); + ViewData["ReturnUrl"] = model.ReturnUrl; + return View(vm); } @@ -180,6 +181,7 @@ namespace IdentityServer4.Quickstart.UI.Controllers public async Task Logout(LogoutViewModel model) { var idp = User?.FindFirst(JwtClaimTypes.IdentityProvider)?.Value; + if (idp != null && idp != IdentityServerConstants.LocalIdentityProvider) { if (model.LogoutId == null) @@ -191,10 +193,15 @@ namespace IdentityServer4.Quickstart.UI.Controllers } string url = "/Account/Logout?logoutId=" + model.LogoutId; + 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 +210,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 +224,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/Data/ApplicationDbContext.cs b/src/Services/Identity/Identity.API/Data/ApplicationDbContext.cs index ddbe7bbfd..bbbe04962 100644 --- a/src/Services/Identity/Identity.API/Data/ApplicationDbContext.cs +++ b/src/Services/Identity/Identity.API/Data/ApplicationDbContext.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using Identity.API.Models; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; -using Identity.API.Models; namespace Identity.API.Data { diff --git a/src/Services/Identity/Identity.API/Identity.API.csproj b/src/Services/Identity/Identity.API/Identity.API.csproj index 7c221e858..ae6aab44b 100644 --- a/src/Services/Identity/Identity.API/Identity.API.csproj +++ b/src/Services/Identity/Identity.API/Identity.API.csproj @@ -1,8 +1,8 @@  - 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,32 +16,9 @@ - - - - - - - - - - - - All - - - All - - - - - - - - - - - + + + @@ -51,10 +28,10 @@ - - - - + + + + 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/Startup.cs b/src/Services/Identity/Identity.API/Startup.cs index c4ee28f25..f579071e7 100644 --- a/src/Services/Identity/Identity.API/Startup.cs +++ b/src/Services/Identity/Identity.API/Startup.cs @@ -11,7 +11,6 @@ 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; @@ -95,16 +94,21 @@ namespace eShopOnContainers.Identity services.AddIdentityServer(x => x.IssuerUri = "null") .AddSigningCredential(Certificate.Get()) .AddAspNetIdentity() - .AddConfigurationStore(builder => - builder.UseSqlServer(connectionString, options => - options.MigrationsAssembly(migrationsAssembly))) - .AddOperationalStore(builder => - builder.UseSqlServer(connectionString, options => - options.MigrationsAssembly(migrationsAssembly))) + .AddConfigurationStore(options => + { + options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, opts => + opts.MigrationsAssembly(migrationsAssembly)); + }) + .AddOperationalStore(options => + { + options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString, opts => + opts.MigrationsAssembly(migrationsAssembly)); + }) .Services.AddTransient(); var container = new ContainerBuilder(); container.Populate(services); + return new AutofacServiceProvider(container.Build()); } @@ -118,7 +122,6 @@ namespace eShopOnContainers.Identity { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); - app.UseBrowserLink(); } else { @@ -142,7 +145,7 @@ namespace eShopOnContainers.Identity await next(); }); - app.UseIdentity(); + app.UseAuthentication(); // Adds IdentityServer app.UseIdentityServer();