From 66077639a6dc36ee80ba85fb6020ace2057b75cc Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Wed, 3 Jan 2018 13:07:43 +0300 Subject: [PATCH] Minor refactoring... --- .../Pages/Account/ExternalLogin.cshtml.cs | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/Volo.Abp.Account.Web/Pages/Account/ExternalLogin.cshtml.cs b/src/Volo.Abp.Account.Web/Pages/Account/ExternalLogin.cshtml.cs index ccbe25a167..6ea57b97a7 100644 --- a/src/Volo.Abp.Account.Web/Pages/Account/ExternalLogin.cshtml.cs +++ b/src/Volo.Abp.Account.Web/Pages/Account/ExternalLogin.cshtml.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Volo.Abp.Identity; +using Volo.Abp.Ui; using Volo.Abp.Uow; namespace Volo.Abp.Account.Web.Pages.Account @@ -61,6 +62,7 @@ namespace Volo.Abp.Account.Web.Pages.Account ErrorMessage = $"Error from external provider: {remoteError}"; return RedirectToPage("./Login"); } + var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { @@ -76,12 +78,13 @@ namespace Volo.Abp.Account.Web.Pages.Account if (result.IsLockedOut) { - return RedirectToPage("./Lockout"); + throw new UserFriendlyException("Cannot proceed because user is locked out!"); } - // If the user does not have an account, then ask the user to create an account. ReturnUrl = returnUrl; LoginProvider = info.LoginProvider; + + //User does not have an account, create an account. var success = await CreateUserAsync(returnUrl, returnUrlHash); if (success) @@ -95,40 +98,41 @@ namespace Volo.Abp.Account.Web.Pages.Account [UnitOfWork] public virtual async Task CreateUserAsync(string returnUrl = null, string returnUrlHash = null) { - if (ModelState.IsValid) + 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."); - } + return false; + } + + // 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 IdentityUser(GuidGenerator.Create(), info.Principal.FindFirstValue(ClaimTypes.Email)); + var user = new IdentityUser(GuidGenerator.Create(), info.Principal.FindFirstValue(ClaimTypes.Email)); - var result = await _userManager.CreateAsync(user); + var result = await _userManager.CreateAsync(user); - //CheckIdentityErrors( await _userManager.CreateAsync(user)); + //todo: needs to check identity errors? + //CheckIdentityErrors( await _userManager.CreateAsync(user)); + if (result.Succeeded) + { + result = await _userManager.AddLoginAsync(user, info); if (result.Succeeded) { - result = await _userManager.AddLoginAsync(user, info); - if (result.Succeeded) - { - await _signInManager.SignInAsync(user, isPersistent: false); - - return true; - } + await _signInManager.SignInAsync(user, false); + return true; } + } - foreach (var error in result.Errors) - { - ModelState.AddModelError(string.Empty, error.Description); - } + foreach (var error in result.Errors) + { + ModelState.AddModelError(string.Empty, error.Description); } return false; - } } } \ No newline at end of file