Installation and Setup - WillSullivan/AzureTableIdentityStorageProvider GitHub Wiki
The following instructions are geared towards Visual Studio 2013. If you're using 2012, good luck :( If you manage to get it working, by all means, please write up a description of the steps you took and I'll be glad to include it!
##Create a New ASP.NET Web Application
Start with a fresh new ASP.NET MVC website. You can use the instructions here to create the website. Note, you want to select MVC as the application type, and select Individual User Accounts for the authentication type.
##Get the Asp.NET Identity Azure Table Provider
Grab the latest version from Nuget.
##Excise the Entity Framework
Remove the default Entity Framework implementation of the user model store. Open the file Models/IdentityModels.cs. You will want to rip out the ApplicationDbContext (eew) and change the base class of the ApplicationUser model. What you will see:
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
what you will change it to:
public class ApplicationUser : AzureTableUser
{
}
//ApplicationDbContext is removed!
##Add New (Limited) User Properties At this point, you can add any new properties you want to the model. Please note, you are limited to what can and cannot be stored in an Azure Table. You can learn more about these limitations here.
##Extend AzureTableUserStore
In addition to tearing out the context and rebasing the user model, you will want to implement your own version of the AzureTableUserStore. Below the ApplicationUser class definition (or in a different file), add the following:
/// <summary>
/// The user repository.
/// </summary>
public class UserStore : AzureTableUserStore<ApplicationUser>
{
/// <summary>
/// Creates a new instance of <see cref="UserStore"/>.
/// </summary>
/// <returns></returns>
public static UserStore Create()
{
return new UserStore(System.Configuration.ConfigurationManager.ConnectionStrings["AzureTableUserStorage"]
.ConnectionString);
}
/// <summary>
/// Initializes a new instance of the <see cref="UserStore" /> class.
/// </summary>
/// <param name="connectionString">The connection string.</param>
protected UserStore(string connectionString) : base(connectionString)
{
}
}
You can define this class differently, but there are two things you must do--You must extend AzureTableUserStore<ApplicationUser>
and you must pass the base class constructor the connection string to your Azure Table. For the rest of this example, I'll be using the above version with the static factory method.
##Add your Azure Table Connection String to the Configuration Yo will now need to add the following connection string within your web.config (the name of the connection string is significant):
<connectionStrings>
<add name="AzureTableUserStorage" connectionString="UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://127.0.0.1:10002/" />
</connectionStrings>
In your Azure website configuration within the Azure portal (or within Server Explorer in Visual Studio) you will need to add a connection string with the exact same name that points to your Table. This version will override the one defined within your web.config once you publish.
##Update the AccountController to Use Your Store You will need to replace the context store (ugh) with our lovely Azure Table store. Open Controllers/AccountController.cs. Change the constructor from
public AccountController()
: this(new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
to
public AccountController()
: this(new UserManager<ApplicationUser>(UserStore.Create()))
{
}
at this point, the Identity UserManager will now be using your implementation of the Azure Table store.
##Be Social
You will want to crank your social up to 11. Otherwise, why even bother using Identity? Open up App_Start/Startup.Auth.cs and uncomment all the social login stuff. I like to store all my auth stuff in AppSettings within web.config:
<appSettings>
<add key="webpages:Version"
value="3.0.0.0" />
<add key="webpages:Enabled"
value="false" />
<add key="ClientValidationEnabled"
value="true" />
<add key="UnobtrusiveJavaScriptEnabled"
value="true" />
<add key="GoogleAppId"
value="123" />
<add key="GoogleSecret"
value="abc" />
<add key="FacebookAppId"
value="123" />
<add key="FacebookSecret"
value="abc" />
<add key="TwitterAppId"
value="123" />
<add key="TwitterSecret"
value="abc" />
<add key="MicrosoftAppId"
value="123" />
<add key="MicrosoftSecret"
value="abc" />
</appSettings>
then expose these as internal static properties within Startup.Auth.cs
internal static string GoogleId = System.Configuration.ConfigurationManager.AppSettings["GoogleAppId"];
internal static string GoogleSecret = System.Configuration.ConfigurationManager.AppSettings["GoogleSecret"];
internal static string FacebookId = System.Configuration.ConfigurationManager.AppSettings["FacebookAppId"];
internal static string FacebookSecret = System.Configuration.ConfigurationManager.AppSettings["FacebookSecret"];
internal static string TwitterId = System.Configuration.ConfigurationManager.AppSettings["TwitterAppId"];
internal static string TwitterSecret = System.Configuration.ConfigurationManager.AppSettings["TwitterSecret"];
internal static string MicrosoftId = System.Configuration.ConfigurationManager.AppSettings["MicrosoftAppId"];
internal static string MicrosoftSecret = System.Configuration.ConfigurationManager.AppSettings["MicrosoftSecret"];
and them use these properties in the startup code:
app.UseMicrosoftAccountAuthentication(
clientId: MicrosoftId,
clientSecret: MicrosoftSecret);
app.UseTwitterAuthentication(
consumerKey: TwitterId,
consumerSecret: TwitterSecret);
app.UseFacebookAuthentication(
appId: FacebookId,
appSecret: FacebookSecret);
app.UseGoogleAuthentication(new GoogleAuthenticationOptions
{
});
##Publish
At this point, you should be good to go! Publish your website and try it out!
####Resources
From start to this point, I have to say the second most frustrating thing I had to deal with was learning both Azure Table storage and ASP.NET Identity at the same time. The most frustrating thing was getting my developer accounts set up for Twitter, Google+, Facebook and Microsoft (not meaning MSDN). For your benefit, here are some links you can use to create these developer accounts. After that point, good fricking luck.
- Microsoft: http://account.live.com/developers
- Facebook: http://developers.facebook.com
- Twitter: http://apps.twitter.com
- Google+: http://console.developers.google.com