Software Engineering, Project Management, and Effectiveness
While ramping up for Windows Azure, we're getting our feet wet with some basic application scenarios. This is a quick step through of wiring up ASP.NET Forms Authentication to use Azure Table Storage for the user store.
It’s longer than I like but I wanted to err on the side of being explicit. It’s nice to know that when you’re going down a path that somebody else has been there and done that and you’re not on your own. While your path may vary, at least you know this is one path that at least a few of our team members went down while creating repros for Azure authentication scenarios with ASP.NET.
Stepping back, the big thing to know is that we didn’t find a Table Storage Membership provider for ASP.NET out of the box, but we found one in the additional C# samples. You’ll see this in step 7. Now, let’s start paving some paths …
Summary of Steps Here are the steps at a glance:
Here we go …
Step 1. Create a New Cloud Service Project. In this step, you create a new cloud service project in Visual Studio:
Step 2. Add a Reference to the AspProvider Project for the Azure Table Storage Provider We didn’t see a Table Storage Membership provider for ASP.NET out of box, but there are samples available for download:
Step 3. Add a Login Page. Use Solution Explorer to add a new Web form named Login.aspx to the WebRole1 site.
Step 4. Create a Way for New Users to Register Add the following two lines into the Login.aspx <form> tag
<asp:Login runat="server" /> <asp:CreateUserWizard runat="server"></asp:CreateUserWizard>
It should resemble the following:
<form id="form1" runat="server"> <div> <asp:Login runat="server" /> <asp:CreateUserWizard runat="server"></asp:CreateUserWizard> </div> </form>
Step 5. Configure ASP.NET to use Forms Authentication In Web.config, add the following line insde the <system.web> tag: <authentication mode="Forms" />
Step 6. Configure ASP.NET to restrict Anonymous Users In Web.config, add the following line inside the <system.web> tag:
<authorization> <deny users="?" /> <allow users="*" /> </authorization>
Note – The preceding configuration allows only authenticated users to access the application. The "?" indicates unauthenticated users and the "*" indicates all users. By denying unauthenticated users, any requests made by unauthenticated users are redirected to the login page. The loginUrl attribute of the <forms> element determines the name of the login page. The default setting of this attribute is Login.aspx.
Step 7. Configure ASP.NET to Use the Azure Table Storage Provider In this step, you configure the Web application to use the AspProviders.TableStorageMembershipProvider.
In Web.config, add the following lines inside the <system.web> tag:
<membership defaultProvider="TableStorageMembershipProvider" userIsOnlineTimeWindow = "20"> <providers> <clear/>
<add name="TableStorageMembershipProvider" type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageMembershipProvider" applicationName="AspProvidersDemo" />
</providers> </membership>
Step 8. Configure the ASP.NET Membership Provider In Web.config, add the following code to the <appSettings> tag as follows:
<appSettings> <!-- account configuration --> <add key = "TableStorageEndpoint" value="http://127.0.0.1:10002/devstoreaccount1"/> <add key = "AccountName" value="devstoreaccount1"/> <add key = "AccountSharedKey" value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="/> </appSettings>
Note that we don’t have a lot of details on the AccountSharedKey, but we saw Jim Nakashima uses this value, so it’s good enough for now, until we know more.
Step 9. Add Test Code to Page_Load to Show the Forms Authentication Details
protected void Page_Load(object sender, EventArgs e) { Response.Write("Hello, " + Server.HtmlEncode(User.Identity.Name));
FormsIdentity id = (FormsIdentity)User.Identity; FormsAuthenticationTicket ticket = id.Ticket;
// optional - but if you use this add a reference to System.Web.Security Response.Write("<p/>TicketName: " + ticket.Name ); Response.Write("<br/>Cookie Path: " + ticket.CookiePath); Response.Write("<br/>Ticket Expiration: " + ticket.Expiration.ToString()); Response.Write("<br/>Expired: " + ticket.Expired.ToString()); Response.Write("<br/>Persistent: " + ticket.IsPersistent.ToString()); Response.Write("<br/>IssueDate: " + ticket.IssueDate.ToString()); Response.Write("<br/>UserData: " + ticket.UserData); Response.Write("<br/>Version: " + ticket.Version.ToString()); }
Step 10. test registering a new user and logging in to the application
The Web application should return something along the following lines:
Hello, bob TicketName: bob Cookie Path: / Ticket Expiration: 3/17/2010 3:04:40 PM Expired: False Persistent: False IssueDate: 3/17/2010 2:34:40 PM UserData: Version: 2
Share your feedback or results in the comments. We’re path paving along with you.
My Related Posts
good one, missed closing these:
</providers>
</membership>
worked, the page displayed similar to what you state here, but then the infrastructure blew with "Windows Azure Development Fabric Load Balancer has stopped working"
Regarding the walkthrough - liked a lot, prescriptive and helpful to bootstrap!
Hey Alik
Good catch - fixed!