c# – 如何从ASP.NET MVC自定义IdentityUser模型获取用户配置文件数据?

前端之家收集整理的这篇文章主要介绍了c# – 如何从ASP.NET MVC自定义IdentityUser模型获取用户配置文件数据?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
this blog的帮助下,我向ApplicationUser类添加了一个自定义配置文件属性OfficeKey(在IdentityModels.cs文件中):
public class ApplicationUser : IdentityUser
{
      public int OfficeKey { get; set; }

      //remaining code here
}

我可以得到这样的用户名

User.Identity.Name

How do I get custom user data OfficeKey?

我试过这些:

How to get Identity User Data from Asp.net mvc Model

How to get user email address from ASP.NET MVC Default Mempership Model?

注意:它是一个带有MysqL成员资格提供程序的ASP.NET MVC 5 Web应用程序.

解决方法

如果使用ApplicationUser配置UserManager,它将返回具有以下信息的用户
// Create manager
 var manager = new UserManager<ApplicationUser>(
    new UserStore<ApplicationUser>(
        new ApplicationDbContext()))

// Find user
var user = manager.FindById(User.Identity.GetUserId());

OfficeKey将在用户中可用.

原文链接:https://www.f2er.com/csharp/92466.html

猜你在找的C#相关文章