I have changed my Register Action Method to accept user Name instead of Email.
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = model.Name,
Email = model.Email,
};
But when I Enter Name in TextBox like Joe Smith It give me an error that is Invalid UserName. It is not allowing user Name to Accept White space. My Question is how can I modify the builtin User Name to allow space and special characters. I am using ASP.NET Core.
Thanks in Advance.
So cloudikka's answer had it right. I'll just add explicitly (cloudikka's link explained it as well) that you need to list ALL the characters you want to allow (not just the whitespace or special characters), like this:
and NOT this
options.User.AllowedUserNameCharacters = " ";
which means 'only whitespace characters allowed'. (I reckon this was Babar's problem.)As mentioned by @Jaffal the UserName should not allow special characters or white spaces. It should be considered as a unique user identifier (a.k.a. nickname or email). To get nicely displayed user name you might want to introduce a new property.
E.g. add a new property called
Name
or in some other implementation may be two fieldsGivenName
andFamilyName
. And ask user to fill in her name to these fields.To extend the database with the extra field(s) your migration can be:
In your code when you Register a new user using ASP.NET Identity classes you will have a code line like:
Basically, the user's
UserName
will always be the same as Email and anotherName
property will be used for displaying purposes.You can set user validation rules configuring identity with options in your Startup.cs.
Related resources:
Configure Identity
UserName should not allow special characters or white spaces, I don't think that there is any website will allow you to do that, you can use FirstName and Last name to get an info like "Joe Smith".
The proposed solution can be tricky if you have an exhaustive list of special characters to whitelist.
If you want to blacklist instead, disable the whitelist in startup.cs:
Then create your custom user validator
Then add it to startup.cs: