Base solution for your next web application
Open Closed

Login with only username and Password #2265


User avatar
0
shyamjith created

Hi,

I am using " _userManager.LoginAsync(usernameOrEmailAddress, password, tenancyName);" Method to login the application, which will accept both username and email address. but my requirement is to authenticate the user only with Username and password not with Email and password, How do I do that?


2 Answer(s)
  • User Avatar
    0
    gpcaretti created

    override the LoginAsync method and throw an exception if you do not find the user via username. If you find it, call the base LoginAsync method.

    I did not test it but it'd be something like this:

    [UnitOfWork]
            public override async Task<AbpLoginResult<Tenant, User>> LoginAsync(string userNameOrEmailAddress, string plainPassword, string tenancyName = null, bool shouldLockout = true) {
                // this method search for the user by checking just the username field, not the emailaddress fiel.
                // In this way, if the user passes the email address the method fails throwing an ex.
                var user = await this.UserManager.FindByNameAsync(userNameOrEmailAddress);
                // if you are here, the username is OK
                return await base.LoginAsync(userNameOrEmailAddress, plainPassword, tenancyName, shouldLockout);
            }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    As an alternative, you can do what I suggested here #2264.