FirstName 和 LastName 以及 ASP.Net.Core.Identity 2.0 上的其他字段

FirstName and LastName and other fields on ASP.Net.Core.Identity 2.0

我是 ASP.Net.Core.Identity.2 的新手。我之前推出了自己的安全性,但使用的是 Net.Core 2.2。我无法升级我的代码,因为大部分代码都不起作用,所以我决定进行切换。我想要做的就是向 User 添加新属性,这样我就可以做类似的事情:-

User.FirstName //or
User.Identity.FirstName
//or some other syntax

我阅读了大量文章并尝试了一些示例,但我一无所获,这些示例要么不起作用,要么给我设计时错误,要么给我 运行 时间错误。

我已通过 类 修改如下。并更新了数据库。我可以看到数据库中的新字段。但是接下来我该怎么办?走到这一步很容易,但现在我完全卡住了。

    public partial class SystemUser : IdentityUser<int>
    {
        //public int Id { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Mobile { get; set; }
        public string JobTitle { get; set; }
        public string Hint { get; set; }
        public int AddressId { get; set; }
        public bool IsActive { get; set; }
        //
        // Summary:
        //     Gets or sets the date and time, in UTC, when any user lockout ends.
        //
        // Remarks:
        //     A value in the past means the user is not locked out.
        [Column(TypeName = "datetime")]
        public override DateTimeOffset? LockoutEnd { get; set; }
    }

我什至不能做这样的事情

 SystemUser user = _context.SystemUser.Where(s => s.UserName == User.Identity.Name).FirstOrDefault();

因为那根本不会带回任何东西。我似乎动不动就受挫:(

我正在使用 VS2017,MySql,Pomelo.EntityFrameworkCore.MySql。任何帮助将不胜感激。提前致谢。

为了让这个工作我必须做的是

它现在正在运行,但我不知道还有什么其他隐藏的宝石在等着我

对于自定义 IdentityUser,无需将 SystemUser 添加到 DbContext

按照以下步骤操作:

  1. SystemUser

    public class SystemUser : IdentityUser<int>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        //your other properties
    }
    
  2. ApplicationDbContext

    public class ApplicationDbContext : IdentityDbContext<SystemUser,IdentityRole<int>,int>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }
    }
    
  3. IdentityHostingStartup

    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(
                        context.Configuration.GetConnectionString("ApplicationDbContextConnection")));
    
                services.AddDefaultIdentity<SystemUser>()
                    .AddEntityFrameworkStores<ApplicationDbContext>();
            });
        }
    }
    
  4. 用例

    public class HomeController : Controller
    {
        private readonly ApplicationDbContext _context;
        private readonly UserManager<SystemUser> _userManager;
        public HomeController(ApplicationDbContext context
            , UserManager<SystemUser> userManager)
        {
            _context = context;
            _userManager = userManager;
        }
        [Authorize]
        public async Task<IActionResult> Index()
        {
            SystemUser user = _context.Users.Where(s => s.UserName == User.Identity.Name).FirstOrDefault();
            SystemUser user1 = await _userManager.FindByNameAsync(User.Identity.Name);
            return View();
        }
    }