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

John Swaringen

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!

akhileshchetu kumar

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET Core Identity 2: User.IsInRole always returns false

ModelState IsValid returns False Using ASP.NET Core 5 MVC Identity with UserName

User.Identity.Name always null and claims count 0 after PasswordSignInAsync MVC .Net Core 3.0

Asp.Net Core PasswordSignInAsync always fails

User.Identity.IsAuthenticated always false after PasswordSignInAsync gives success

ASP.NET MVC checkbox always false

PasswordSignInAsync returns always failed

ASP.NET MVC 5 ModelState.IsValid is always returning false in MVC

ModelState.IsValid always returns false in ASP.NET Core 6.0 MVC

checkbox always bind to false even if it is checked asp.net mvc 5

ASP.NET 5, MVC6, WebAPI -> ModelState.IsValid always returns true

ASP.NET Core 2 MVC CheckBoxFor always returning false in model

ASP.NET CheckBox returns always false value

ASP.NET MVC 5 Identity 2 Login redirect based on user role

CheckBox always returns null in ASP.NET MVC

Why is the login attempt returning {NotAllowed} in the PasswordSignInAsync method (ASP.NET)

MVC 5 Checkbox returns “False,false” or “false”

override Asp.net Identity MVC5

SignalR ASP.NET MVC5 Identity

Asp.Net MVC 5 Identity Create Database Admin Interface

Login popup with Identity framework in ASP.NET MVC 5

null reference in ASP.NET MVC 5 identity

Creating Roles in Asp.net Identity MVC 5

Customize the cookie value in MVC5 ASP.NET Identity

ASP.NET MVC 5 Identity with Model First

ASP.NET MVC 5, Identity, Unity container solution architecture

How Does Identity Work in Asp .Net MVC 5

ASP.NET MVC5 Identity Implementation

ASP.NET MVC5 Identity Customizing