添加用户数据以管理MVC5中的页面

时间:2014-12-01 16:02:11

标签: asp.net asp.net-mvc asp.net-mvc-5

我一直在关注本教程的“添加配置文件数据到用户类”部分,以便在MVC 5的注册页面中添加更多字段。到目前为止,它工作正常,我没有任何问题。问题是现在我不确定如何在管理页面上显示,用户可以看到他的个人资料信息,如更改密码。例如,我想在该页面上添加名字和姓氏。

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on#ap

以下是我正在谈论的页面的屏幕截图: http://puu.sh/dcMmj/82ddd8fc97.PNG

我的项目是Visual Studio为您创建的项目,在注册页面中添加了名字和姓氏。在教程

中的以下身份模型中添加了此内容
public class ApplicationUser : IdentityUser
{
    public string FirstName{ get; set; }
    public string LastName { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

我认为我需要在ManageController.cs中添加一些东西,但我不确定。

// GET: /Manage/Index
    public async Task<ActionResult> Index(ManageMessageId? message)
    {
        ViewBag.StatusMessage =
            message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
            : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
            : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
            : message == ManageMessageId.Error ? "An error has occurred."
            : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
            : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
            : "";

        var model = new IndexViewModel
        {
            HasPassword = HasPassword(),
            PhoneNumber = await UserManager.GetPhoneNumberAsync(User.Identity.GetUserId()),
            TwoFactor = await UserManager.GetTwoFactorEnabledAsync(User.Identity.GetUserId()),
            Logins = await UserManager.GetLoginsAsync(User.Identity.GetUserId()),
            BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(User.Identity.GetUserId()),

           // I think it goes in here somewhere


        };
        return View(model);
    }

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码替换 var model

ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

var model = new IndexViewModel {
    HasPassword = HasPassword(),
    PhoneNumber = user.PhoneNumber,
    TwoFactor = user.TwoFactorEnabled,
    Logins = await UserManager.GetLoginsAsync(user),
    BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(user),
    FirstName = user.FirstName        
}

现在,您可以在视图中使用model.FirstName。