mirror of https://github.com/abpframework/abp.git
7 changed files with 236 additions and 22 deletions
Binary file not shown.
@ -0,0 +1,29 @@ |
|||
@page |
|||
@model Volo.Abp.Account.Web.Pages.Account.ExternalLoginModel |
|||
@{ |
|||
ViewData["Title"] = "Register"; |
|||
} |
|||
|
|||
<h2>@ViewData["Title"]</h2> |
|||
<h4>Associate your @Model.LoginProvider account.</h4> |
|||
<hr /> |
|||
|
|||
<p class="text-info"> |
|||
You've successfully authenticated with <strong>@Model.LoginProvider</strong>. |
|||
Please enter an email address for this site below and click the Register button to finish |
|||
logging in. |
|||
</p> |
|||
|
|||
<div class="row"> |
|||
<div class="col-md-4"> |
|||
<form asp-page-handler="Confirmation" asp-route-returnUrl="@Model.ReturnUrl" method="post"> |
|||
<div asp-validation-summary="All" class="text-danger"></div> |
|||
<div class="form-group"> |
|||
<label asp-for="Input.Email"></label> |
|||
<input asp-for="Input.Email" class="form-control" /> |
|||
<span asp-validation-for="Input.Email" class="text-danger"></span> |
|||
</div> |
|||
<button type="submit" class="btn btn-default">Register</button> |
|||
</form> |
|||
</div> |
|||
</div> |
|||
@ -0,0 +1,133 @@ |
|||
using System; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.Security.Claims; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Volo.Abp.Identity; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace Volo.Abp.Account.Web.Pages.Account |
|||
{ |
|||
public class ExternalLoginModel : AccountModelBase |
|||
{ |
|||
private readonly SignInManager<IdentityUser> _signInManager; |
|||
private readonly UserManager<IdentityUser> _userManager; |
|||
|
|||
public ExternalLoginModel( |
|||
SignInManager<IdentityUser> signInManager, |
|||
UserManager<IdentityUser> userManager) |
|||
{ |
|||
_signInManager = signInManager; |
|||
_userManager = userManager; |
|||
} |
|||
|
|||
[BindProperty] |
|||
public InputModel Input { get; set; } |
|||
|
|||
public string LoginProvider { get; set; } |
|||
|
|||
public string ReturnUrl { get; set; } |
|||
|
|||
[TempData] |
|||
public string ErrorMessage { get; set; } |
|||
|
|||
public class InputModel |
|||
{ |
|||
[Required] |
|||
[EmailAddress] |
|||
public string Email { get; set; } |
|||
} |
|||
|
|||
public IActionResult OnGetAsync() |
|||
{ |
|||
return RedirectToPage("./Login"); |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual IActionResult OnPost(string provider, string returnUrl = null) |
|||
{ |
|||
// Request a redirect to the external login provider.
|
|||
var redirectUrl = Url.Page("./ExternalLogin", pageHandler: "Callback", values: new { returnUrl }); |
|||
var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); |
|||
return new ChallengeResult(provider, properties); |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task<IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null, string returnUrlHash = "") |
|||
{ |
|||
if (remoteError != null) |
|||
{ |
|||
ErrorMessage = $"Error from external provider: {remoteError}"; |
|||
return RedirectToPage("./Login"); |
|||
} |
|||
var info = await _signInManager.GetExternalLoginInfoAsync(); |
|||
if (info == null) |
|||
{ |
|||
return RedirectToPage("./Login"); |
|||
} |
|||
|
|||
// 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); |
|||
if (result.Succeeded) |
|||
{ |
|||
return RedirectSafely(returnUrl, returnUrlHash); |
|||
} |
|||
|
|||
if (result.IsLockedOut) |
|||
{ |
|||
return RedirectToPage("./Lockout"); |
|||
} |
|||
|
|||
// If the user does not have an account, then ask the user to create an account.
|
|||
ReturnUrl = returnUrl; |
|||
LoginProvider = info.LoginProvider; |
|||
if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email)) |
|||
{ |
|||
Input = new InputModel |
|||
{ |
|||
Email = info.Principal.FindFirstValue(ClaimTypes.Email) |
|||
}; |
|||
} |
|||
return Page(); |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null, string returnUrlHash = null) |
|||
{ |
|||
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 IdentityUser(GuidGenerator.Create(), Input.Email); |
|||
|
|||
var result = await _userManager.CreateAsync(user); |
|||
|
|||
//CheckIdentityErrors( await _userManager.CreateAsync(user));
|
|||
|
|||
if (result.Succeeded) |
|||
{ |
|||
result = await _userManager.AddLoginAsync(user, info); |
|||
if (result.Succeeded) |
|||
{ |
|||
await _signInManager.SignInAsync(user, isPersistent: false); |
|||
|
|||
return RedirectSafely(returnUrl, returnUrlHash); |
|||
} |
|||
} |
|||
foreach (var error in result.Errors) |
|||
{ |
|||
ModelState.AddModelError(string.Empty, error.Description); |
|||
} |
|||
} |
|||
|
|||
ReturnUrl = returnUrl; |
|||
return Page(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,27 +1,61 @@ |
|||
@page |
|||
@model Volo.Abp.Account.Web.Pages.Account.LoginModel |
|||
<div class="row"> |
|||
<div class="col-md-3"> |
|||
<form method="post"> |
|||
<div class="form-group"> |
|||
<label for="UsernameOrEmailAddress">Username or email address</label> |
|||
<input type="text" class="form-control" id="UsernameOrEmailAddress" name="UsernameOrEmailAddress"> |
|||
<div class="col-md-6"> |
|||
<div class="col-md-4"> |
|||
<form method="post"> |
|||
<div class="form-group"> |
|||
<label for="UsernameOrEmailAddress">Username or email address</label> |
|||
<input type="text" class="form-control" id="UsernameOrEmailAddress" name="UsernameOrEmailAddress"> |
|||
</div> |
|||
<div class="form-group"> |
|||
<label for="Password">Password</label> |
|||
<input type="password" class="form-control" id="Password" name="Password"> |
|||
</div> |
|||
<div class="form-check"> |
|||
<label class="form-check-label"> |
|||
<input type="checkbox" name="RememberMe" value="true" checked="checked"> |
|||
Remember me |
|||
</label> |
|||
</div> |
|||
<button type="submit" class="btn btn-primary">Login</button> |
|||
</form> |
|||
|
|||
<div style="padding-top: 20px"> |
|||
<a href="@Url.Page("./Register", new { returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash })">Register</a> |
|||
</div> |
|||
<div class="form-group"> |
|||
<label for="Password">Password</label> |
|||
<input type="password" class="form-control" id="Password" name="Password"> |
|||
</div> |
|||
<div class="form-check"> |
|||
<label class="form-check-label"> |
|||
<input type="checkbox" name="RememberMe" value="true" checked="checked"> |
|||
Remember me |
|||
</label> |
|||
</div> |
|||
<button type="submit" class="btn btn-primary">Login</button> |
|||
</form> |
|||
|
|||
<div> |
|||
<a href="@Url.Page("./Register", new { returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash })">Register</a> |
|||
</div> |
|||
</div> |
|||
|
|||
<div class="col-md-6 col-md-offset-2"> |
|||
<section> |
|||
<h4>Use another service to log in.</h4> |
|||
<hr /> |
|||
@{ |
|||
if ((Model.ExternalLogins?.Count ?? 0) == 0) |
|||
{ |
|||
<div> |
|||
<p> |
|||
There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a> |
|||
for details on setting up this ASP.NET application to support logging in via external services. |
|||
</p> |
|||
</div> |
|||
} |
|||
else |
|||
{ |
|||
<form asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal"> |
|||
<div> |
|||
<p> |
|||
@foreach (var provider in Model.ExternalLogins) |
|||
{ |
|||
<button type="submit" class="btn btn-default" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button> |
|||
} |
|||
</p> |
|||
</div> |
|||
</form> |
|||
} |
|||
} |
|||
</section> |
|||
</div> |
|||
|
|||
</div> |
|||
Loading…
Reference in new issue