-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Update Identity custom user data documentation for .NET 8/9/10 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
Open
Copilot
wants to merge
14
commits into
main
Choose a base branch
from
copilot/customize-identity-user-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
14 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 e7d6b3e
Change #region/#endregion snippet markers to // comment format in 9.x…
Copilot 89dd773
Add .NET 10 moniker section and 10.x sample; update 9.x moniker to bo…
Copilot b2cbd1a
Remove outdated author info from add-user-data.md
wadepickett c61fd35
Remove 9.x sample folder; redirect .NET 8 and 9 moniker sections to 1…
Copilot 8e9e5ef
Apply suggestion from @wadepickett
wadepickett 2fbfa7f
Apply suggestions from code review
wadepickett 74f6c52
Apply suggestions from code review
wadepickett 5fb1d29
Update project creation steps in add-user-data.md
wadepickett 37823bd
Apply suggestion from @wadepickett
wadepickett 98d8d7d
Corrected migration and update commands and instructions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...authentication/add-user-data/samples/10.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
11
...ty/authentication/add-user-data/samples/10.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
38
...ion/add-user-data/samples/10.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
130
.../add-user-data/samples/10.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 | ||
| { | ||
| // <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(); | ||
| } | ||
| } | ||
| // </snippet> | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
...dd-user-data/samples/10.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 | ||
|
wadepickett marked this conversation as resolved.
Outdated
|
||
| { | ||
| /// <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
15
...dd-user-data/samples/10.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
1
...-user-data/samples/10.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.
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.
Uh oh!
There was an error while loading. Please reload this page.