UserManager.CheckPasswordAsync总是返回失败 [英] UserManager.CheckPasswordAsync always returns failure

查看:92
本文介绍了UserManager.CheckPasswordAsync总是返回失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Winforms项目中使用AspNet.Identity.我已经通过传入的UserManager.FindByNameAsync用户名成功找到了用户,但是我无法使用登录的密码来登录.我可以使用相同的数据库和相同的凭据在我的MVC应用程序中正常登录.

I am using AspNet.Identity with a Winforms project. I am finding the user successfully via the passed in username to UserManager.FindByNameAsync, but I cannot get my login to work with the passed in password. I am able to log in just fine in my MVC app using the same database and same exact credentials.

这是我的代码:

public async Task<List<string>> VerifyUserNamePassword(string userName, string password)
    {
        var usermanager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new IdentityFrameworkDbContext()));

        var user = await usermanager.FindAsync(userName, password);

        //This is ALWAYS NULL!
        if (user != null) 
            label1.Text = "Success!";

        var u = await usermanager.FindByEmailAsync(userName);

        if (u != null)
        {
            //This works up to this point - check if password is correct

            var success = await usermanager.CheckPasswordAsync(u, password);

            //THIS IS ALWAYS FALSE!
            if (success)
                label1.Text = "YES THIS WORKED!";
        }            

        //This works, I get the Roles back and they are correct
        var users = await usermanager.GetRolesAsync(u.Id);

        return users.ToList();
    }

推荐答案

密码散列取决于app.config或web.config中的这些设置.

The password hashing is dependent on these settings in the app.config or web.config.

<system.web>
  <machineKey decryption="" decryptionKey="" validation="" validationKey="" />
</system.web>

如果不存在,将使用框架machine.config中的设置.如果要多个应用程序或部署使用同一数据库,则必须在这些部署中提供相同的值.

If they are not present the settings in the frameworks machine.config will be used. If you want multiple apps or deployments to use the same database then you must supply the same values across those deployments.

如果您已经拥有一个正在运行的aps.net实例,则应该将值从 web.config 复制到Windows窗体的 app.config 或从计算机中复制.在Web服务器上进行配置.

Seeing as you already have a running aps.net instance you should copy the values from the web.config to your windows form's app.config or from the machine.config on the web server.

这篇关于UserManager.CheckPasswordAsync总是返回失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆