如何解决Asp.Net自定义身份错误?

huangapple go评论55阅读模式
英文:

How can I solve an Asp.Net Custom Identity Error?

问题

我在ASP.NET Core项目中使用了CustomIdentityUser库,我想创建一个用户更新页面。但尽管没有代码错误,当用户想要更改帐户数据时,系统会报错,错误信息是"密码不正确"。

但我使用的密码与注册用户时的密码相同。这个错误的原因可能是什么?你能给我一些想法吗?提前谢谢。

我的控制器是:

public ActionResult UpdateUser(ChangeUserViewModel changeUserViewModel)
{
    if (ModelState.IsValid)
    {
        CustomIdentityUser member = new CustomIdentityUser
        {
            UserName = changeUserViewModel.UserName,
            Email = changeUserViewModel.Email
        };

        var checkPassword = _userManager.CheckPasswordAsync(member, changeUserViewModel.NewPassword).Result;
        IdentityResult changePasswordResult = _userManager.ChangePasswordAsync(member, changeUserViewModel.CurrentPassword, changeUserViewModel.NewPassword).Result;
        if (changePasswordResult.Succeeded)
        {
            _signInManager.RefreshSignInAsync(member);
            return RedirectToAction("Login", "Account");
        }
        else
        {
            foreach (var error in changePasswordResult.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
        }
    }
    return View(changeUserViewModel);
}

如果需要进一步帮助,请提供更多关于问题的信息。

英文:

I used CustomIdentityUser library in ASP.NET Core project, I wanted to create a user update page. But even though there is no code error, when the user wants to change account data, the system gives error like "Incorrect password".

But I used the same password as when registering the user. What can be the cause of this error? Can you give me an idea? Thank you in advance.

My controller is

public ActionResult UpdateUser(ChangeUserViewModel changeUserViewModel)
{
     if (ModelState.IsValid)
     {
         
            CustomIdentityUser member = new CustomIdentityUser
            {
                UserName = changeUserViewModel.UserName,
                Email = changeUserViewModel.Email
            };

             var checkPassword = _userManager.CheckPasswordAsync(member, changeUserViewModel.NewPassword).Result;
             IdentityResult changePasswordResult = _userManager.ChangePasswordAsync(member, changeUserViewModel.CurrentPassword, changeUserViewModel.NewPassword).Result;
             if (changePasswordResult.Succeeded)
             {
                _signInManager.RefreshSignInAsync(member);
                return RedirectToAction("Login", "Account");
            }
            else
            {
                foreach (var error in changePasswordResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                } 
            }
            
        }
        return View(changeUserViewModel);
 }

答案1

得分: 1

> But I used the same password as when registering the user. What can be the cause of this error?

使用与注册用户时相同的密码,为什么会出现这个错误?

In this scenario, you should better use CheckPasswordAsync before calling ChangePasswordAsync method bcause then you would understand if the password is correct.

在这种情况下,在调用ChangePasswordAsync方法之前,最好使用CheckPasswordAsync,因为这样您就能够了解密码是否正确。

> I wanted to create a user update page. But even though there is no code error, when the user wants to change account data, the system gives error like "Incorrect password".

我想创建一个用户更新页面。但是,尽管没有代码错误,但当用户想要更改帐户数据时,系统会显示"密码不正确"等错误。

Actually, based of your shared code snippet and descriptions at the birds-eye view, few inconsistencies appeared which might causing the error.

实际上,根据您分享的代码片段和总体描述,出现了一些可能导致错误的不一致之处。

The first and most importantly, if you would like to update Identity user information then why you are creating a new object of CustomIdentityUser rather you should update the information by finding the existing user.

首先,最重要的是,如果您想要更新Identity用户信息,为什么要创建一个新的CustomIdentityUser对象,而不是通过查找现有用户来更新信息。

Thus, instead of creating a new user instance, you should find the existing user by your user information you would like to update. So, you can use your current context to pull that user information. You can do:

因此,不要创建新的用户实例,而应该根据要更新的用户信息查找现有用户。这样,您可以使用当前上下文来获取用户信息。您可以这样做:

var user = await _userManager.GetUserAsync(User);

if (user == null)
{
    return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}

var changePasswordResult = await _userManager.ChangePasswordAsync(user, Input.OldPassword, Input.NewPassword);
if (!changePasswordResult.Succeeded)
{
    foreach (var error in changePasswordResult.Errors)
    {
        ModelState.AddModelError(string.Empty, error.Description);
    }
    return Page();
}

await _signInManager.RefreshSignInAsync(user);

Note: It requires users from user claim principal. You can get more details here in the official sample and document.

注意: 它需要从user claim principal获取用户。您可以在官方示例文档中获取更多详细信息。

英文:

> But I used the same password as when registering the user. What can be the cause of this error?

In this scenario, you should better use CheckPasswordAsync before calling ChangePasswordAsync method bcause then you would understand if the password is correct.

>
> I wanted to create a user update page. But even though there is no
> code error, when the user wants to change account data, the system
> gives error like "Incorrect password".

Actually, based of your shared code snippet and descriptions at the birds eye view few inconsistencies appeared which might causing the error.

The first and most importantly, if you would like to update Identity user information then why you are creating new object of CustomIdentityUser rather you should update the information by finding existing user.

Thus, instead of creating new user instance you should find the existing user by your user information you would like to update. So, you can use your current context to pull that user information. You can do:
<!-- language: c# -->
var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return NotFound($&quot;Unable to load user with ID &#39;{_userManager.GetUserId(User)}&#39;.&quot;);
            }

            var changePasswordResult = await _userManager.ChangePasswordAsync(user, Input.OldPassword, Input.NewPassword);
            if (!changePasswordResult.Succeeded)
            {
                foreach (var error in changePasswordResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                return Page();
            }

            await _signInManager.RefreshSignInAsync(user);

Note: It requires users from user claim principal. You can get more details here in official sample and document

huangapple
  • 本文由 发表于 2023年6月12日 22:00:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定