【问题标题】:FirstName and LastName and other fields on ASP.Net.Core.Identity 2.0ASP.Net.Core.Identity 2.0 上的 FirstName 和 LastName 以及其他字段
【发布时间】:2019-07-12 03:52:09
【问题描述】:

我是 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。任何帮助将不胜感激。提前致谢。

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

  • 删除存储的身份 cookie
  • 将数据从字符串更改为 int(1、2、3 等)
  • 在数据库中将 ID 列从 string 更改为 int

它现在可以工作了,但我不知道还有什么其他隐藏的宝石在等着我

【问题讨论】:

标签: asp.net-core asp.net-identity


【解决方案1】:

要自定义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();
        }
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多