0

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);
ekad
  • 14,436
  • 26
  • 44
  • 46
dmikester1
  • 1,374
  • 11
  • 55
  • 113
  • Are you sure you are looking at the correct database? – DavidG Jun 09 '15 at 13:23
  • That's a good question. I am assuming that function is using the correct DB, but how do I verify? – dmikester1 Jun 09 '15 at 13:26
  • How do you create `UserManager`? What is the connection string called? And what is the constructor of the context? – DavidG Jun 09 '15 at 13:27
  • I have three of my own contexts but it looks like it is calling GetOwinContext(). I'm not sure what that does. – dmikester1 Jun 09 '15 at 13:34
  • I think I provided everything you asked for. Let me know if you need more. – dmikester1 Jun 09 '15 at 13:41
  • Somewhere in your code you have a line telling `OwinContext` how to create the user manager object, something like this: `app.CreatePerOwinContext(ApplicationUserManager.Create);`? – DavidG Jun 09 '15 at 13:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80064/discussion-between-dmikester1-and-davidg). – dmikester1 Jun 09 '15 at 13:47

2 Answers2

0

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");
    }
    
DavidG
  • 113,891
  • 12
  • 217
  • 223
0

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