Base solution for your next web application
Open Closed

Unique username validation #4325


User avatar
0
omarsultan created

How to validate that the entered username is unique ?


3 Answer(s)
  • User Avatar
    0
    omarsultan created

    i wanna add a custom dto validation which needs to access my repos , what to do ?

  • User Avatar
    0
    aaron created
    Support Team

    Do it in your application service. This is how aspnetboilerplate does it:

    public virtual async Task<IdentityResult> CheckDuplicateUsernameOrEmailAddressAsync(long? expectedUserId, string userName, string emailAddress)
    {
        var user = (await FindByNameAsync(userName));
        if (user != null && user.Id != expectedUserId)
        {
            throw new UserFriendlyException(string.Format(L("Identity.DuplicateUserName"), userName));
        }
    
        user = (await FindByEmailAsync(emailAddress));
        if (user != null && user.Id != expectedUserId)
        {
            throw new UserFriendlyException(string.Format(L("Identity.DuplicateEmail"), emailAddress));
        }
    
        return IdentityResult.Success;
    }
    
  • User Avatar
    0
    omarsultan created

    thank u :)