Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 5 Identity 2 PasswordSignInAsync method always returns false

I'm writing come code that will be for an external app. I'm trying to use Microsoft Identity 2.0 and ASP.NET MVC 5. I've customized the UserViewModel to hold FirstName and LastName And a couple of other parameters. Whenever I register a user it logs in successfully but when I try to do the normal login with email/password. the SignInManager.PasswordSignAsync always returns false. Since all the code that checks that is internal to the framework I'm trying to figure out how to even debug the situation. I will provide some code here to help anybody who's willing to look at it. Thanks in Advance!

That is the generic login function that is provided by Microsoft in their examples. I've not changed anything there. But that is the function that has the error message.

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

This is the login function

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

This is the customized Identity Model

    public class ApplicationUser : IdentityUser
{
    [DisplayName("Prefix")]
    public string Prefix { get; set; }

    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [DisplayName("Last Name")]
    public string LastName { get; set; }

    [DisplayName("Suffix")]
    public string Suffix { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<JBIPlacementTracking.Models.PlacementViewModel> PlacementViewModels { get; set; }

    public System.Data.Entity.DbSet<JBIPlacementTracking.Models.Placement> Placements { get; set; }

    public System.Data.Entity.DbSet<JBIPlacementTracking.Models.PacketStatus> PacketStatus { get; set; }
}

I'm wondering if the problem might be that I don't have a custom LoginViewModel.

I'm definitely in over my head right now and if somebody has some good resources I'd really appreciate a link to it.

Thanks in Advance Again!

like image 549
John Swaringen Avatar asked Mar 13 '23 10:03

John Swaringen


2 Answers

You are using Email as UserName at the login function. Make sure that upon registration, the email is inserted to both the UserName and Email Fields. I Also like to use use Trim() and ToLower() before inserting and after reading, but that's only to rest my mind about what is stored in my DB, and might not be necessary. Good luck.

like image 117
Issac Avatar answered Mar 16 '23 11:03

Issac


You are using Email as UserName then you need to set EmailConfirmed field 'true' in AspNetUsers table.

like image 37
akhileshchetu kumar Avatar answered Mar 16 '23 13:03

akhileshchetu kumar