locked
Beginner's question: Add User Name to Register RRS feed

  • Question

  • User-314484952 posted

    Hi,

    I created a simple project with MVC 5 (VS 2017), using Individual User Account. I need only tiny modification: add User Name to Register so that when my user logged in, the greeting will be "Hello, UserName!" rather than "Hello, Email!". I added UserName to RegisterViewModel, modified AccountController - Register (UserName = model.UserName,). added User Name textbox to Register view. That's all I did. As the result, I am able to register, but not able to login. In the AccountController - Login,

    SignInManager.PasswordSignInAsync
    

    goes directly to "invlide login attempt."  Login failure. Why? There is nothing to do with login. I did not touch that part.

    Please help!

    Friday, April 12, 2019 3:01 PM

Answers

  • User-314484952 posted

    Ok, I have found my solution. Post here in case someone else has the same issue. 

    AccountController - Login action, SignInManager.PasswordSignInAsync works with user name and password combination, not Email. Since my UserName != Email, it returns a Failure. I used the following solution and it worked. 

    ApplicationUser signedUser = UserManager.FindByEmail(model.Email);
    var result = await SignInManager.PasswordSignInAsync(signedUser.UserName, model.Password, model.RememberMe, shouldLockout: false);

    //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

    Thank you for looking into my problem!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 12, 2019 4:40 PM

All replies

  • User753101303 posted

    Hi,

    Seems a message. What if you start by looking at the exact value returned by PasswordSignInAsync...

    Friday, April 12, 2019 3:09 PM
  • User475983607 posted

    Hi,

    I created a simple project with MVC 5 (VS 2017), using Individual User Account. I need only tiny modification: add User Name to Register so that when my user logged in, the greeting will be "Hello, UserName!" rather than "Hello, Email!". I added UserName to RegisterViewModel, modified AccountController - Register (UserName = model.UserName,). added User Name textbox to Register view. That's all I did. As the result, I am able to register, but not able to login. In the AccountController - Login,

    SignInManager.PasswordSignInAsync
    

    goes directly to "invlide login attempt."  Login failure. Why? There is nothing to do with login. I did not touch that part.

    Please help!

    I'm guessing you forgot to update the login View.  The Registration View and Model are not the only updates you need to make to update the default template.  Use the "Find" feature in Visual Studio to find all the places where "Email" is used.

    Friday, April 12, 2019 3:27 PM
  • User-314484952 posted

    Ok, I have found my solution. Post here in case someone else has the same issue. 

    AccountController - Login action, SignInManager.PasswordSignInAsync works with user name and password combination, not Email. Since my UserName != Email, it returns a Failure. I used the following solution and it worked. 

    ApplicationUser signedUser = UserManager.FindByEmail(model.Email);
    var result = await SignInManager.PasswordSignInAsync(signedUser.UserName, model.Password, model.RememberMe, shouldLockout: false);

    //var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

    Thank you for looking into my problem!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 12, 2019 4:40 PM