【问题标题】:Show custom profile information on manage page MVC5在管理页面 MVC5 上显示自定义配置文件信息
【发布时间】:2015-05-11 04:53:31
【问题描述】:

我尝试通过本教程将自定义数据添加到 MVC5 身份验证。 http://blog.falafel.com/customize-mvc-5-application-users-using-asp-net-identity-2-0/ 这工作正常。 现在我想显示我添加到管理个人资料页面的自定义字段。 在这里您现在只能更改密码。 这是我尝试获取用户信息的控制器:

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 userId = User.Identity.GetUserId();
        //Here starts my error
        var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
        var currentUser = manager.FindById(User.Identity.GetUserId());

        var model = new IndexViewModel
        {
            HasPassword = HasPassword(),
            Logins = await UserManager.GetLoginsAsync(userId),
            BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(userId),

        };
        return View(model);
    }

通过 var manager = new UserManager...... 我得到错误 找不到命名空间“UserStore”的类型。 但这是 MVC 用于身份验证的标准命名空间。

我在这里错过了什么吗? 在此先感谢

【问题讨论】:

    标签: c# authentication asp.net-mvc-5


    【解决方案1】:

    如果我引入以下命名空间,该代码对我有用:

    using Microsoft.AspNet.Identity.EntityFramework;
    

    你能看看这是否能解决你的问题吗?

    【讨论】:

    • 是的,工作!但现在该函数总是返回 null。没有用户 ID。我发现很多人都遇到了这个问题。但不知道为什么。
    • User.Identity.GetUserId() 是否返回 userId?他们登录了吗?
    猜你喜欢
    • 2013-11-05
    • 2023-03-30
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    相关资源
    最近更新 更多