An open API service indexing awesome lists of open source software.

https://github.com/arnovb-github/aspnetcore31withsqliteandcustomidentity

Sample project showing common customizations in a ASP.NET Core app
https://github.com/arnovb-github/aspnetcore31withsqliteandcustomidentity

Last synced: 11 months ago
JSON representation

Sample project showing common customizations in a ASP.NET Core app

Awesome Lists containing this project

README

          

# Purpose #
This project shows a number of common customizations to using ASP.NET `Authentication` with `Individual User Acounts`. That is: you control the authentication database. I created it because I simply cannot remember the steps and they keep changing with every iteration of .Net Core. This is for .Net Core 3.1, it may be relevant to 5.0, it may not be.

- Use SQLite instead of MS SQL Server (this is entirely optional).
- Customize `IdentityUser`.
- Change the Primary Key data type.
- Rename the columns that `Microsoft.AspNetCore.Identity` uses by default.

# Step-by-step guide #
- In Visual Studio, create a new ASP.Net Core 3.x project (I used ASP.NET Core Web Application).
- make sure you select the option to use Authentication and set it to Individual Accounts
- Delete all files under `Migrations`

You can do also this with the `dotnet` CLI and Visual Studio Code. You can even bring in Authentication in existing projects, this repository is not about that.

# Using SQLite #
*Skip this step if you are fine with MS SQL Server*
- Install the `Microsoft.EntityFrameworkCore.Sqlite` package. Do not just bring in the `Microsoft.EntityFrameworkCore.Sqlite.Core` package.
- (optionally)Create a folder that will hold your database
- (optionally)Remove the now unneeded reference to `Microsoft.EntityFrameworkCore.SqlServer`
- Edit `appsettings.json` to point to SQLite (see source)
- Edit the `ConfigureServices` method in `Startup.cs` to use SQLite (see source)

This will make you application use SQLite instead of MS SQL Server

# Customize IdentityUser #
- Add a class `ApplicationUser` (I put it in a folder `Models`), make it inherit from `IdentityUser`. This allows you to extend the properties of `IdentityUser` and it also sets the primary key to use an `int` instead of a string (default).
- Replace all other instances of `IdentityUser` in the entire project with `ApplicationUser`
- Do not forget to bring in the namespace of `ApplicationUser` in the files where it is used (like `_LoginPartial.cshtml`)

# Customize table names #
In `ApplicationDbContext`, change the class declaration to:

```c#
public class ApplicationDbContext : IdentityDbContext, int>
```

Changing the type parameters of `IdentityDbContext` took me days to figure out. You'd think you'd be fine with passing in just your customized `IdentityUser` type, e.g. `ApplicationUser`. But no. You also need the role and type. Go figure. I believe that from this point on, you can extend the declaration of `IdentityDbContext` with other modified `Identity` classes, but that is beyond the scope of this example.

Once you get the context to work at all, overriding the creation of the tablenames is simple, also notice the overriding of the default primary key types (to `int`):

```c#
// override default table names
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity().ToTable("Users"); // Your custom IdentityUser class
builder.Entity>().ToTable("UserLogins"); // use int not string
builder.Entity>().ToTable("UserTokens"); // use int not string
builder.Entity>().ToTable("UserClaims"); // use int not string
builder.Entity>().ToTable("UserRoles"); // use int not string
builder.Entity>().ToTable("RoleClaims"); // use int not string
builder.Entity>().ToTable("Roles"); // use int not string
}
```

Add and apply a migration in the Package Manager Console:
- Add-Migration CreateIdentitySchema
- Update-Database

Notice that if you changed you database to SQLite, EF is smart enough to generate SQLite syntax.

Inspect your database. It should now have the tablenames your defined in `ApplicationDbContext`. Your `Users` table should have a column `ExampleColumn`.

Run you app. (It may complain about SSL. Accept the suggested certificate for now.) It should just work.