From 915635c5f6f2b08f2bba55a4839ea7e50b8e7ddf Mon Sep 17 00:00:00 2001 From: Jacob Roberts Date: Fri, 5 Jun 2020 22:04:13 -0600 Subject: [PATCH] Upgrade app to Asp.net Core 3.1 --- .gitignore | 4 ++ .../Controllers/ManageController.cs | 2 +- .../HtBox.IdentityServer.csproj | 47 +++++------------ .../Models/ApplicationUser.cs | 2 +- .../ManageViewModels/ManageLoginsViewModel.cs | 4 +- .../src/HtBox.IdentityServer/Program.cs | 16 +++--- .../src/HtBox.IdentityServer/Startup.cs | 51 ++++++++----------- .../Views/Account/Login.cshtml | 8 +-- .../Views/Manage/ManageLogins.cshtml | 4 +- 9 files changed, 57 insertions(+), 81 deletions(-) diff --git a/.gitignore b/.gitignore index f1e3d20..8ada537 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# Temp Developer Key +*.rsa + + ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. diff --git a/IdentityServer/src/HtBox.IdentityServer/Controllers/ManageController.cs b/IdentityServer/src/HtBox.IdentityServer/Controllers/ManageController.cs index 65d97fd..c0c9442 100644 --- a/IdentityServer/src/HtBox.IdentityServer/Controllers/ManageController.cs +++ b/IdentityServer/src/HtBox.IdentityServer/Controllers/ManageController.cs @@ -285,7 +285,7 @@ public async Task ManageLogins(ManageMessageId? message = null) return View("Error"); } var userLogins = await _userManager.GetLoginsAsync(user); - var otherLogins = _signInManager.GetExternalAuthenticationSchemes().Where(auth => userLogins.All(ul => auth.AuthenticationScheme != ul.LoginProvider)).ToList(); + var otherLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).Where(auth => userLogins.All(ul => auth.DisplayName != ul.LoginProvider)).ToList(); ViewData["ShowRemoveButton"] = user.PasswordHash != null || userLogins.Count > 1; return View(new ManageLoginsViewModel { diff --git a/IdentityServer/src/HtBox.IdentityServer/HtBox.IdentityServer.csproj b/IdentityServer/src/HtBox.IdentityServer/HtBox.IdentityServer.csproj index 0761164..1a073c7 100644 --- a/IdentityServer/src/HtBox.IdentityServer/HtBox.IdentityServer.csproj +++ b/IdentityServer/src/HtBox.IdentityServer/HtBox.IdentityServer.csproj @@ -1,14 +1,12 @@  - netcoreapp1.1 + netcoreapp3.1 true HtBox.IdentityServer Exe HtBox.IdentityServer aspnet-IdentityServer-0f0420fb-4409-4bd9-9562-3ec1585f9316 - 1.1.1 - $(PackageTargetFallback);dotnet5.6;portable-net45+win8 @@ -18,33 +16,19 @@ - - - - - All + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all - - - - - - - - - - All - - - - - - - - - - - @@ -52,9 +36,4 @@ - - - - - diff --git a/IdentityServer/src/HtBox.IdentityServer/Models/ApplicationUser.cs b/IdentityServer/src/HtBox.IdentityServer/Models/ApplicationUser.cs index aa9ede7..af62d3b 100644 --- a/IdentityServer/src/HtBox.IdentityServer/Models/ApplicationUser.cs +++ b/IdentityServer/src/HtBox.IdentityServer/Models/ApplicationUser.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.AspNetCore.Identity; namespace HtBox.IdentityServer.Models { diff --git a/IdentityServer/src/HtBox.IdentityServer/Models/ManageViewModels/ManageLoginsViewModel.cs b/IdentityServer/src/HtBox.IdentityServer/Models/ManageViewModels/ManageLoginsViewModel.cs index e753d3c..999da25 100644 --- a/IdentityServer/src/HtBox.IdentityServer/Models/ManageViewModels/ManageLoginsViewModel.cs +++ b/IdentityServer/src/HtBox.IdentityServer/Models/ManageViewModels/ManageLoginsViewModel.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore.Http.Authentication; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Identity; namespace HtBox.IdentityServer.Models.ManageViewModels @@ -11,6 +11,6 @@ public class ManageLoginsViewModel { public IList CurrentLogins { get; set; } - public IList OtherLogins { get; set; } + public IList OtherLogins { get; set; } } } diff --git a/IdentityServer/src/HtBox.IdentityServer/Program.cs b/IdentityServer/src/HtBox.IdentityServer/Program.cs index c2c2283..d632509 100644 --- a/IdentityServer/src/HtBox.IdentityServer/Program.cs +++ b/IdentityServer/src/HtBox.IdentityServer/Program.cs @@ -1,5 +1,6 @@ -using System.IO; +using System; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; namespace HtBox.IdentityServer { @@ -7,14 +8,15 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() - .UseStartup() - .Build(); + var host = CreateHostBuilder(args).Build(); host.Run(); } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => { + webBuilder.UseStartup(); + }); } } diff --git a/IdentityServer/src/HtBox.IdentityServer/Startup.cs b/IdentityServer/src/HtBox.IdentityServer/Startup.cs index 43f3057..00e1094 100644 --- a/IdentityServer/src/HtBox.IdentityServer/Startup.cs +++ b/IdentityServer/src/HtBox.IdentityServer/Startup.cs @@ -1,36 +1,27 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using HtBox.IdentityServer.Data; using HtBox.IdentityServer.Models; using IdentityServer.Services; +using Microsoft.AspNetCore.Identity; +using Microsoft.Extensions.Hosting; namespace HtBox.IdentityServer { public class Startup { - public Startup(IHostingEnvironment env) - { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); + public IWebHostEnvironment Environment { get; } + public IConfiguration Configuration { get; } - if (env.IsDevelopment()) - { - builder.AddUserSecrets(); - } - - builder.AddEnvironmentVariables(); - Configuration = builder.Build(); + public Startup(IWebHostEnvironment environment, IConfiguration configuration) + { + Environment = environment; + Configuration = configuration; } - 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) { @@ -42,27 +33,26 @@ public void ConfigureServices(IServiceCollection services) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); - services.AddMvc(); + services.AddControllersWithViews(); // Add application services. services.AddTransient(); services.AddTransient(); // Add IdentityServer 4 - services.AddIdentityServer() - .AddTemporarySigningCredential() + var builder = services.AddIdentityServer() .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources()) .AddInMemoryClients(IdentityServerConfig.GetClients()) .AddAspNetIdentity(); + + // not recommended for production - you need to store your key material somewhere secure + builder.AddDeveloperSigningCredential(); } // 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) { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - - if (env.IsDevelopment()) + if (Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); @@ -75,15 +65,16 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF app.UseStaticFiles(); - app.UseIdentity(); + app.UseRouting(); app.UseIdentityServer(); - app.UseMvc(routes => + app.UseAuthentication(); + app.UseAuthorization(); + + app.UseEndpoints(endpoints => { - routes.MapRoute( - name: "default", - template: "{controller=Home}/{action=Index}/{id?}"); + endpoints.MapDefaultControllerRoute(); }); } } diff --git a/IdentityServer/src/HtBox.IdentityServer/Views/Account/Login.cshtml b/IdentityServer/src/HtBox.IdentityServer/Views/Account/Login.cshtml index d803819..53af7b8 100644 --- a/IdentityServer/src/HtBox.IdentityServer/Views/Account/Login.cshtml +++ b/IdentityServer/src/HtBox.IdentityServer/Views/Account/Login.cshtml @@ -1,6 +1,6 @@ @using System.Collections.Generic @using Microsoft.AspNetCore.Http -@using Microsoft.AspNetCore.Http.Authentication +@using Microsoft.AspNetCore.Authentication @model LoginViewModel @inject SignInManager SignInManager @@ -59,8 +59,8 @@

Use another service to log in.


@{ - var loginProviders = SignInManager.GetExternalAuthenticationSchemes().ToList(); - if (loginProviders.Count == 0) + var loginProviders = (await SignInManager.GetExternalAuthenticationSchemesAsync()).ToList(); + if (loginProviders.Count() == 0) {

@@ -76,7 +76,7 @@

@foreach (var provider in loginProviders) { - + }

diff --git a/IdentityServer/src/HtBox.IdentityServer/Views/Manage/ManageLogins.cshtml b/IdentityServer/src/HtBox.IdentityServer/Views/Manage/ManageLogins.cshtml index cb764d4..9e4cbcc 100644 --- a/IdentityServer/src/HtBox.IdentityServer/Views/Manage/ManageLogins.cshtml +++ b/IdentityServer/src/HtBox.IdentityServer/Views/Manage/ManageLogins.cshtml @@ -1,5 +1,5 @@ @model ManageLoginsViewModel -@using Microsoft.AspNetCore.Http.Authentication +@using Microsoft.AspNetCore.Authentication @{ ViewData["Title"] = "Manage your external logins"; } @@ -46,7 +46,7 @@

@foreach (var provider in Model.OtherLogins) { - + }