Browse Source

Minor refactoring...

pull/188/head
Alper Ebicoglu 8 years ago
parent
commit
66077639a6
  1. 52
      src/Volo.Abp.Account.Web/Pages/Account/ExternalLogin.cshtml.cs

52
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.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Identity; using Volo.Abp.Identity;
using Volo.Abp.Ui;
using Volo.Abp.Uow; using Volo.Abp.Uow;
namespace Volo.Abp.Account.Web.Pages.Account namespace Volo.Abp.Account.Web.Pages.Account
@ -61,6 +62,7 @@ namespace Volo.Abp.Account.Web.Pages.Account
ErrorMessage = $"Error from external provider: {remoteError}"; ErrorMessage = $"Error from external provider: {remoteError}";
return RedirectToPage("./Login"); return RedirectToPage("./Login");
} }
var info = await _signInManager.GetExternalLoginInfoAsync(); var info = await _signInManager.GetExternalLoginInfoAsync();
if (info == null) if (info == null)
{ {
@ -76,12 +78,13 @@ namespace Volo.Abp.Account.Web.Pages.Account
if (result.IsLockedOut) 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; ReturnUrl = returnUrl;
LoginProvider = info.LoginProvider; LoginProvider = info.LoginProvider;
//User does not have an account, create an account.
var success = await CreateUserAsync(returnUrl, returnUrlHash); var success = await CreateUserAsync(returnUrl, returnUrlHash);
if (success) if (success)
@ -95,40 +98,41 @@ namespace Volo.Abp.Account.Web.Pages.Account
[UnitOfWork] [UnitOfWork]
public virtual async Task<bool> CreateUserAsync(string returnUrl = null, string returnUrlHash = null) public virtual async Task<bool> CreateUserAsync(string returnUrl = null, string returnUrlHash = null)
{ {
if (ModelState.IsValid) if (!ModelState.IsValid)
{ {
// Get the information about the user from the external login provider return false;
var info = await _signInManager.GetExternalLoginInfoAsync(); }
if (info == null)
{ // Get the information about the user from the external login provider
throw new ApplicationException("Error loading external login information during confirmation."); 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) if (result.Succeeded)
{ {
result = await _userManager.AddLoginAsync(user, info); await _signInManager.SignInAsync(user, false);
if (result.Succeeded) return true;
{
await _signInManager.SignInAsync(user, isPersistent: false);
return true;
}
} }
}
foreach (var error in result.Errors) foreach (var error in result.Errors)
{ {
ModelState.AddModelError(string.Empty, error.Description); ModelState.AddModelError(string.Empty, error.Description);
}
} }
return false; return false;
} }
} }
} }
Loading…
Cancel
Save