如何在我的 ASP.NET 核心应用程序中显示和更新我的自定义身份字段?

How do I display and update my custom Identity fields in my ASP.NET Core application?

我已经使用 core 3.1 创建了一个 ASP.NET 核心应用程序,并且我在其中包含了开箱即用的身份验证。我想为我的用户添加一些自定义字段来完成,例如 LocationInstagram。在 instructions here 之后,我创建了一个继承 IdentityUserApplicationUser.cs 模型,并成功迁移了该模型以将字段添加到我的 AspNetUsers table。目前一切正常。

我想将这些字段添加到我的用户帐户页面。我意识到我看不到用户帐户页面。使用 following instructions 我将缺少的帐户页面添加到我的项目中。但是,当我添加缺少的字段时,它声称无法找到它们,尽管整个项目标识都按照文档中的说明使用 ApplicationUser

Areas/Pages/Account/Manage/Index.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Profile";
    ViewData["ActivePage"] = ManageNavPages.Index;
}

<h4>@ViewData["Title"]</h4>
<partial name="_StatusMessage" model="Model.StatusMessage" />
<div class="row">
    HEY
    <div class="col-md-6">
        <form id="profile-form" method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Username"></label>
                <input asp-for="Username" class="form-control" disabled />
            </div>
            <div class="form-group">
                <label asp-for="Location"></label>
                <input asp-for="Location" class="form-control" />

            </div>
            <div class="form-group">
                <label asp-for="Instagram"></label>
                <input asp-for="Instagram" class="form-control" />

            </div>
            <div class="form-group">
                <label asp-for="Input.PhoneNumber"></label>
                <input asp-for="Input.PhoneNumber" class="form-control" />
                <span asp-validation-for="Input.PhoneNumber" class="text-danger"></span>
            </div>
            <button id="update-profile-button" type="submit" class="btn btn-primary">Save</button>
        </form>
    </div>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

位置和 Instagram return 在模型中找不到它们的错误。我是不是错过了一个步骤,或者我还需要做些什么来添加这些字段?

您应该在 IndexModel 中添加 LocationInstagram 的属性。

更新 Areas/Identity/Pages/Account/Manage/Index.cshtml.cs 中的 InputModel :

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

    public string Instagram { get; set; }

    public string Location { get; set; }

}

为 loading/showing 属性更新 LoadAsync 方法:

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,

        Instagram=user.Instagram,

        Location = user.Location
    };
}

更新 OnPostAsync 方法以更新 LocationInstagram 属性:

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}'.");
        }
    }
    if (Input.Instagram != user.Instagram)
    {
        user.Instagram = Input.Instagram;
    }

    if (Input.Location != user.Location)
    {
        user.Location = Input.Location;
    }

    await _userManager.UpdateAsync(user);


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

最后,修改 Index.cshtml 以包含两个属性:

<div class="form-group">
    <label asp-for="Input.Location"></label>
    <input asp-for="Input.Location" class="form-control" />

</div>
<div class="form-group">
    <label asp-for="Input.Instagram"></label>
    <input asp-for="Input.Instagram" class="form-control" />

</div>

您也可以点击here了解更多详情。