名字和姓氏以及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。任何帮助将不胜感激。提前谢谢。

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

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

它现在正在工作,但我不知道还有什么隐藏的宝石在等着我

对于自定义IdentityUser,没有必要在DbContext中添加SystemUser

请按照以下步骤操作:

  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();
    }
    }
    

最新更新