UserManager.IsEmailConfirmedAsync(user.Id)返回false

时间:2015-06-09 13:20:27

标签: asp.net asp.net-mvc asp.net-identity

我正在处理我网站的ForgotPassword部分。当我测试它时,我在函数中有一个断点,我可以看到这行代码返回false:

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

我已验证AspNetUsers表中的EmailConfirmed字段设置为True。为什么这仍然是假的?

以下是帐户控制器初始化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;
        }
    }

在我的Startup.Auth.cs类中找到了请求的Owin字符串:

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 个答案:

答案 0 :(得分:0)

您可能指向错误的数据库。带有Identity的MVC项目的默认模板将在上下文中包含如下代码:

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

然后OwinContext将其用作创建上下文的方法,这意味着它将从web.config文件中获取名为DefaultConnection的连接字符串。所以你有两个选择:

  1. 修复连接字符串以指向正确的数据库。

    <add name="DefaultConnection" 
         connectionString="correct details here" 
         providerName="System.Data.SqlClient" />
    
  2. 更改create方法以返回具有特定连接字符串的上下文:

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

答案 1 :(得分:-1)

await UserManager.IsEmailConfirmedAsync(user.Id)) SQL表中将EmailConfirmed设置为False时,似乎AspNetUsers始终返回false。