How To Use ASP.NET Forms Auth with Roles in Azure Tables

In my previous post, How To Use ASP.NET Forms Auth to Azure Tables, we walked through creating a simple Web page that authenticates using ASP.NET Forms Authentication and stores the users in Azure Tables using the sample Azure Table Storage provider for ASP.NET.

In this post, we extend that sample to include Roles Authentication, where the roles are stored in Azure Tables.  Normally, I'm not a fan of extending samples, but in this case, it's simple enough that I don't want to repeat my previous post here.

Before you begin, create the sample in How To Use ASP.NET Forms Auth to Azure Tables, if you haven't already.

Summary of Steps
Here are the steps at a glance to add Roles authorization:

  • Step 1. Configure roleManager Settings in web.config
  • Step 2. Add Test Code to Page_Load to Show the Roles Authorization
  • Step 3. Test Your Repro

Step 1. Configure roleManager Settings in web.config
Add the following to Web.config, to point the roleManager to the Azure Table Storage:

    <roleManager enabled="true">
      <providers>
        <add applicationName="FormsAzTables" name="TableStorageRoleProvider"
            type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageRoleProvider" />
      </providers>
    </roleManager>

Step 2. Add Test Code to Page_Load to Show the Roles Authorization
Add the following test code to Page_Load in default.aspx.cs:

Response.Write("<br/>Is in Users: " + (Roles.IsUserInRole("Users") ? "true": "false") );
if (!Roles.RoleExists("Users")) Roles.CreateRole("Users");

if (!Roles.IsUserInRole("Users")) Roles.AddUserToRole(User.Identity.Name, "Users");

Step 3. Test Your Repro
Press F5 to start with debugging.  You should see something like the following output:

Hello, bob
TicketName: bob
Cookie Path: /
Ticket Expiration: 3/22/2010 11:50:02 AM
Expired: False
Persistent: False
IssueDate: 3/22/2010 11:20:02 AM
UserData:
Version: 2
Is in Users: true

The first time you run this, Is in Users should return false, but the second time you run this, it should return true.

My Related Posts