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.

Install-package microsoft.aspnet.identity.samples -pre

The Identity Sample modifies/replaces a large number of files (around 35) and it asks permission to replace every one. However, it also gives you an option to Replace All. This is the option that I usually choose however you have to be careful with this option. It will completely replace your Home/Index.cshtml and Shared/_Layout.cshtml files blowing away any custom code that you had on these pages, thus the reason that I install it immediately after creating my project before I do anything else.

After installing the Identity Sample 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)

{

Response.Redirect("/UserBlocked.html");

}

…

}

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s