Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET Identity with Azure Storage Table

0.00/5 (No votes)
1 Nov 2015 1  
How to: Using ASP.NET Identity using Azure Storage Tables

Introduction

This tutorial aims to fill in the gap on how to use ASP.NET Identity using Azure Storage Table, which is a better option for many than using the default SQL Server LocalDB and EntityFramework.

Background

Using the Code

The downloadable project uses the Single Page Application template, and all these steps have been done. Prior to using it, the connection string for the AzureStore must be configured. I also deleted all the NuGet packages so that the size of the file was below 10MB, Visual Studio should restore them upon building the solution.

  1. Create a ASP.NET (4.5 or greater tested) Web Application, and use the MVC, Web API or Single Page Application template, leaving the default Individual User Accounts authentication.
  2. From NuGet:
    1. Install the package WindowsAzure.Storage
    2. Delete these packages:
      1. Microsoft.AspNet.Identity.EntityFramework
      2. EntityFramework
    3. [Optional] In the Package Manager Console, run Update-Package so that everything is updated
  3. In the Models folder, create a class that will match your Azure Storage Table user data - here named AzureTableUser
    1. The class must inherit from TableEntity and from IUser.
    2. Implement the IUser interface (using Right-Click > Implement Interface)
      1. In my case, the ID variable is not used, so I made it return the UserName
      2. I made the UserName variable an auto-implemented property as that is enough in my case
    3. Add these properties:
      1. public string Password { get; set; }
      2. public string Email { get; set; }
      3. public int FailedLogIns { get; set; }
      4. public DateTimeOffset LockOutEndDate { get; set; }
      5. If you are going to use Roles, add public IList<string> Roles { get; set; }
      6. If you are going to use Claims, add public IList<string> Claims { get; set; }
  4. In the helpers folder, create a class that will work with your Storage Table - here named AzureStore
    1. Add the using statement for your Model namespace if needed
    2. The class must inherit from at least 4 of the interfaces in the Microsoft.AspNet.Identity namespace, these are:
      1. IUserStore<AzureTableUser>
      2. IUserPasswordStore<AzureTableUser>
      3. IUserLockoutStore<AzureTableUser, TKey>
      4. IUserTwoFactorStore<AzureTableUser, TKey>
    3. I used string as the TKey for the Lockout and TwoFactor stores as that allows to implement the GetUserById/GetUserByName using a string
    4. Implement all the interfaces. I recommend separating the implementations using #region directives.
    5. Add a private readonly CloudTable cloudTable field that will take care of executing the commands on your Storage Table.
    6. Create a constructor, and do the following to get the reference to the table:
      1. StorageUri storageUri = new StorageUri(new Uri("https://yourstorageaccountname.table.core.windows.net/"));
      2. CloudStorageAccount csa = CloudStorageAccount.Parse("connectionString");
      3. cloudTable = csa.CreateClouldTableClient().GetTableReference("yourTableName");
  5. Open the main Web.config file, and delete the EntityFramework sections.
  6. Open Startup.Auth (from App_Start)
    1. Remove unnecessary using directives
    2. Under ConfigureAuth, remove the first CreatePerOwinContext call, since we will not use the database that was created as part of the template
    3. Comment the lines that assign OnValidateIdentity in the new CookieAuthenticationOptions - this doesn't work unless implemented in AzureTableUser
  7. From the Models folder, delete the file IdentityModels.cs since it won't be used
  8. Open IdentityConfig (from App_Start)
    1. Remove unnecessary using directives
    2. Unless you will implement custom Email and/or SMS Two-Factor Authentication, delete the classes EmailService and SmsService
      1. Also delete the calls to new EmailService and new SmsService in the create function of the ApplicationUserManager class
    3. For each call to the default ApplicationUser class, change it to AzureTableUser
    4. In the call to new ApplicationuserManager under Create, pass as parameter a new instance of the AzureStore class created in step 7
    5. In the ApplicationSignInManager class, delete the override to CreateUserIdentityAsync
  9. Open the AccountController
    1. Change the calls to the default ApplicationUser to AzureTableUser
    2. Remove the calls that assign Hometown to the class above, unless you implemented it
  10. Open the ManageController
    1. Change the calls to user.PasswordHash for user.Password
    2. If you implemented a phone number property in the AzureTableUser class, change the call to user.PhoneNumber to that property.
    3. Otherwise, delete all the views and methods related to the phone number.
  11. Open the MeController
    1. Remove unnecessary using directives
    2. Remove the call to user.Hometown in the Get method unless you implemented it

Points of Interest

While doing this for the first time, I found out that creating the AzureStore class can be quite challenging. Even if you are not going to lock out accounts or use Two-Factor Authentication, the store must implement those interfaces and cannot throw an exception, so at least a dummy/hard-coded implementation must be done.

History

  • 11/1/2015: First version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here