I am working with a system that should allow users to register by a unique username, or unique email. Register by both is possible as well.
I am using the default identity pages with some modifications, here is a sample of the register code:
public class InputModel
{
//[Required] //Validated in server side based on role
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
//[Required] //Validated in server side based on role
[Display(Name = "Username")]
public string UserName { get; set; }
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
}
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
string username = Input.UserName ?? Input.Email;
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = username,
Email = Input.Email,
Name = Input.Name,
};
}
}
Basically, if the user entered a username, Username column will be Input.UserName. if no username (only email), the Username = Input.Email, because obviously it cannot be empty. Now both username and email are equal as the default.
examples:
Username:a@a, Email:a@a,
>> no username
Username:xyz, Email:null,
>> no email
Username:abc, Email:a@b,
>> user entered both username and email
For now, username is always unique and always required (required by identity not the user), but not the case for the email, it can be null as expected but not unique, I added this line in the startup.cs for the uniqueness:
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
5 Points
12 Posts
Register with unique email OR unique username in ASP.NET Core MVC
Nov 28, 2020 08:44 PM|rosaud|LINK
I am working with a system that should allow users to register by a unique username, or unique email. Register by both is possible as well.
I am using the default identity pages with some modifications, here is a sample of the register code:
Basically, if the user entered a username, Username column will be Input.UserName. if no username (only email), the Username = Input.Email, because obviously it cannot be empty. Now both username and email are equal as the default.
examples:
Username: a@a , Email: a@a , >> no username
Username: xyz , Email: null , >> no email
Username: abc , Email: a@b , >> user entered both username and email
For now, username is always unique and always required (required by identity not the user), but not the case for the email, it can be null as expected but not unique, I added this line in the startup.cs for the uniqueness:
but now it cannot be null, it give this validation error:
All-Star
57864 Points
15495 Posts
Re: Register with unique email OR unique username in ASP.NET Core MVC
Nov 28, 2020 08:48 PM|bruce (sqlwork.com)|LINK
It’s a database limitation. If the key is unique, there can only be one null row. You will need add your own validation.
Member
5 Points
12 Posts
Re: Register with unique email OR unique username in ASP.NET Core MVC
Nov 28, 2020 09:08 PM|rosaud|LINK
can you be more specific please
Contributor
2680 Points
874 Posts
Re: Register with unique email OR unique username in ASP.NET Core MVC
Nov 30, 2020 06:40 AM|Rena Ni|LINK
Hi rosaud,
You could custom validation like below:
Startup.cs:
Best Regards,
Rena