0
$\begingroup$

I'm working on my final year project(fyp) and intended to add an account system using EF Core Identity. When I started my project I found it difficult to get to grasps with the best way to use Identity and left the work I done learning it in a separate project. I started my fyp in a new project and this is how my DbContext looks

    public class SystemDbContext : DbContext
    {
        public SystemDbContext(DbContextOptions<SystemDbContext> options) : base(options) { }

Most of what I seen to add Identity looks like this

    public class SystemDbContext : IdentityDbContext

If I try changing my DbContext to look like the line above, it keeps failing to migrate, and most of the information I have found detail that it is just really hard to add Identity in later.

As part of my fyp I had to make a repository on GitHub to show committs and my work over time so I would like to avoid starting from scratch if I can; Or if there is a way I can start from scratch and somehow incorporate that into my GitHub, I would appreciate advice on that.

Basically I want to know if there is a way to add Identity at this point in my project, or is my only option starting over?

$\endgroup$
1
  • 1
    $\begingroup$ Just add a separate identity context. I think it's always a good idea to not mix authentication concerns with the rest of the application. $\endgroup$ Commented Apr 30, 2025 at 10:53

1 Answer 1

1
$\begingroup$

You have 2 options (aside from starting all over again)

  1. Add a second dedicated context that inherits IdentityDbContext that should be configured to the same database instance as the original SystemDbContext. In this new `IdentityDbContext` derived context you will map to dbsets only the tables (entities) that you will need for the authentication flow such as `ApplicationUser`, `UserRoles`, `Roles` etc...You could also map to tables (entities) that are used in the `SystemDbContext` but you will need to exclude them from migrations to avoid creating duplicate migrations that will fail executing against the database. You can achieve this using ExcludeFromMigrations method in the OnModelCreating override. The opposite is also true you will be able to map to Identity entities (tables) to use them in your original SystemDbContext by configuring them via ExcludeFromMigraitons in order to instruct the SustemDbContext not to track changes on these entities and create migration for them.

  2. The second approach, which is way better from my point of view, is to create a dedicated Identity (Authority) web service or web app that will be responsible for user authentication(log in) and will act as an Identity (Authority) service for the your main app. This dedicated identity service will have again its own IdentityDbContext derived context that again could use the same database as your original context from your main app and just map to specific tables that will be needed for the user authentication or it could use it's own database instance which would be a more advanced approach. Here you can review samples of such dedicated Identity apps that act as an Authority provider for other third party web apps or web services. I think that you will really impress your teachers and classmates with such an approach if you implement it. In the end your solution will contain two asp.net web api/razor/blazor apps one Identity app for user login (authentication), password recovery, two factore authentication etc.. and another Systems app for your main business logic that will use the first Identity app for user login/authentication/token issuing. To achieve this you will need to integrate microsoft entra, azure active directory or identity server in your identity service and your main system app in order to comply with oauth2/openid connect protocols. For example if you decide to go with IdentityServer you can follow this tutorial. In the long run you can "plug in" other apps to this Authority app i.e. System2 app which will enable single sign on between apps i.e. users logged in in the System app would be able to automatically sign in the System2 app without re-entering username and password.

$\endgroup$
Sign up to request clarification or add additional context in comments.

1 Comment

I really appreciate the detailed answer :) with the time I have left I will probably go with the first option, but will try out the second option over the summer to better my skills, thanks so much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.