From 26ea920fa6244115503942f5faf2bbf47ca34224 Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Tue, 12 Dec 2017 11:27:11 -0800 Subject: [PATCH] Set LoginProvider when ExternalLoginConfirmation fails (#187) Addresses #98 --- .../Pages/Account/ExternalLogin.cshtml.cs | 16 +++++++++------- .../Identity/Controllers/AccountController.cs | 15 +++++++++------ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/ExternalLogin.cshtml.cs b/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/ExternalLogin.cshtml.cs index fc19f502b9..c613b5f751 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/ExternalLogin.cshtml.cs +++ b/src/Microsoft.DotNet.Web.ProjectTemplates/content/RazorPagesWeb-CSharp/Pages/Account/ExternalLogin.cshtml.cs @@ -72,7 +72,7 @@ namespace Company.WebApplication1.Pages.Account } // Sign in the user with this external login provider if the user already has a login. - var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor : true); + var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true); if (result.Succeeded) { _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider); @@ -100,14 +100,15 @@ namespace Company.WebApplication1.Pages.Account public async Task OnPostConfirmationAsync(string returnUrl = null) { + // Get the information about the user from the external login provider + var info = await _signInManager.GetExternalLoginInfoAsync(); + if (info == null) + { + throw new ApplicationException("Error loading external login information during confirmation."); + } + if (ModelState.IsValid) { - // Get the information about the user from the external login provider - var info = await _signInManager.GetExternalLoginInfoAsync(); - if (info == null) - { - throw new ApplicationException("Error loading external login information during confirmation."); - } var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) @@ -126,6 +127,7 @@ namespace Company.WebApplication1.Pages.Account } } + LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return Page(); } diff --git a/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Areas/Identity/Controllers/AccountController.cs b/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Areas/Identity/Controllers/AccountController.cs index aab6086c94..ea6ec226be 100644 --- a/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Areas/Identity/Controllers/AccountController.cs +++ b/src/Microsoft.DotNet.Web.ProjectTemplates/content/StarterWeb-CSharp/Areas/Identity/Controllers/AccountController.cs @@ -307,14 +307,15 @@ namespace Company.WebApplication1.Identity.Controllers [ValidateAntiForgeryToken] public async Task ExternalLoginConfirmation(ExternalLoginViewModel model, string returnUrl = null) { + // Get the information about the user from the external login provider + var info = await _signInManager.GetExternalLoginInfoAsync(); + if (info == null) + { + throw new ApplicationException("Error loading external login information during confirmation."); + } + if (ModelState.IsValid) { - // Get the information about the user from the external login provider - var info = await _signInManager.GetExternalLoginInfoAsync(); - if (info == null) - { - throw new ApplicationException("Error loading external login information during confirmation."); - } var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user); if (result.Succeeded) @@ -327,9 +328,11 @@ namespace Company.WebApplication1.Identity.Controllers return RedirectToLocal(returnUrl); } } + AddErrors(result); } + ViewData["LoginProvider"] = info.LoginProvider; ViewData["ReturnUrl"] = returnUrl; return View(nameof(ExternalLogin), model); }