I'm currently utilizing ASP.NET Core Identity within my project. I have a form that includes fields for Name and Username. The issue arises when attempting to save values from the Name field, as it triggers an error stating that they are not in the correct Username format. For example, "John Doe" is deemed invalid due to not being in username format. How can I successfully create a custom column for Name so that the AspNetUsers table is adjusted to store these values separately in a distinct "Name" column rather than within the existing "Username" column?
Here are the steps I've taken thus far:
// ApplicationUser.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
namespace VideoRentalStore.Data
{
public class ApplicationUser : IdentityUser
{
public string Name { get; set; } // Added this new column to the ApplicationUser model.
public string Username { get; set; }
public override string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
}
Executed a new migration: *dotnet ef migrations create "20230216".* There were no changes detected in the 'migrations' directory.
Included a column in the view: Register.cshtml:
<form method="post">
@Html.AntiForgeryToken()
<div asp-validation-summary="All" class="text-danger"></div>
// New column
<div class="form-group">
<label asp-for="Input.Name"></label>
<input asp-for="Input.Name" class="form-control" />
<span asp-validation-for="Input.Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.Username"></label>
<input asp-for="Input.Username" class="form-control" />
<span asp-validation-for="Input.Username" class="text-danger"></span>
</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>
<div class="form-group">
<label asp-for="Input.Password"></label>
<input asp-for="Input.Password" class="form-control" />
<span asp-validation-for="Input.Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.ConfirmPassword"></label>
<input asp-for="Input.ConfirmPassword" class="form-control" type="password" />
<span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span>
</div>
<button type="submit" class="btn-primary">Register</button>
</form>
// Updated the input model and post action in Register.cshtml.cs:
public class InputModel
{
[Required]
[StringLength(200)]
[Display(Name = "Name")]
public string Name { get; set; }
[Required]
[StringLength(200)]
[Display(Name = "Username")]
public string Username { get; set; }
[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; }
}
[HttpPost]
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
Name = Input.Name,
UserName = Input.Username,
Email = Input.Email
};
A "Name" column has been added to the database, yet it continues to validate based on the Username format. My query revolves around whether the AspNetUsers table is the appropriate location for incorporating this new column.
// User form
https://i.sstatic.net/B4s11.png
// Invalid username message