locked
Can I ask identity to login by email or phonenumber? RRS feed

  • Question

  • User1849955458 posted

    var result = await _signInManager.PasswordSignInAsync(UserName, Password, RememberMe, lockoutOnFailure: false);

    I want to pass in the phonenumber in the username parameter

    Monday, June 29, 2020 1:10 AM

All replies

  • User2078676645 posted

    Hi,

    You can login via phonenumber or email because the PasswordSignInAsync method has two overrides.

    public virtual Task<SignInResult> PasswordSignInAsync(string userName, string password, bool isPersistent, bool lockoutOnFailure);
    public virtual Task<SignInResult> PasswordSignInAsync(TUser user, string password, bool isPersistent, bool lockoutOnFailure);

    Iterate through existing users in the UserManager with email and phonenumber. For example:

    [HttpPost]
            public async Task<IActionResult> Login(string email, string password)
            {
                //login functionality
                var user = await _userManager.FindByEmailAsync(email);
                if (user != null)
                {
                    //sign in
                    var signInResult = await _signInManager.PasswordSignInAsync(user, password, false, false);
                    if (signInResult.Succeeded)
                    {
                        return RedirectToAction("");
                    }
                }
                return RedirectToAction("");
            }

    Regards,

    Evern

    Monday, June 29, 2020 7:23 AM
  • User-1330468790 posted

    Hi laolaolulu,

    I understand that you might want to login within either email or phone number in "username" textbox and password in "password" textbox.

    However, from SignInManager level, the phone number might not be supported as it is a kind of custom user data and not under consideration of the SignInManger design. 
    The most common way to use phone number in identity is Two-factor authentication (2FA) which uses phone number to fetch short message code. You could refer to this: Two-factor authentication with SMS in ASP.NET Core to learn how to integrate SMS in your project.

    Regarding the "email" property, if you treat the email as the user name in user's registration, you could directly pass the value of "email" as the parameter "UserName" calling the below method.

    var result = await _signInManager.PasswordSignInAsync(UserName, Password, RememberMe, lockoutOnFailure: false);


    If you do have "email" and "username" at the same time, you could use an overload method of PassPasswordSignInAsync:  PassPasswordSignInAsync(TUser, String, Boolean, Boolean), which is able to take IdentityUser as parameter. Before that, you have to find the IdentityUser by using below codes.

    var currentUser= await _userManager.FindByEmailAsync(String email); 
    var result = await _signInManager.PasswordSignInAsync(currentUser, Password, RememberMe, lockoutOnFailure: false);


    Note that it is recommended that you make the Email field unique.

    If you already have set up a project within the identity, you could post the codes in this thread.
    It would be easier to help you solve the problem in a certain scenario.


    Hope this can help you.

    Best regards,
    Sean

    Monday, June 29, 2020 7:53 AM
  • User1034446946 posted

    there is no validation in identity to ensure that the phone number is unique, so you couldn't do it out of the box.

    but you could download the source code and alter it, adding phone validation and changing the lookup to check of email and phone number.

    Monday, June 29, 2020 11:56 AM