UserManager.IsEmailConfirmedAsync(user.Id) is returning false

3.6k views Asked by At

I am working on the ForgotPassword section of my site. When I test it I have a breakpoint in the function and I can see that this line of code is returning false:

(await UserManager.IsEmailConfirmedAsync(user.Id)))

I have verified that EmailConfirmed field in the AspNetUsers table is set to True. Why would this still be returning false?

Here is the first part of the Account Controller where it initializes the UserManager:

[Authorize]
public class AccountController : Controller
{
    private ApplicationSignInManager _signInManager;
    private ApplicationUserManager _userManager;

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager )
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set 
        { 
            _signInManager = value; 
        }
    }

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

Found the requested Owin string in my Startup.Auth.cs class:

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context, user manager and signin manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
2

There are 2 answers

0
DavidG On

You are likely pointing to the wrong database. The default templates for an MVC project with Identity will have code like this in the context:

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

This is then used by OwinContext as the method to create your context, and as such means that it will take the connection string from your web.config file called DefaultConnection. So you have 2 choices:

  1. Fix the connection string to point to the correct database.

    <add name="DefaultConnection" 
         connectionString="correct details here" 
         providerName="System.Data.SqlClient" />
    
  2. Change the create method to return the context with a specific connection string:

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext("NameOfConnectionStringHere");
    }
    
1
TenaciousKey On

It seems await UserManager.IsEmailConfirmedAsync(user.Id)) always returns false when EmailConfirmed is set to False in AspNetUsers SQL table.