【问题标题】:Adding user data to manage page in MVC5在 MVC5 中添加用户数据以管理页面
【发布时间】:2014-12-01 16:02:11
【问题描述】:

我一直在关注本教程的将配置文件数据添加到用户类部分,以便在 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);
    }

【问题讨论】:

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


    【解决方案1】:

    您可以用以下代码替换您的 var 模型

    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。

    【讨论】:

    • 我的项目是 Visual Studio 为您创建的项目,并在注册页面中添加了名字和姓氏。从教程向我展示的内容中添加了身份模型中的额外内容。
    • 不错!感谢您花时间帮助我。它现在可以工作了,但是在我将 await 添加到该行之前,它给了我一个错误。如果有人遇到类似问题,我会在此处发布更改。 ApplicationUser user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
    • 我试着让它工作。但我收到错误 Models.indexViewModel 不包含“名称”的定义。
    • 检查你的 indexViewModel 类,在我看来你忘记实现 'name' 属性或者它被设置为非公开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多