簡體   English   中英

擴展UserManager以允許管理新用戶數據?

[英]Extending UserManager to allow for management of new user data?

ASPNET Identity中的UserManager包含AddPasswordAsyncSetPhoneNumberAsync類的功能。

我已經添加了FirstName和LastName的屬性,這些屬性在我注冊用戶時未設置值,因此我想添加屏幕來管理這些值(設置和更改它們)。

我如何擴展UserManager來添加類似AddNamesAsync的函數來執行此操作?

當我們按以下方式評估ManageController動作時,此功能變得必要:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> SetPassword(SetPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
        if (result.Succeeded)
        {
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
            if (user != null)
            {
                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
            }
            return RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess });
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

按照相同的邏輯,我想向控制器添加一個函數,如下所示:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> SetNames(SetUserFullNamesViewModel model)
{
    if (ModelState.IsValid)
    {
        var result = await UserManager.AddNamesAsync(User.Identity.GetUserId(), model.FirstName, model.LastName);
        if (result.Succeeded)
        {
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

            return RedirectToAction("Index", new { Message = ManageMessageId.SetNamesSuccess });
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

如何向UserManager添加函數或擴展UserManager,以便可以添加允許我在用戶上更新添加和更新自定義屬性值的函數?

首先將custom columns添加到AppliicationUser

然后您可以像這樣更新custom columns

[HttpPost]
public async Task<ActionResult> SetNames(SetUserFullNamesViewModel model)
{
    ApplicationUser usermodel = UserManager.FindById(user.Id);
    usermodel.Name = model.Name;
    usermodel.Surname = model.Surname;


    IdentityResult result = await UserManager.UpdateAsync(usermodel);
    if (result.Succeeded)
    {
        return RedirectToAction("ListUser", "User");
    }

    return View();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM