ASP.NET Core Identity Using PostgreSQL
Following on from my much older posts about using ASP.NET Identity 2 to manage user accounts in MVC 4 sites, today I needed to use Identity on an ASP.NET Core MVC site.
As with previous versions, the current Identity library for .NET Core 1.1 uses Entity Framework out-the-box. Luckily it's much easier to change this behaviour for the simplest register/log-in flow.
Install
Firstly you need to get the right NuGet package. For the Identity library without Entity Framework this is:
Microsoft.AspNetCore.Identity
Once this is installed in your project you need to provide your own implementation of the IUserStore<TUser>
and IRoleStore<TRole>
.
Unlike previous versions the TUser
no longer needs to implement a specific interface.
For my project I was using PostgreSQL as the database and Dapper.Contrib as the ORM. My User class was simply:
[Table("\"user\"")]
public class User
{
[ExplicitKey]
public Guid Id { get; set; }
public string UserName { get; set; }
public string NormalizedUserName { get; set; }
public string Email { get; set; }
public string PasswordHash { get; set; }
}
(I had to do some faffing around to make sure the table name was detected correctly in my schema but could have called the table anything, it doesn't have to be called "user").