How to Use ASP.NET Membership in Azure



Last updated: January 28th, 2024

Problem

When using Windows Azure, I recently noticed strange behavior with the membership provider with SQL Azure. The application is deployed to many different environments (dev, staging, prod) and was previously using the same database for membershp in all environments. That connection string was set in the Web.config and was not in the .cscfg files.

When the requirement came up to have different membership databases, one for each environment, I thought all I needed to do was copy the existing database and update the .csdef and .cscfg to use the membership connection string. Unfortunately the portal was showing the wrong information, it was showing the connection string I wanted to use but the application was actually using the one set in the Web.config.

Because of this strange behavior, integration tests were failing. I thought there was some problem with the way the machine key and the password was being validated. When I looked further, the call to authenticate a user was returning the message: Could not determine storage version; a valid storage connection or a version hint is required.

Solution

It turns out that the Web Role, this is using a Cloud Service, needs to update the connection string for the membership provider when it starts up. One of my co-workers pointed me to these 2 resources: Moving database connection string to Azure service configuration and ASP.NET Universal Providers + Azure cscfg.

What this meant was that I needed the Web Role to change the Web.config in the OnStart method. So I put the connection string in the Web.config (as a place holder and for running without Azure), the .csdef and the .cscfg for each environment, with each .cscfg using its own database for membership.

The steps to fix were:

  • Put a default connection string in the Web.config
  • Update the .csdef with the connection string name.
  • Modify all .cscfg files to use the connection string name and value (the database I want for membership in that environment).
  • Change the connection string in the application during the OnStart event for the Work Role by modifying the WebRole.cs file in the web project.

The end result is:

This gets called in the OnStart method by using the code below. This is because using IIS express it isn't needed. Locally, I never saw the problem anyway, only in Azure. To see this happen locally you need to change the Azure project to use the full IIS and to ignore the emulated part.

Automation

If you want to use the command line to make a membership database with SQL Azure, you cannot use the typical way, you need to use a special Azure specific command accessible with: aspnet_regsqlazure.zip.

To run that command in PowerShell to create membership and roles you would do:


Comments

No Comments

Post Comment

Prove you are human 9 + 13 =

Tagged: Windows Azure


Join my email list!



ryan
About Me

With 15 years in tech, I've excelled as a senior software engineer, specializing in ASP.NET, C#, SQL, Azure, and front-end technologies. I've led diverse projects across various sectors, from startups to global corporations, particularly during my decade in the San Francisco Bay Area.


Sign Up With SoftSys Hosting! (My host)