Tuesday, January 23, 2024

Managing Session Integrity in .NET Core Web Applications: A Middleware Approach

 In our journey of developing a .NET Core web application, we encountered a peculiar challenge with our time-tracking feature. The application worked seamlessly in a single tab, but issues arose when users attempted to use it in multiple tabs or windows.

Opening the application in multiple tabs caused the timer to aggregate time across all instances, leading to unexpected behavior. If, for instance, the application was opened in five tabs, it would log 5 minutes instead of the intended 1 minute after 1 minute had passed.

To address this behavior, we aimed to implement a solution that would log out the previous session when more than one instance of the application was opened. We explored various options, including handling it with JavaScript or creating a custom attribute, but ultimately settled on using custom middleware.

Why middleware, you ask? The primary reason is that the middleware's function executes for every request, providing us with a robust solution. So, what did we include in our middleware?

Upon a user's initial login, we store their email and Bearer Token in an SQL table. Subsequent logins generate a new Bearer Token, allowing us to track sessions. The middleware checks the combination of Email and Bearer Token by querying the table. If a record is found, we assume the request is from the same tab. However, if a user logs in from a new tab, a new Bearer Token is generated. Querying the database with this new token results in no rows, prompting us to replace the table entry with the new Bearer Token, thus allowing access to the application.



This middleware-based approach ensures session integrity by handling multiple instances, providing a seamless user experience while maintaining the expected behavior. If a user returns to a previous tab with an expired session, the middleware gracefully handles it by returning a 401 status, effectively logging out the user.

Below is the source code




Tuesday, June 2, 2020

Entity Framework Core Seed


Whenever we create our database using model classes in entity framework core, after initial or first migration our database tables remains empty.

We can seed or insert initial data by using below steps.

Create Model Class.

In first step we have created our model class



Create Connection String

Open appsettings.json file and add ConnectionStrings like mentioned below






Refer model in DbConectext class



First Migration

Next we will open Package Manager console (View >> Other Windows >> Package Manager Console. Once Package Manager console is launched we will execute our first migration




Update Database

In next step we will run update-database command from package manager console to sync our changes with database.




Once update-database command is finished we can connect to database from Visual Studio by launching SQL Server Object Explorer ( View >> SQL Server Object Explorer)

We can found our database inside (localdb)\MSSQLLocalDB >> Database >> {{ Your Database Name }}



In above screenshot DemoDB is our database and PostModels is the name of the table

Initially PostModels table is empty




In order to create table with some initial records we use seed method.

So first we will revert our last migration.

In last step we have updated database with update-database command so we can't directly use remove-migration command here.

So first we have to query __EFMigrationsHistory table which contains all the migration.



To remove last migration(which was InitialMigration) we have to restore database to migration which was before InitialMigration (which is CreateIdentitySchema), so we will issue below mentioned command from package manager console





We have only one row in __EFMigrationsHistory table now



If we refresh our database we found that now we don't have PostModels table in database now.






Now we will open our DbConectext class and below method to create PostModels table with some initial data



In next step we will add migration follow by update-database command



Now if we check PostModels table in database then we will found it contains 2 rows.




Managing Session Integrity in .NET Core Web Applications: A Middleware Approach

 In our journey of developing a .NET Core web application, we encountered a peculiar challenge with our time-tracking feature. The applicati...