ASP.NET ChangePassword userId = null

时间:2016-05-10 00:52:22

标签: c# asp.net controls change-password

所以,我试图使用默认的ASP.NET方法更改用户密码,但是不能正常工作,不知怎的,我无法获取当前的用户数据。我尝试了不同的方法来获取userId,但仍然没有。除了[AllowAnonymous]之外,我没有改变任何东西,因为当我试图调用该方法时,有一个错误说许可被拒绝。

enter image description here

// POST api/Account/ChangePassword
    [AllowAnonymous]
    [Route("ChangePassword")]
    public async Task<IHttpActionResult> ChangePassword(ChangePasswordBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword,
            model.NewPassword);

        if (!result.Succeeded)
        {
            return GetErrorResult(result);
        }

        return Ok();
    }

2 个答案:

答案 0 :(得分:0)

GetUserId()是IIdentity的扩展方法,它位于Microsoft.AspNet.Identity.IdentityExtensions中。确保使用Microsoft.AspNet.Identity;。

添加了命名空间

或者您可以使用

string currentUserId = User.Identity.GetUserId();
ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);

如果您使用EF然后使用User ApplicationUser代码

答案 1 :(得分:0)

所以,我找到了答案......我忘了把它放在请求上:

authorization: bearer my_token