Click here to Skip to main content
15,886,689 members
Articles / Hosted Services / Azure
Tip/Trick

ASP.NET Identity with Azure Storage Table

Rate me:
Please Sign up or sign in to vote.
4.60/5 (3 votes)
1 Nov 2015CPOL4 min read 21.6K   8   8
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Uruguay Uruguay
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSample project Pin
Gerard Mangues22-Sep-21 22:59
Gerard Mangues22-Sep-21 22:59 
QuestionSample Project Pin
Vishnu Rana30-Jan-18 2:56
Vishnu Rana30-Jan-18 2:56 
QuestionCode Download Pin
chris rosa5-Feb-16 12:12
chris rosa5-Feb-16 12:12 
AnswerRe: Code Download Pin
gromas3-Feb-17 5:37
gromas3-Feb-17 5:37 
QuestionI would recommend the NuGet package ElCamino.AspNet.Identity.AzureTable Pin
Jon Smith7-Nov-15 1:07
Jon Smith7-Nov-15 1:07 
AnswerRe: I would recommend the NuGet package ElCamino.AspNet.Identity.AzureTable Pin
Neohuman Software10-Nov-15 11:07
professionalNeohuman Software10-Nov-15 11:07 
QuestionNice solution! Pin
emeraldcutinc1-Nov-15 15:36
emeraldcutinc1-Nov-15 15:36 
AnswerRe: Nice solution! Pin
Neohuman Software1-Nov-15 15:39
professionalNeohuman Software1-Nov-15 15:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.