-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Update Identity custom user data documentation for .NET 8/9 with MVC instructions and troubleshooting #36222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b1eea87
Initial plan
Copilot 98e3e0f
Create .NET 9 sample by copying 6.x sample and updating to net9.0
Copilot 656ed24
Add .NET 9 and 8 moniker sections with MVC instructions, quick-start ...
Copilot b22fd13
Apply suggestion from @wadepickett
wadepickett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
424 changes: 410 additions & 14 deletions
aspnetcore/security/authentication/add-user-data.md
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
.../authentication/add-user-data/samples/9.x/SampleApp/Areas/Identity/Data/WebApp1Context.cs
This file contains hidden or 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,22 @@ | ||
| using Microsoft.AspNetCore.Identity; | ||
| using Microsoft.AspNetCore.Identity.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using WebApp1.Areas.Identity.Data; | ||
|
|
||
| namespace WebApp1.Data; | ||
|
|
||
| public class WebApp1Context : IdentityDbContext<WebApp1User> | ||
| { | ||
| public WebApp1Context(DbContextOptions<WebApp1Context> options) | ||
| : base(options) | ||
| { | ||
| } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder builder) | ||
| { | ||
| base.OnModelCreating(builder); | ||
| // Customize the ASP.NET Identity model and override the defaults if needed. | ||
| // For example, you can rename the ASP.NET Identity table names and more. | ||
| // Add your customizations after calling base.OnModelCreating(builder); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
...ity/authentication/add-user-data/samples/9.x/SampleApp/Areas/Identity/Data/WebApp1User.cs
This file contains hidden or 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,11 @@ | ||
| using Microsoft.AspNetCore.Identity; | ||
|
|
||
| namespace WebApp1.Areas.Identity.Data; | ||
|
|
||
| public class WebApp1User : IdentityUser | ||
| { | ||
| [PersonalData] | ||
| public string? Name { get; set; } | ||
| [PersonalData] | ||
| public DateTime DOB { get; set; } | ||
| } |
38 changes: 38 additions & 0 deletions
...tion/add-user-data/samples/9.x/SampleApp/Areas/Identity/Pages/Account/Manage/Index.cshtml
This file contains hidden or 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,38 @@ | ||
| @page | ||
| @model IndexModel | ||
| @{ | ||
| ViewData["Title"] = "Profile"; | ||
| ViewData["ActivePage"] = ManageNavPages.Index; | ||
| } | ||
|
|
||
| <h3>@ViewData["Title"]</h3> | ||
| <partial name="_StatusMessage" for="StatusMessage" /> | ||
| <div class="row"> | ||
| <div class="col-md-6"> | ||
| <form id="profile-form" method="post"> | ||
| <div asp-validation-summary="ModelOnly" class="text-danger"></div> | ||
| <div class="form-floating"> | ||
| <input asp-for="Username" class="form-control" disabled /> | ||
| <label asp-for="Username" class="form-label"></label> | ||
| </div> | ||
| <div class="form-floating"> | ||
| <input asp-for="Input.Name" class="form-control" /> | ||
| <label asp-for="Input.Name" class="form-label"></label> | ||
| </div> | ||
| <div class="form-floating"> | ||
| <input asp-for="Input.DOB" class="form-control" /> | ||
| <label asp-for="Input.DOB" class="form-label"></label> | ||
| </div> | ||
| <div class="form-floating"> | ||
| <input asp-for="Input.PhoneNumber" class="form-control" /> | ||
| <label asp-for="Input.PhoneNumber" class="form-label"></label> | ||
| <span asp-validation-for="Input.PhoneNumber" class="text-danger"></span> | ||
| </div> | ||
| <button id="update-profile-button" type="submit" class="w-100 btn btn-lg btn-primary">Save</button> | ||
| </form> | ||
| </div> | ||
| </div> | ||
|
|
||
| @section Scripts { | ||
| <partial name="_ValidationScriptsPartial" /> | ||
| } |
130 changes: 130 additions & 0 deletions
...n/add-user-data/samples/9.x/SampleApp/Areas/Identity/Pages/Account/Manage/Index.cshtml.cs
This file contains hidden or 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,130 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| #nullable disable | ||
|
|
||
| using System; | ||
| using System.ComponentModel.DataAnnotations; | ||
| using System.Text.Encodings.Web; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Identity; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.RazorPages; | ||
| using WebApp1.Areas.Identity.Data; | ||
|
|
||
| namespace WebApp1.Areas.Identity.Pages.Account.Manage | ||
| { | ||
| #region snippet | ||
| public class IndexModel : PageModel | ||
| { | ||
| private readonly UserManager<WebApp1User> _userManager; | ||
| private readonly SignInManager<WebApp1User> _signInManager; | ||
|
|
||
| public IndexModel( | ||
| UserManager<WebApp1User> userManager, | ||
| SignInManager<WebApp1User> signInManager) | ||
| { | ||
| _userManager = userManager; | ||
| _signInManager = signInManager; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public string Username { get; set; } | ||
|
|
||
| // Remaining API warnings ommited. | ||
|
|
||
| [TempData] | ||
| public string StatusMessage { get; set; } | ||
|
|
||
| [BindProperty] | ||
| public InputModel Input { get; set; } | ||
|
|
||
| public class InputModel | ||
| { | ||
| [Required] | ||
| [DataType(DataType.Text)] | ||
| [Display(Name = "Full name")] | ||
| public string Name { get; set; } | ||
|
|
||
| [Required] | ||
| [Display(Name = "Birth Date")] | ||
| [DataType(DataType.Date)] | ||
| public DateTime DOB { get; set; } | ||
|
|
||
| [Phone] | ||
| [Display(Name = "Phone number")] | ||
| public string PhoneNumber { get; set; } | ||
| } | ||
|
|
||
| private async Task LoadAsync(WebApp1User user) | ||
| { | ||
| var userName = await _userManager.GetUserNameAsync(user); | ||
| var phoneNumber = await _userManager.GetPhoneNumberAsync(user); | ||
|
|
||
| Username = userName; | ||
|
|
||
| Input = new InputModel | ||
| { | ||
| Name = user.Name, | ||
| DOB = user.DOB, | ||
| PhoneNumber = phoneNumber | ||
| }; | ||
| } | ||
|
|
||
| public async Task<IActionResult> OnGetAsync() | ||
| { | ||
| var user = await _userManager.GetUserAsync(User); | ||
| if (user == null) | ||
| { | ||
| return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); | ||
| } | ||
|
|
||
| await LoadAsync(user); | ||
| return Page(); | ||
| } | ||
|
|
||
| public async Task<IActionResult> OnPostAsync() | ||
| { | ||
| var user = await _userManager.GetUserAsync(User); | ||
| if (user == null) | ||
| { | ||
| return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); | ||
| } | ||
|
|
||
| if (!ModelState.IsValid) | ||
| { | ||
| await LoadAsync(user); | ||
| return Page(); | ||
| } | ||
|
|
||
| var phoneNumber = await _userManager.GetPhoneNumberAsync(user); | ||
| if (Input.PhoneNumber != phoneNumber) | ||
| { | ||
| var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber); | ||
| if (!setPhoneResult.Succeeded) | ||
| { | ||
| StatusMessage = "Unexpected error when trying to set phone number."; | ||
| return RedirectToPage(); | ||
| } | ||
| } | ||
|
|
||
| if (Input.Name != user.Name) | ||
| { | ||
| user.Name = Input.Name; | ||
| } | ||
|
|
||
| if (Input.DOB != user.DOB) | ||
| { | ||
| user.DOB = Input.DOB; | ||
| } | ||
|
|
||
| await _userManager.UpdateAsync(user); | ||
| await _signInManager.RefreshSignInAsync(user); | ||
| StatusMessage = "Your profile has been updated"; | ||
| return RedirectToPage(); | ||
| } | ||
| } | ||
| #endregion | ||
| } |
123 changes: 123 additions & 0 deletions
...add-user-data/samples/9.x/SampleApp/Areas/Identity/Pages/Account/Manage/ManageNavPages.cs
This file contains hidden or 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,123 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| #nullable disable | ||
|
|
||
| using System; | ||
| using Microsoft.AspNetCore.Mvc.Rendering; | ||
|
|
||
| namespace WebApp1.Areas.Identity.Pages.Account.Manage | ||
| { | ||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static class ManageNavPages | ||
| { | ||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string Index => "Index"; | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string Email => "Email"; | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string ChangePassword => "ChangePassword"; | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string DownloadPersonalData => "DownloadPersonalData"; | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string DeletePersonalData => "DeletePersonalData"; | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string ExternalLogins => "ExternalLogins"; | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string PersonalData => "PersonalData"; | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string TwoFactorAuthentication => "TwoFactorAuthentication"; | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string IndexNavClass(ViewContext viewContext) => PageNavClass(viewContext, Index); | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string EmailNavClass(ViewContext viewContext) => PageNavClass(viewContext, Email); | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string ChangePasswordNavClass(ViewContext viewContext) => PageNavClass(viewContext, ChangePassword); | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string DownloadPersonalDataNavClass(ViewContext viewContext) => PageNavClass(viewContext, DownloadPersonalData); | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string DeletePersonalDataNavClass(ViewContext viewContext) => PageNavClass(viewContext, DeletePersonalData); | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string ExternalLoginsNavClass(ViewContext viewContext) => PageNavClass(viewContext, ExternalLogins); | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string PersonalDataNavClass(ViewContext viewContext) => PageNavClass(viewContext, PersonalData); | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string TwoFactorAuthenticationNavClass(ViewContext viewContext) => PageNavClass(viewContext, TwoFactorAuthentication); | ||
|
|
||
| /// <summary> | ||
| /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used | ||
| /// directly from your code. This API may change or be removed in future releases. | ||
| /// </summary> | ||
| public static string PageNavClass(ViewContext viewContext, string page) | ||
| { | ||
| var activePage = viewContext.ViewData["ActivePage"] as string | ||
| ?? System.IO.Path.GetFileNameWithoutExtension(viewContext.ActionDescriptor.DisplayName); | ||
| return string.Equals(activePage, page, StringComparison.OrdinalIgnoreCase) ? "active" : null; | ||
| } | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
...add-user-data/samples/9.x/SampleApp/Areas/Identity/Pages/Account/Manage/_ManageNav.cshtml
This file contains hidden or 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,15 @@ | ||
| @inject SignInManager<WebApp1User> SignInManager | ||
| @{ | ||
| var hasExternalLogins = (await SignInManager.GetExternalAuthenticationSchemesAsync()).Any(); | ||
| } | ||
| <ul class="nav nav-pills flex-column"> | ||
| <li class="nav-item"><a class="nav-link @ManageNavPages.IndexNavClass(ViewContext)" id="profile" asp-page="./Index">Profile</a></li> | ||
| <li class="nav-item"><a class="nav-link @ManageNavPages.EmailNavClass(ViewContext)" id="email" asp-page="./Email">Email</a></li> | ||
| <li class="nav-item"><a class="nav-link @ManageNavPages.ChangePasswordNavClass(ViewContext)" id="change-password" asp-page="./ChangePassword">Password</a></li> | ||
| @if (hasExternalLogins) | ||
| { | ||
| <li id="external-logins" class="nav-item"><a id="external-login" class="nav-link @ManageNavPages.ExternalLoginsNavClass(ViewContext)" asp-page="./ExternalLogins">External logins</a></li> | ||
| } | ||
| <li class="nav-item"><a class="nav-link @ManageNavPages.TwoFactorAuthenticationNavClass(ViewContext)" id="two-factor" asp-page="./TwoFactorAuthentication">Two-factor authentication</a></li> | ||
| <li class="nav-item"><a class="nav-link @ManageNavPages.PersonalDataNavClass(ViewContext)" id="personal-data" asp-page="./PersonalData">Personal data</a></li> | ||
| </ul> |
1 change: 1 addition & 0 deletions
...d-user-data/samples/9.x/SampleApp/Areas/Identity/Pages/Account/Manage/_ViewImports.cshtml
This file contains hidden or 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 @@ | ||
| @using WebApp1.Areas.Identity.Pages.Account.Manage |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.