From 6d7736ff336dd6b55aa1ab4d49cd8f25f102b2cb Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Wed, 23 Aug 2017 06:30:13 -0500 Subject: [PATCH] Upgrade to ASP.NET Core 2 --- .gitignore | 1 + .../Sqlite/Controllers/AccountController.cs | 6 +---- Sqlite/Sqlite/Controllers/ManageController.cs | 9 +++---- Sqlite/Sqlite/EfSqlite.csproj | 27 +++++-------------- Sqlite/Sqlite/Models/ApplicationUser.cs | 6 +---- .../ManageViewModels/ManageLoginsViewModel.cs | 9 +++---- Sqlite/Sqlite/Program.cs | 19 +++++-------- Sqlite/Sqlite/Startup.cs | 10 +++---- Sqlite/Sqlite/Views/Account/Login.cshtml | 12 ++++----- 9 files changed, 29 insertions(+), 70 deletions(-) diff --git a/.gitignore b/.gitignore index f1e3d20..bf4e004 100644 --- a/.gitignore +++ b/.gitignore @@ -250,3 +250,4 @@ paket-files/ # JetBrains Rider .idea/ *.sln.iml +/Sqlite/Sqlite/Database.db diff --git a/Sqlite/Sqlite/Controllers/AccountController.cs b/Sqlite/Sqlite/Controllers/AccountController.cs index 9a999db..f07498b 100644 --- a/Sqlite/Sqlite/Controllers/AccountController.cs +++ b/Sqlite/Sqlite/Controllers/AccountController.cs @@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using Sqlite.Models; using Sqlite.Models.AccountViewModels; using Sqlite.Services; @@ -23,19 +22,16 @@ public class AccountController : Controller private readonly IEmailSender _emailSender; private readonly ISmsSender _smsSender; private readonly ILogger _logger; - private readonly string _externalCookieScheme; public AccountController( UserManager userManager, SignInManager signInManager, - IOptions identityCookieOptions, IEmailSender emailSender, ISmsSender smsSender, ILoggerFactory loggerFactory) { _userManager = userManager; _signInManager = signInManager; - _externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme; _emailSender = emailSender; _smsSender = smsSender; _logger = loggerFactory.CreateLogger(); @@ -48,7 +44,7 @@ public AccountController( public async Task Login(string returnUrl = null) { // Clear the existing external cookie to ensure a clean login process - await HttpContext.Authentication.SignOutAsync(_externalCookieScheme); + await HttpContext.Authentication.SignOutAsync(IdentityConstants.ExternalScheme); ViewData["ReturnUrl"] = returnUrl; return View(); diff --git a/Sqlite/Sqlite/Controllers/ManageController.cs b/Sqlite/Sqlite/Controllers/ManageController.cs index 4004099..68c094c 100644 --- a/Sqlite/Sqlite/Controllers/ManageController.cs +++ b/Sqlite/Sqlite/Controllers/ManageController.cs @@ -18,7 +18,6 @@ public class ManageController : Controller { private readonly UserManager _userManager; private readonly SignInManager _signInManager; - private readonly string _externalCookieScheme; private readonly IEmailSender _emailSender; private readonly ISmsSender _smsSender; private readonly ILogger _logger; @@ -26,14 +25,12 @@ public class ManageController : Controller public ManageController( UserManager userManager, SignInManager signInManager, - IOptions identityCookieOptions, IEmailSender emailSender, ISmsSender smsSender, ILoggerFactory loggerFactory) { _userManager = userManager; _signInManager = signInManager; - _externalCookieScheme = identityCookieOptions.Value.ExternalCookieAuthenticationScheme; _emailSender = emailSender; _smsSender = smsSender; _logger = loggerFactory.CreateLogger(); @@ -291,7 +288,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.Name != ul.LoginProvider)).ToList(); ViewData["ShowRemoveButton"] = user.PasswordHash != null || userLogins.Count > 1; return View(new ManageLoginsViewModel { @@ -307,7 +304,7 @@ public async Task ManageLogins(ManageMessageId? message = null) public async Task LinkLogin(string provider) { // Clear the existing external cookie to ensure a clean login process - await HttpContext.Authentication.SignOutAsync(_externalCookieScheme); + await HttpContext.Authentication.SignOutAsync(IdentityConstants.ExternalScheme); // Request a redirect to the external login provider to link a login for the current user var redirectUrl = Url.Action(nameof(LinkLoginCallback), "Manage"); @@ -336,7 +333,7 @@ public async Task LinkLoginCallback() { message = ManageMessageId.AddLoginSuccess; // Clear the existing external cookie to ensure a clean login process - await HttpContext.Authentication.SignOutAsync(_externalCookieScheme); + await HttpContext.Authentication.SignOutAsync(IdentityConstants.ExternalScheme); } return RedirectToAction(nameof(ManageLogins), new { Message = message }); } diff --git a/Sqlite/Sqlite/EfSqlite.csproj b/Sqlite/Sqlite/EfSqlite.csproj index e75a45d..ce68053 100644 --- a/Sqlite/Sqlite/EfSqlite.csproj +++ b/Sqlite/Sqlite/EfSqlite.csproj @@ -1,38 +1,23 @@  - netcoreapp1.1 + netcoreapp2.0 - $(PackageTargetFallback);portable-net45+win8+wp8+wpa81; + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81 aspnet-Sqlite-5f842dd2-e86e-4bd6-9a88-e2d35188f2ae - - - - - - - - - - - - - - - - + - - - + + + diff --git a/Sqlite/Sqlite/Models/ApplicationUser.cs b/Sqlite/Sqlite/Models/ApplicationUser.cs index 82c841a..74fa19e 100644 --- a/Sqlite/Sqlite/Models/ApplicationUser.cs +++ b/Sqlite/Sqlite/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; namespace Sqlite.Models { diff --git a/Sqlite/Sqlite/Models/ManageViewModels/ManageLoginsViewModel.cs b/Sqlite/Sqlite/Models/ManageViewModels/ManageLoginsViewModel.cs index b6e2fc4..644ebaa 100644 --- a/Sqlite/Sqlite/Models/ManageViewModels/ManageLoginsViewModel.cs +++ b/Sqlite/Sqlite/Models/ManageViewModels/ManageLoginsViewModel.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http.Authentication; +using System.Collections.Generic; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Identity; namespace Sqlite.Models.ManageViewModels @@ -11,6 +8,6 @@ public class ManageLoginsViewModel { public IList CurrentLogins { get; set; } - public IList OtherLogins { get; set; } + public IList OtherLogins { get; set; } } } diff --git a/Sqlite/Sqlite/Program.cs b/Sqlite/Sqlite/Program.cs index fd27225..07d92b1 100644 --- a/Sqlite/Sqlite/Program.cs +++ b/Sqlite/Sqlite/Program.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace Sqlite @@ -11,15 +7,12 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() - .UseApplicationInsights() .Build(); - - host.Run(); - } } } diff --git a/Sqlite/Sqlite/Startup.cs b/Sqlite/Sqlite/Startup.cs index 1ee8965..5a39151 100644 --- a/Sqlite/Sqlite/Startup.cs +++ b/Sqlite/Sqlite/Startup.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -13,6 +8,7 @@ using Sqlite.Models; using Sqlite.Services; using EfSqlite.Data; +using Microsoft.AspNetCore.Identity; namespace Sqlite { @@ -77,7 +73,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF app.UseStaticFiles(); - app.UseIdentity(); + app.UseAuthentication(); // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715 diff --git a/Sqlite/Sqlite/Views/Account/Login.cshtml b/Sqlite/Sqlite/Views/Account/Login.cshtml index d803819..8887486 100644 --- a/Sqlite/Sqlite/Views/Account/Login.cshtml +++ b/Sqlite/Sqlite/Views/Account/Login.cshtml @@ -1,6 +1,4 @@ -@using System.Collections.Generic -@using Microsoft.AspNetCore.Http -@using Microsoft.AspNetCore.Http.Authentication +@using System.Linq @model LoginViewModel @inject SignInManager SignInManager @@ -59,7 +57,7 @@

Use another service to log in.


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

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