Software Engineering, Project Management, and Effectiveness
This post is a quick step through of creating a Windows Azure cloud project that authenticates using ASP.NET Forms Authentication with SQL Server as the user store.
The core steps are very much the same as my previous post How To Use ASP.NET Forms Auth with Azure Tables. The key difference is step 7 and step 8, which specify the connection to SQL Server.
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 Login Page. Use Solution Explorer to add a new Web form named Login.aspx to the WebRole1 site.
Step 3. 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 4. Configure ASP.NET to use Forms Authentication In Web.config, add the following line insde the <system.web> tag: <authentication mode="Forms" />
Step 5. 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 6. Set up the SQL Membership Database In this step, you configure the SQL data store for membership. This is accomplished through the use of the aspnet_regsql.exe utility. Details on aspnet_regsql.exe can be found at: http://msdn.microsoft.com/en-us/library/ms229862(VS.80).aspx
Step 7. Add the SQL Connection String In Web.config, add the connection string to the connectionStrings tag using the <add> tag as follows:
<connectionStrings> <add name="MyLocalSQLServer" connectionString="Initial Catalog=aspnetdb;Data Source=MyServerName;Integrated Security=SSPI"/> </connectionStrings>
Step 8. Configure ASP.NET to Use the SQL Membership Provider In this step, you configure the Web application to use the SQL Membership Provider.
In Web.config, add the following lines inside the <system.web> tag:
<membership defaultProvider="MySqlMembershipProvider" > <providers> <clear/> <add name="MySqlMembershipProvider" connectionStringName="MyLocalSQLServer" applicationName="MyAppName" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> </providers> </membership>
Step 9. Add Test Code to Page_Load to Show the Forms Authentication Details Add a using statement to Default.aspx.cs in your WebRole1 project to add a reference to System.Web.Security. Add the following code to Page Load of Default.aspx.cs in WebRole1: protected void Page_Load(object sender, EventArgs e) { Response.Write("Hello, " + Server.HtmlEncode(User.Identity.Name) + "<br />"); }
Step 10. test registering a new user and logging in to the application
The Web application should return something along the following lines:
Hello, waldo
Share your feedback or results in the comments. We’re path paving along with you.
My Related Posts
Is there a way of creating a local copy of SqlAzure so we are coming closer to seeing the authentication functionality we would see in the cloud?
Hey John
Not that I know of, but I'll see if I can find out the story from the product team.