Asp.net Core 3.1 configuring Identity Framework using SQLite

8/14/2020 2:57:41 PM 15 min read

Firstly, you need to create a dbcontext class that contains a user property for SQLite connection.

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace Something.Identity
{
    public class SqliteDBContext : IdentityDbContext
    {
        private readonly IConfiguration _configuration;

        public SqliteDBContext(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            options.UseSqlite(_configuration.GetConnectionString("BlogConnectionString"));
        }

        public DbSet<User> ApplicationUsers { get; set; }
    }
}

As you can see, the SqliteDBContext class is inherited from IdentityDbContext instead of regular DbContext. In order to have that, you need to install the nuget packages which you can see from the using statements on top.


The User class is pretty simple and inherited from IdentityUser.

using Microsoft.AspNetCore.Identity;

namespace Something.Identity
{
    public class User : IdentityUser
    {
        public int IsActive { get; set; }
    }
}


Then, we are pretty much ready for configuring the startup class.


You will need to add this two lines in ConfigureServices method.

services.AddDbContext<SqliteDBContext>();
services.AddIdentity<User, IdentityRole>().AddEntityFrameworkStores<SqliteDBContext>();


Then you can use Dotnet CLI to add the migration and update the DB. Go to the root folder of the project and open the command line. First you may need to ensure Dotnet CLI and Entity Framework CLI is working properly. You can test it like that.


After that you can run the following commands

dotnet ef migrations add "initial" (This creates a migration folder that contains your identity classes)

dotnet ef database update (This will create the identity tables in the DB).


I use this tool to manage my SQLite Databases. You can open your database file and examine with that.

https://sqlitebrowser.org


Your tables should like this.


Now you are ready to scaffold you identity area in .net and enjoy. You can do it with these steps.

  1. Right Click your project, add new item.
  2. Add new scaffolding item.
  3. Select Identity.
  4. Select pages that you want. Initially you can choose login, logout, register.
  5. Then you might need to tweak the classes a little bit according to what do you want to achieve with your project.


After that you can add additional configurations to the Startup.cs under ConfigureServices method.

services.AddRazorPages();
services.ConfigureApplicationCookie(options =>
            {
                options.LoginPath = "/identity/account/login";
                options.AccessDeniedPath = "/identity/account/accessdenied";
            });

AddRazorPages enables you to navigate to Identity area. The other part sets the loginpath for your authorized areas.

You can use [Authorize] attribute on top of your controllers.


I hope this article can help you and you enjoyed the article.


Cheers.



Comments


There are no comments