Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# dotnet 3.1:ChangePasswordAsync提供的密码不匹配,而CheckPasswordAsync接受相同的密码_C#_Asp.net Core_Asp.net Identity - Fatal编程技术网

C# dotnet 3.1:ChangePasswordAsync提供的密码不匹配,而CheckPasswordAsync接受相同的密码

C# dotnet 3.1:ChangePasswordAsync提供的密码不匹配,而CheckPasswordAsync接受相同的密码,c#,asp.net-core,asp.net-identity,C#,Asp.net Core,Asp.net Identity,我正在我的应用程序中的一个部分工作,在那里用户可以更新他们的密码。我在身份验证服务中为此创建了一个方法 AuthenticationService.cs [Authorize] public async Task ChangePassword(ChangePasswordViewModel model) { // Need to get it this way otherwise the ChangePasswordAsync won't accept this user var

我正在我的应用程序中的一个部分工作,在那里用户可以更新他们的密码。我在身份验证服务中为此创建了一个方法

AuthenticationService.cs

[Authorize]
public async Task ChangePassword(ChangePasswordViewModel model)
{
    // Need to get it this way otherwise the ChangePasswordAsync won't accept this user
    var user = await _userManager.FindByIdAsync(_userService.CurrentUser.Id);

    var result = await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);

    if (!result.Succeeded)
    {
        throw new Exception();
    }
}
[HttpPost]
public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
{
    try
    {
        await _authenticationService.ChangePassword(model);

        return Ok();
    }
    catch (Exception)
    {
        return BadRequest(new ErrorViewModel()
        {
            ErrorKey = "error_changing_password"
        });
    }
}
public class ChangePasswordViewModel
{
    public string CurrentPassword { get; set; }

    public string NewPassword { get; set; }
}
userService.CurrentUser
是一个getter,它使用JWT从
IdentityDbContext
获取用户。我进行了调试,结果返回了正确的用户/ID

我从我的
AuthenticationController.cs

[Authorize]
public async Task ChangePassword(ChangePasswordViewModel model)
{
    // Need to get it this way otherwise the ChangePasswordAsync won't accept this user
    var user = await _userManager.FindByIdAsync(_userService.CurrentUser.Id);

    var result = await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);

    if (!result.Succeeded)
    {
        throw new Exception();
    }
}
[HttpPost]
public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
{
    try
    {
        await _authenticationService.ChangePassword(model);

        return Ok();
    }
    catch (Exception)
    {
        return BadRequest(new ErrorViewModel()
        {
            ErrorKey = "error_changing_password"
        });
    }
}
public class ChangePasswordViewModel
{
    public string CurrentPassword { get; set; }

    public string NewPassword { get; set; }
}
但是由于某些原因,
ChangePasswordAsync
返回此错误:
密码不匹配“
。我绝对肯定这个密码是正确的。我使用它通过相同的
用户管理器登录:

await _userManager.CheckPasswordAsync(user, model.Password)

你知道为什么
ChangePasswordAsync
会给我这个反馈,而
CheckPasswordAsync
会返回一个积极的结果吗?我允许错误的密码吗?我们仅通过
UserManager
使用
CreateAsync
(创建时)或使用带有生成令牌的
ResetPasswordAsync
设置密码。

您可以将
wait\u UserManager.CheckPasswordAsync(user,model.CurrentPassword)
在您的
更改密码
方法中,在
结果
之前查看它是否为真。@Yinqiu我这样做了,发生了最奇怪的事情。当我在
ChangePasswordAsync
上方使用
CheckPasswordAsync
时,它确实可以工作。它会更改密码。当我注释掉它并再次编译时,
ChangePasswordAsync
失败。我猜
CheckPasswordAsync
修改了
用户
,但文档中没有说明这一点。您可以尝试添加此代码
wait\u-signInManager.RefreshSignInAsync(用户)更改密码后。