-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue-80: update sample project with identity
- Loading branch information
1 parent
e1db896
commit 73618e0
Showing
127 changed files
with
43,327 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-4 KB
(96%)
src/Services/auth/O2NextGen.Auth/O2NextGen.Auth.Reference/app.db
Binary file not shown.
21 changes: 21 additions & 0 deletions
21
...rvices/auth/O2NextGen.Auth/O2NextGen.Auth.Sample/Areas/Identity/IdentityHostingStartup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Identity.UI; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using O2NextGen.Auth.Sample.Data; | ||
|
||
[assembly: HostingStartup(typeof(O2NextGen.Auth.Sample.Areas.Identity.IdentityHostingStartup))] | ||
namespace O2NextGen.Auth.Sample.Areas.Identity | ||
{ | ||
public class IdentityHostingStartup : IHostingStartup | ||
{ | ||
public void Configure(IWebHostBuilder builder) | ||
{ | ||
builder.ConfigureServices((context, services) => { | ||
}); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...uth/O2NextGen.Auth/O2NextGen.Auth.Sample/Areas/Identity/Pages/Account/AccessDenied.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@page | ||
@model AccessDeniedModel | ||
@{ | ||
ViewData["Title"] = "Access denied"; | ||
} | ||
|
||
<header> | ||
<h1 class="text-danger">@ViewData["Title"]</h1> | ||
<p class="text-danger">You do not have access to this resource.</p> | ||
</header> |
17 changes: 17 additions & 0 deletions
17
.../O2NextGen.Auth/O2NextGen.Auth.Sample/Areas/Identity/Pages/Account/AccessDenied.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace O2NextGen.Auth.Sample.Areas.Identity.Pages.Account | ||
{ | ||
public class AccessDeniedModel : PageModel | ||
{ | ||
public void OnGet() | ||
{ | ||
|
||
} | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
...uth/O2NextGen.Auth/O2NextGen.Auth.Sample/Areas/Identity/Pages/Account/ConfirmEmail.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
@page | ||
@model ConfirmEmailModel | ||
@{ | ||
ViewData["Title"] = "Confirm email"; | ||
} | ||
|
||
<h1>@ViewData["Title"]</h1> | ||
<div> | ||
<p> | ||
Thank you for confirming your email. | ||
</p> | ||
</div> |
44 changes: 44 additions & 0 deletions
44
.../O2NextGen.Auth/O2NextGen.Auth.Sample/Areas/Identity/Pages/Account/ConfirmEmail.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace O2NextGen.Auth.Sample.Areas.Identity.Pages.Account | ||
{ | ||
[AllowAnonymous] | ||
public class ConfirmEmailModel : PageModel | ||
{ | ||
private readonly UserManager<IdentityUser> _userManager; | ||
|
||
public ConfirmEmailModel(UserManager<IdentityUser> userManager) | ||
{ | ||
_userManager = userManager; | ||
} | ||
|
||
public async Task<IActionResult> OnGetAsync(string userId, string code) | ||
{ | ||
if (userId == null || code == null) | ||
{ | ||
return RedirectToPage("/Index"); | ||
} | ||
|
||
var user = await _userManager.FindByIdAsync(userId); | ||
if (user == null) | ||
{ | ||
return NotFound($"Unable to load user with ID '{userId}'."); | ||
} | ||
|
||
var result = await _userManager.ConfirmEmailAsync(user, code); | ||
if (!result.Succeeded) | ||
{ | ||
throw new InvalidOperationException($"Error confirming email for user with ID '{userId}':"); | ||
} | ||
|
||
return Page(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...th/O2NextGen.Auth/O2NextGen.Auth.Sample/Areas/Identity/Pages/Account/ExternalLogin.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
@page | ||
@model ExternalLoginModel | ||
@{ | ||
ViewData["Title"] = "Register"; | ||
} | ||
|
||
<h1>@ViewData["Title"]</h1> | ||
<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-primary">Register</button> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
@section Scripts { | ||
<partial name="_ValidationScriptsPartial" /> | ||
} |
140 changes: 140 additions & 0 deletions
140
...O2NextGen.Auth/O2NextGen.Auth.Sample/Areas/Identity/Pages/Account/ExternalLogin.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace O2NextGen.Auth.Sample.Areas.Identity.Pages.Account | ||
{ | ||
[AllowAnonymous] | ||
public class ExternalLoginModel : PageModel | ||
{ | ||
private readonly SignInManager<IdentityUser> _signInManager; | ||
private readonly UserManager<IdentityUser> _userManager; | ||
private readonly ILogger<ExternalLoginModel> _logger; | ||
|
||
public ExternalLoginModel( | ||
SignInManager<IdentityUser> signInManager, | ||
UserManager<IdentityUser> userManager, | ||
ILogger<ExternalLoginModel> logger) | ||
{ | ||
_signInManager = signInManager; | ||
_userManager = userManager; | ||
_logger = logger; | ||
} | ||
|
||
[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"); | ||
} | ||
|
||
public 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); | ||
} | ||
|
||
public async Task<IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null) | ||
{ | ||
returnUrl = returnUrl ?? Url.Content("~/"); | ||
if (remoteError != null) | ||
{ | ||
ErrorMessage = $"Error from external provider: {remoteError}"; | ||
return RedirectToPage("./Login", new {ReturnUrl = returnUrl }); | ||
} | ||
var info = await _signInManager.GetExternalLoginInfoAsync(); | ||
if (info == null) | ||
{ | ||
ErrorMessage = "Error loading external login information."; | ||
return RedirectToPage("./Login", new { ReturnUrl = returnUrl }); | ||
} | ||
|
||
// 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) | ||
{ | ||
_logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider); | ||
return LocalRedirect(returnUrl); | ||
} | ||
if (result.IsLockedOut) | ||
{ | ||
return RedirectToPage("./Lockout"); | ||
} | ||
else | ||
{ | ||
// 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(); | ||
} | ||
} | ||
|
||
public async Task<IActionResult> OnPostConfirmationAsync(string returnUrl = null) | ||
{ | ||
returnUrl = returnUrl ?? Url.Content("~/"); | ||
// Get the information about the user from the external login provider | ||
var info = await _signInManager.GetExternalLoginInfoAsync(); | ||
if (info == null) | ||
{ | ||
ErrorMessage = "Error loading external login information during confirmation."; | ||
return RedirectToPage("./Login", new { ReturnUrl = returnUrl }); | ||
} | ||
|
||
if (ModelState.IsValid) | ||
{ | ||
var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; | ||
var result = await _userManager.CreateAsync(user); | ||
if (result.Succeeded) | ||
{ | ||
result = await _userManager.AddLoginAsync(user, info); | ||
if (result.Succeeded) | ||
{ | ||
await _signInManager.SignInAsync(user, isPersistent: false); | ||
_logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); | ||
return LocalRedirect(returnUrl); | ||
} | ||
} | ||
foreach (var error in result.Errors) | ||
{ | ||
ModelState.AddModelError(string.Empty, error.Description); | ||
} | ||
} | ||
|
||
LoginProvider = info.LoginProvider; | ||
ReturnUrl = returnUrl; | ||
return Page(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...h/O2NextGen.Auth/O2NextGen.Auth.Sample/Areas/Identity/Pages/Account/ForgotPassword.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@page | ||
@model ForgotPasswordModel | ||
@{ | ||
ViewData["Title"] = "Forgot your password?"; | ||
} | ||
|
||
<h1>@ViewData["Title"]</h1> | ||
<h4>Enter your email.</h4> | ||
<hr /> | ||
<div class="row"> | ||
<div class="col-md-4"> | ||
<form 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-primary">Submit</button> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
@section Scripts { | ||
<partial name="_ValidationScriptsPartial" /> | ||
} |
Oops, something went wrong.