【问题标题】:How to change the value of custom fields (\Areas\Identity\Pages\Account\Manage\Index.cshtml.c)?如何更改自定义字段的值(\Areas\Identity\Pages\Account\Manage\Index.cshtml.c)?
【发布时间】:2020-02-03 04:40:48
【问题描述】:

(环境:Visual Studio 2019 v16.4.3)

我使用以下选项创建一个新的“ASP.NET Core Web 应用程序”

  1. ASP.Net Core 3.1
  2. 角度
  3. 个人用户帐户的身份验证(使用“在应用程序中存储用户帐户”,唯一选项)

然后我做了以下添加所有脚手架的项目。

  • 在解决方案资源管理器中,右键单击项目 > 添加 > 新建脚手架项目
  • ApplicationUser 类中添加了新字段FirstNameLastName,并更新了代码以启用新字段的注册。注册后可以将名字和姓氏保存到数据库中。

现在我需要修改代码以使用户能够修改自定义字段FirstNameLastName。下面显示了类(我对FirstNameLastName 做了一些更改)。在方法OnPostAsync() 中,它调用_userManager.SetPhoneNumberAsync(user, Input.PhoneNumber) 来更改值。 _userManager.SetFistNameAsync(user, Input.PhoneNumber)_userManager.SetLastNameAsync(user, Input.PhoneNumber)如何定义?

\Areas\Identity\Pages\Account\Manage\Index.cshtml.cs

public partial class IndexModel : PageModel
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;

    public IndexModel(
        UserManager<ApplicationUser> userManager,
        SignInManager<ApplicationUser> signInManager)
    {
        _userManager = userManager;
        _signInManager = signInManager;
    }

    public string Username { get; set; }

    [TempData]
    public string StatusMessage { get; set; }

    [BindProperty]
    public InputModel Input { get; set; }

    public class InputModel
    {
        [Phone]
        [Display(Name = "Phone number")]
        public string PhoneNumber { get; set; }
        [Display(Name = "First name")]
        public string FirstName { get; set; }
        [Display(Name = "Last name")]
        public string LastName { get; set; }
    }

    private async Task LoadAsync(ApplicationUser user)
    {
        var userName = await _userManager.GetUserNameAsync(user);
        var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

        Username = userName;

        Input = new InputModel
        {
            PhoneNumber = phoneNumber,
            FirstName = user.FirstName,
            LastName = user.LastName
        };
    }

    public async Task<IActionResult> OnGetAsync()
    {
        var user = await _userManager.GetUserAsync(User);
        if (user == null)
        {
            return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
        }

        await LoadAsync(user);
        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        var user = await _userManager.GetUserAsync(User);
        if (user == null)
        {
            return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
        }

        if (!ModelState.IsValid)
        {
            await LoadAsync(user);
            return Page();
        }

        var phoneNumber = await _userManager.GetPhoneNumberAsync(user);
        if (Input.PhoneNumber != phoneNumber)
        {
            var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);
            if (!setPhoneResult.Succeeded)
            {
                var userId = await _userManager.GetUserIdAsync(user);
                throw new InvalidOperationException($"Unexpected error occurred setting phone number for user with ID '{userId}'.");
            }
        }

        await _signInManager.RefreshSignInAsync(user);
        StatusMessage = "Your profile has been updated";
        return RedirectToPage();
    }
}

【问题讨论】:

    标签: c# asp.net asp.net-core


    【解决方案1】:

    我已经有一段时间没有使用 ASP.NET Core Identity 的东西了,但我相信你会从这里更新你的自定义用户对象(GetUserAsync 应该返回你的自定义 ApplicationUser 类):

    var user = await _userManager.GetUserAsync(User);
    user.FirstName = Input.FirstName;
    user.LastName = Input.LastName;
    
    

    然后调用_userManager.UpdateAsync(user):

    await _userManager.UpdateAsync(user);
    

    【讨论】:

    • 奇怪的是,为什么它有一个预定义的SetPhoneNumberAsync() 用于电话号码?
    • 我相信由于身份系统带有双重身份验证,电话是一种方法,它是内置的。
    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多