Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Identity in ASP .NET Core 3.1 without EF Core?

I have a project in ASP.NET Core 3.1 and I want to implement Identity to my application. I've found a lot of examples with EF Core, but I'm using LinqConnect (Devart) instead of EF Core and database - SQL Server. I'm wondering how to implement identity to my project in a simple way.

like image 380
Kamil Avatar asked Feb 20 '20 08:02

Kamil


Video Answer


1 Answers

Basically all you need to do is to create your custom user store.

public class CustomUserStore : IUserStore<IdentityUser>,
                               IUserClaimStore<IdentityUser>,
                               IUserLoginStore<IdentityUser>,
                               IUserRoleStore<IdentityUser>,
                               IUserPasswordStore<IdentityUser>,
                               IUserSecurityStampStore<IdentityUser>
{
    // interface implementations not shown
}

And then inject it :

public void ConfigureServices(IServiceCollection services)
{
    // Add identity types
    services.AddIdentity<ApplicationUser, ApplicationRole>()
        .AddDefaultTokenProviders();

    // Identity Services
    services.AddTransient<IUserStore<ApplicationUser>, CustomUserStore>();
    string connectionString = Configuration.GetConnectionString("DefaultConnection");
    services.AddTransient<SqlConnection>(e => new SqlConnection(connectionString));
    services.AddTransient<DapperUsersTable>();

    // additional configuration
}

It is documented in the official documentation.

like image 94
Bruno Martins Avatar answered Oct 17 '22 00:10

Bruno Martins