0

I want to implement CustomUserValidator class instead of UserValidator<User>, so that I can make userName not unique , as identity refuse Duplicated usernames, when I override ValidateAsync Function and called the below function,

public class CustomUserValidator :UserValidator<User>
{
    public CustomUserValidator(): base(new IdentityErrorDescriber())
    {

    }

    public override async
    Task<IdentityResult> ValidateAsync(UserManager<User> manager, User user) {
        if (manager == null) {
            throw new ArgumentNullException(nameof(manager));
        }
        if (user == null) {
            throw new ArgumentNullException(nameof(user));
        }
        var errors = new List<IdentityError>();
        await ValidateUserName (manager, user, errors);
        if (manager.Options.User.RequireUniqueEmail) {
            await ValidateEmail (manager, user, errors);
        }
        return errors.Count > 0 ? IdentityResult.Failed(errors.ToArray()) : IdentityResult.Success;
    }


    private async Task
    ValidateUserName(UserManager<User> manager, User user, ICollection<IdentityError> errors) {
        var userName = await manager.GetUserNameAsync(user);
        if (string.IsNullOrWhiteSpace(userName)) {
            errors.Add(Describer.InvalidUserName(userName));
        } else if (!string.IsNullOrEmpty(manager.Options.User.AllowedUserNameCharacters) &&
                userName.Any(c => !manager.Options.User.AllowedUserNameCharacters.Contains(c)))
        {
            errors.Add(Describer.InvalidUserName(userName));
        }
        else
        {
            var owner = await manager.FindByNameAsync(userName);
            if (owner != null &&
                    !string.Equals(await manager.GetUserIdAsync(owner), await manager.GetUserIdAsync(user))) {
                errors.Add(Describer.DuplicateUserName(userName));
            }
        }
    }
}

by commenting

if (owner != null && !string.Equals(await manager.GetUserIdAsync(owner), await manager.GetUserIdAsync(user))) {
    errors.Add(Describer.DuplicateUserName(userName));
}

also I get DuplicatedUsername error, and if I replace DuplicateUserName with anything else it gets me this error and also the duplicatedUserName error

Is there any easy way to handle that?

3
  • How DuplicateUserName method is implemented? Commented Aug 29, 2018 at 4:02
  • it is derived from Describer class which produce the error, it returns just string with the error, the main point that when i override ValidateAsync(), it returns the base return with the changed one both, so i can't validate on it, even if i validate it will return error from base class UserValidator() Commented Aug 29, 2018 at 5:08
  • Possible duplicate of Identity - Custom User Validators Commented Aug 29, 2018 at 6:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.