Welcome!

Security Authors: Liz McMillan, Jason Thompson, Xenia von Wedel, Elizabeth White, Greg Akers

Related Topics: .NET, Silverlight, Web 2.0, Cloud Expo, Security

.NET: Article

Wildcard Subdomains in Windows Azure

Want to support my.fancy.app.can.serve.up.anything.mybrand.com in your Windows Azure application?

Now, once you do this, all traffic for all subdomains of adamhoffman.net is routed to wildcardsubdomain.cloudapp.net. This is true not only of 3rd level subdomains, but also of 4th level (hello.world.adamhoffman.net) and all levels beyond it (this.is.really.cool.adamhoffman.net). Once we have this, it's really just a matter of inspecting the request to make multi-tenant decisions in our application code using Request.Url.Host which will contain the full domain name that was originally asked for in the request (not the cloudapp.net name), like this:

protected void Page_Load(object sender, EventArgs e)
{
	string[] domainparts = Request.Url.Host.Split(new char[] { '.' });
	if (domainparts.Length > 2)
	{
		// there must be a subdomain in front of the two last parts 
		// (i.e. xxx.adamhoffman.net or xxx.yyy.adamhoffman.net, etc.)
		System.Text.StringBuilder sbOut = new System.Text.StringBuilder("Subdomain Segments: <br/>");
		for (int i = 0; i < domainparts.Length - 2; i++)
		{
			sbOut.Append(domainparts[i] + "<br/>");
		}

		this.output.Text = sbOut.ToString();
	}
}

Now, some of you are saying, hey, can't I use the page routing features of ASP.NET or the MVC framework to add routes to this party as well? Again, yes and no. The out of the box routing functionality does not appear to support subdomain routing, but Maarten Balliauw has done some work to extend it and make this possible. While this is a potential nicety, it's not actually necessary to figure out what subdomain was the source of an original request, and therefore we won't cover that here.

To be sure, there are many DNS providers out there that support wildcard CNAMEs. Route 53 is one of them, and Go Daddy is not. If you've successfully done this with a different DNS provider, drop me a line and I'll add the details here.

Source code is here.

Interesting side note - it appears that you can actually use A records and rely on the IP of an Azure deployment as long as you don't delete and redeploy your, um, deployment... I still wouldn't recommend it, since you'll undoubtedly forget to update your DNS servers the one time you do actually delete and redeploy. Nonetheless, good reading on the subject is here.

Ready to try this yourself? Get a FREE 90 Day trial of Windows Azure here.

More Stories By Adam Hoffman

Adam is an technical evangelist working for Microsoft. By day, you can likely find him somewhere in the Midwest, driving to somewhere, ready to figure out how the cloud can save your family from certain doom, and make you rich and successful in the process. Before he started evangelizing, Adam was a Senior Developer Lead for Microsoft in Redmond, working on Office 365, BPOS, and Office Live. He misses Redmond, and the excitement of the mother ship, but the call of bitter cold and lots of snow in Chicago was too much for him, and he had to return.

Prior that that he developed software and ran teams for Thomson Reuters, Method Engine (which he co-founded), VSA Partners, Navigant Consulting, Andersen Consulting, and a couple of CASE tool companies way back in the day. His favorite possession is a clone of the original Altair computer, running BillG's 8K BASIC, which he's still intending to hook up to the cloud, someday, somehow.

You can read his Windows Azure Blog at http://stratospher.es, and tweeter-er him at http://twitter.com/stratospher_es.

Additionally, if you'd like to keep track of his cloud connected car, you can follow it on Twitter at http://twitter.com/cloudcar1.