I have created a registration form with ASP.NET Core Identity, using the default fields ("Email", "Password", "Confirm Password") and a custom field "Name". When I enter a random string value into the name field and press "Register", I always receive the below error message:
"User name '' is invalid, can only contain letters or digits."
Things I have tried:
- I have set an option on the 'User' object for AllowedUserNameCharacters, but this doesn't seem to make a difference:
services.Configure<IdentityOptions>(options =>
{
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@ ";
}
My registration controller:
[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
Name = model.Name,
Email = model.Email
};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
return RedirectToAction("index", "Home");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
}
return View(model);
}
Regsitration View:
<form method="post">
<div asp-validation-summary="All" ></div>
<div >
<label asp-for="Name"></label>
<input asp-for="Name" />
<span asp-validation-for="Name" ></span>
</div>
<div >
<label asp-for="Email"></label>
<input asp-for="Email" />
<span asp-validation-for="Email" ></span>
</div>
<div >
<label asp-for="Password"></label>
<input asp-for="Password" />
<span asp-validation-for="Password" ></span>
</div>
<div >
<label asp-for="ConfirmPassword"></label>
<input asp-for="ConfirmPassword" />
<span asp-validation-for="ConfirmPassword" ></span>
</div>
<button type="submit" >Register</button>
</form>
My appDBContext:
public class AppDBContext : IdentityDbContext<ApplicationUser>
{
private readonly DbContextOptions _options;
public AppDBContext()
{
}
public AppDBContext(DbContextOptions options): base(options)
{
_options = options;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=Codefirst");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>()
.Property(e => e.Name)
.HasMaxLength(250);
modelBuilder.Entity<ApplicationUser>()
.Property(e => e.Email)
.HasMaxLength(250);
}
}
My applicationuser.cs class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
namespace SLE_System.Data
{
public class ApplicationUser : IdentityUser
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
}
If anyone has any ideas, these would be much appreciated?
Thanks,
Robert
CodePudding user response:
Here IdentiyUser Class is inherited
public class ApplicationUser : IdentityUser
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
And the IdentityUser class has following properties as per Microsoft documentation link there is property UserName which is also inherited.
By default string(Non Nullable Properties) datatype have default data annotation [Required("AllowEmptyStrings=true)].Since while send the registered data UserName is missing but it is required so it is throwing error.
CodePudding user response:
"UserName" is invalid, can only contain letters or digits.
is the default error message about UserName
property. In your Regsitration View, There is no input for UserName
, So when you use CreateAsync()
method to register user, The project will validate your properties and find UserName
property is null and return this error message.
So one simply method is give it a value when you register ApplicationUser.
[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
Name = model.Name,
UserName = model.Name,
Email = model.Email
};
//.......
But please note that when you input Name
property, You need to satisfy UserName
's validation rules. The default rule is to only support the following characters
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@ "
You can also custom it's validation rule by using:
services.Configure<IdentityOptions>(options =>
{
options.User.AllowedUserNameCharacters = "xxxxxx";
//..............