I'm new to ASP.Net.Core.Identity.2. I've previously rolled my own security but with Net.Core 2.2. I can't upgrade my code as most of it won't work so I've decided to make the switch. All I'm trying to do is add new properties to User
so I can do something like:-
User.FirstName //or
User.Identity.FirstName
//or some other syntax
I've read loads of articles and tried a fair few examples but I've gotten nowhere, the examples either don't work, give me design time errors or give me run time errors.
I've modified by classes as below. and updated the database. I can see the new fields in the database. But what do I do next? Getting this far was pretty easy but now I'm completely stuck.
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; }
}
I can't even do something like this
SystemUser user = _context.SystemUser.Where(s => s.UserName == User.Identity.Name).FirstOrDefault();
As that doesn't bring back anything at all. I seem to be thwarted at every turn :(
I'm using VS2017, MySql, Pomelo.EntityFrameworkCore.MySql. Any help would be very much appreciated. Thanks in advance.
To get this working what I had to do was
It's now working but I have no idea what other hidden gems await me
For customing the IdentityUser
, there is no need to add SystemUser
to DbContext
.
Follow steps below:
SystemUser
public class SystemUser : IdentityUser<int>
{
public string FirstName { get; set; }
public string LastName { get; set; }
//your other properties
}
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);
}
}
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>();
});
}
}
Use Case
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();
}
}