Extending Identity – Identity Sample and Blocking Users

The Identity framework is a great tool that allows asp.net developers to easily add authentication to their applications. It is functional with limited capabilities out of the box but I like to do some additional configuration to make it fully functional for the way that I work. This is the first post in a 3 part series on extending the ASP.NET Identity Framework.

Immediately after setting up my visual studio site with Individual User Accounts set as the authentication type I install the Identity Samples NuGet package. This package will add additional administrative functionality to the application including web based interfaces to manage users and roles. To install the Identity Sample simply run the following command in the NuGet package manager console.

UPDATE: Because Identity Sample modifies/replaces a large number of files (around 35) and it asks permission to replace every one and will completely replace your Home/Index.cshtml and Shared/_Layout.cshtml files blowing away any custom code that you had on these pages, I created an alternative NuGet package (https://www.nuget.org/packages/IdentityUserRoleManagement/). You can install IdentityUserRoleManagement using the command below.

Install-Package IdentityUserRoleManagement

After installing I will modify the Identity model to include an additional field titled IsBlocked. This will allow me to easily lock users out of my site if they are trolling or causing other issues. Blocking them is more effective then delete them because if they are deleted then they are able to sign back up again using the same email address. By blocking the account the user must sign up with a different email. Eventually the process of creating new emails to use becomes too much of a hassle for them and they eventually leave.

To create the IsBlocked field in the AspNetUsers table add the following property to the ApplicationUser class in the IdentityModel.cs file.

public bool IsBlocked { get; set; }

When the Identity framework is call upon the first time (when a user tries to register or login) the framework will use Code First to create the required tables and will add an IsBlocked filed into the AspNetUsers table. All that is required now is to check this field when a user logs in. To do this we simply add an if statement in the Login method of the AccountController.cs file.

Open the AccountController from the Controllers folder and scroll down to the HttpPost Login method (around line 70). Add the following check just after the if(user != null) condition as follows:

if(user != null) {
     if (user.IsBlocked){

With this in place all that is needed to block a user is to set the IsBlocked filed in the database to true for that user record. I create a HTML page called UserBlocked.html at the root of the application which is where I redirect the blocked users.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *