5

I try to create new project with MVC 5 and OWIN by "Individial User Accounts". The problem is when I register a new account, the system requires me input an email then that email will be populated to both Email and Username in database. When I try to login, the system asks me to input the email but it compares that email with column Username. So the values of Email and Username are the same. The login code below is in action Login (Account Controller)

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

Is there any ways to compare the inputted email with the Email column instead of Username?

2
  • 2
    you need to give examples of your code Commented Nov 12, 2015 at 15:59
  • Thanks, I've given the login code, we can see: model.Email -> the problem is the system will compare this email with column Username in database. Is there any way to compare this value with Email column? Commented Nov 12, 2015 at 16:06

3 Answers 3

4

To answer:

Is there any ways to compare the inputted email with the Email column instead of Username?

This is probably not the best answer but the SignInManager should have access to UserManager which has a Users property. So you could lookup the user based on the email and then using the result call the signin method.

Something like this (untested):

var user = SignInManager.UserManage.Users.Where(u => u.email == model.Email).FirstOrDefault();
var result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout: false);

You would want to check that the user was populated otherwise return invalid login details error. Same as if PasswordSignInAsync failed.

Sign up to request clarification or add additional context in comments.

1 Comment

Very simple. In the scope of my question. Your answer should be accepted. Thanks!
2

Here is what to do as @ChrisMoutray suggested, find the username for the email provided and log in. I put it here.

Instantiate usermanager and signinmanager

private readonly SignInManager<ApplicationUser> signInManager;
private readonly UserManager<ApplicationUser> userManager;

        public AccountController(SignInManager<ApplicationUser> signInManager, UserManager<ApplicationUser> userManager)
        {
            this.signInManager = signInManager;
            this.userManager = userManager;
        }

and then in the Login (post) task:

  var user = await userManager.FindByEmailAsync(model.Email);
  var result = await signInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, lockoutOnFailure: false);

PS. Found myself in this old post so just adding a working solution.

Comments

0

The reason is because when you register. you get username = email.

  public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);

3 Comments

Sure, we can change UserName = model.UserName, then when login, I can disable email validation to input the username. But I want to input the email because the email is unique. I just want the system to compare inputted email to Email column.
@AnhHoang If we change UserName = "John", MVC will recognize model.Email as your login email to login. As you can see from the login method, it is using model.Email not model.UserName to login, so I'm not sure what you mean by comparing the inputted email to the Email column when it already is.
@JuliusDoan if you dig into the underlying code of the ASP.Net Identity the SignInManager.PasswordSignInAsync will compare any value sent to it to the "username" column in the database. If you change the register code to set a non email address as the username it will fail logins because it is hard coded to look at the username field of the database which makes it difficult to login with the email address if you try to seperate the username and email address. I had to implement my own Identity Provider to get around this same issue in a site I'm developing myself

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.