Issue
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" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword"></label>
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<button type="submit" class="btn-primary">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
Solution
"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";
//..............
Answered By - Xinran Shen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.