-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Intro to Identity to 2.1 (#7930)
* WIP:Update Intro to Identity to 2.1 * work * work * work * work * work * work * work * minor corrections * more typos * react to feedback
- Loading branch information
1 parent
9e7489c
commit 30c029e
Showing
13 changed files
with
528 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
### View the Identity database | ||
|
||
# [Visual Studio](#tab/visual-studio) | ||
|
||
* From the **View** menu, select **SQL Server Object Explorer** (SSOX). | ||
* Navigate to **(localdb)MSSQLLocalDB(SQL Server 13)**. Right-click on **dbo.AspNetUsers** > **View Data**: | ||
|
||
 | ||
|
||
# [.NET Core CLI](#tab/netcore-cli) | ||
|
||
There are many third party tools you can download to manage and view a SQLite database, for example [DB Browser for SQLite](http://sqlitebrowser.org/). | ||
|
||
------ |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file removed
BIN
-117 KB
aspnetcore/security/authentication/identity/_static/01-new-project.png
Binary file not shown.
Binary file removed
BIN
-113 KB
aspnetcore/security/authentication/identity/_static/02-new-project.png
Binary file not shown.
Binary file removed
BIN
-30.6 KB
aspnetcore/security/authentication/identity/_static/03-new-project-auth.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions
16
...tcore/security/authentication/identity/sample/src/ASPNETv2.1-IdentityDemo/About.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,16 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace WebApp1.Pages | ||
{ | ||
[Authorize] | ||
public class AboutModel : PageModel | ||
{ | ||
public string Message { get; set; } | ||
|
||
public void OnGet() | ||
{ | ||
Message = "Your application description page."; | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...tcore/security/authentication/identity/sample/src/ASPNETv2.1-IdentityDemo/Login.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,107 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebApp1.Areas.Identity.Pages.Account | ||
{ | ||
[AllowAnonymous] | ||
public class LoginModel : PageModel | ||
{ | ||
private readonly SignInManager<IdentityUser> _signInManager; | ||
private readonly ILogger<LoginModel> _logger; | ||
|
||
public LoginModel(SignInManager<IdentityUser> signInManager, ILogger<LoginModel> logger) | ||
{ | ||
_signInManager = signInManager; | ||
_logger = logger; | ||
} | ||
|
||
[BindProperty] | ||
public InputModel Input { get; set; } | ||
|
||
public IList<AuthenticationScheme> ExternalLogins { get; set; } | ||
|
||
public string ReturnUrl { get; set; } | ||
|
||
[TempData] | ||
public string ErrorMessage { get; set; } | ||
|
||
public class InputModel | ||
{ | ||
[Required] | ||
[EmailAddress] | ||
public string Email { get; set; } | ||
|
||
[Required] | ||
[DataType(DataType.Password)] | ||
public string Password { get; set; } | ||
|
||
[Display(Name = "Remember me?")] | ||
public bool RememberMe { get; set; } | ||
} | ||
|
||
public async Task OnGetAsync(string returnUrl = null) | ||
{ | ||
if (!string.IsNullOrEmpty(ErrorMessage)) | ||
{ | ||
ModelState.AddModelError(string.Empty, ErrorMessage); | ||
} | ||
|
||
returnUrl = returnUrl ?? Url.Content("~/"); | ||
|
||
// Clear the existing external cookie to ensure a clean login process | ||
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); | ||
|
||
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); | ||
|
||
ReturnUrl = returnUrl; | ||
} | ||
|
||
#region snippet | ||
public async Task<IActionResult> OnPostAsync(string returnUrl = null) | ||
{ | ||
returnUrl = returnUrl ?? Url.Content("~/"); | ||
|
||
if (ModelState.IsValid) | ||
{ | ||
// This doesn't count login failures towards account lockout | ||
// To enable password failures to trigger account lockout, | ||
// set lockoutOnFailure: true | ||
var result = await _signInManager.PasswordSignInAsync(Input.Email, | ||
Input.Password, Input.RememberMe, lockoutOnFailure: true); | ||
if (result.Succeeded) | ||
{ | ||
_logger.LogInformation("User logged in."); | ||
return LocalRedirect(returnUrl); | ||
} | ||
if (result.RequiresTwoFactor) | ||
{ | ||
return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, | ||
RememberMe = Input.RememberMe }); | ||
} | ||
if (result.IsLockedOut) | ||
{ | ||
_logger.LogWarning("User account locked out."); | ||
return RedirectToPage("./Lockout"); | ||
} | ||
else | ||
{ | ||
ModelState.AddModelError(string.Empty, "Invalid login attempt."); | ||
return Page(); | ||
} | ||
} | ||
|
||
// If we got this far, something failed, redisplay form | ||
return Page(); | ||
} | ||
#endregion | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...core/security/authentication/identity/sample/src/ASPNETv2.1-IdentityDemo/Logout.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,43 @@ | ||
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; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebApp1.Areas.Identity.Pages.Account | ||
{ | ||
[AllowAnonymous] | ||
public class LogoutModel : PageModel | ||
{ | ||
private readonly SignInManager<IdentityUser> _signInManager; | ||
private readonly ILogger<LogoutModel> _logger; | ||
|
||
public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger) | ||
{ | ||
_signInManager = signInManager; | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
} | ||
|
||
public async Task<IActionResult> OnPost(string returnUrl = null) | ||
{ | ||
await _signInManager.SignOutAsync(); | ||
_logger.LogInformation("User logged out."); | ||
if (returnUrl != null) | ||
{ | ||
return LocalRedirect(returnUrl); | ||
} | ||
else | ||
{ | ||
return Page(); | ||
} | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...re/security/authentication/identity/sample/src/ASPNETv2.1-IdentityDemo/Register.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,100 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Identity.UI.Services; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WebApp1.Areas.Identity.Pages.Account | ||
{ | ||
[AllowAnonymous] | ||
public class RegisterModel : PageModel | ||
{ | ||
private readonly SignInManager<IdentityUser> _signInManager; | ||
private readonly UserManager<IdentityUser> _userManager; | ||
private readonly ILogger<RegisterModel> _logger; | ||
private readonly IEmailSender _emailSender; | ||
|
||
public RegisterModel( | ||
UserManager<IdentityUser> userManager, | ||
SignInManager<IdentityUser> signInManager, | ||
ILogger<RegisterModel> logger, | ||
IEmailSender emailSender) | ||
{ | ||
_userManager = userManager; | ||
_signInManager = signInManager; | ||
_logger = logger; | ||
_emailSender = emailSender; | ||
} | ||
|
||
[BindProperty] | ||
public InputModel Input { get; set; } | ||
|
||
public string ReturnUrl { get; set; } | ||
|
||
public class InputModel | ||
{ | ||
[Required] | ||
[EmailAddress] | ||
[Display(Name = "Email")] | ||
public string Email { get; set; } | ||
|
||
[Required] | ||
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] | ||
[DataType(DataType.Password)] | ||
[Display(Name = "Password")] | ||
public string Password { get; set; } | ||
|
||
[DataType(DataType.Password)] | ||
[Display(Name = "Confirm password")] | ||
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] | ||
public string ConfirmPassword { get; set; } | ||
} | ||
|
||
public void OnGet(string returnUrl = null) | ||
{ | ||
ReturnUrl = returnUrl; | ||
} | ||
|
||
#region snippet | ||
public async Task<IActionResult> OnPostAsync(string returnUrl = null) | ||
{ | ||
returnUrl = returnUrl ?? Url.Content("~/"); | ||
if (ModelState.IsValid) | ||
{ | ||
var user = new IdentityUser { UserName = Input.Email, Email = Input.Email }; | ||
var result = await _userManager.CreateAsync(user, Input.Password); | ||
if (result.Succeeded) | ||
{ | ||
_logger.LogInformation("User created a new account with password."); | ||
|
||
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); | ||
var callbackUrl = Url.Page( | ||
"/Account/ConfirmEmail", | ||
pageHandler: null, | ||
values: new { userId = user.Id, code = code }, | ||
protocol: Request.Scheme); | ||
|
||
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", | ||
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); | ||
|
||
await _signInManager.SignInAsync(user, isPersistent: false); | ||
return LocalRedirect(returnUrl); | ||
} | ||
foreach (var error in result.Errors) | ||
{ | ||
ModelState.AddModelError(string.Empty, error.Description); | ||
} | ||
} | ||
|
||
// If we got this far, something failed, redisplay form | ||
return Page(); | ||
} | ||
#endregion | ||
} | ||
} |
Oops, something went wrong.