Departmental Application Migration to Azure – Part 4 – ADFS with Azure web app


This is part of a series of blogs.  See the preceding blog entries:

As mentioned before on this blog series, for me the authentication is the major challenge for this proof of concept.  I need this web application to support Enterprise Single Sign-On through Active Directory.  I’ve decided to use ADFS 2.0 and Windows Identity Foundation (WIF).  In the last article, I’ve showed how to get an on-premise web application using ADFS for authentication.  On this blog entry, I’m going to the cloud, so let’s get started!

Prerequisites

You need to have ADFS 2.0 installed.  This was covered in a previous blog entry from this series.

You’ll also need the Windows Identity Foundation (WIF) SDK.  You can download it at http://www.microsoft.com/downloads/details.aspx?familyid=C148B2DF-C7AF-46BB-9162-2C9422208504&displaylang=en.

You’ll also need the Windows Azure SDK.  I’m using the one from June 2010, you can download it at http://www.microsoft.com/downloads/details.aspx?familyid=21910585-8693-4185-826E-E658535940AA&displaylang=en.

To top it all, you can install the Windows Azure Tools for Microsoft Visual Studio 1.2 (also from June 2010).  You can download it at http://www.microsoft.com/downloads/details.aspx?familyid=2274A0A8-5D37-4EAC-B50A-E197DC340F6F&displaylang=en.

I’ll use Visual Studio 2010.

Creating a web role in Dev Fabric

Let’s start by creating a vanilla web role in Dev Fabric.  The Dev Fabric is the dev environment provided with the Windows Azure SDK, basically, the developer work station.

I start by creating an empty solution which I name AdfsAuthenticatedAzure.

I then create a Windows Azure Cloud Service named MyCloudFacade.

image

A Cloud service is a unit of deployment.  A service contains different roles and a configuration for the service.  The roles are all deployed together in one package.

I create one role within that service, a web role named AdfsAuthenticatedWebRole.

image

Like the solution on-premise I did in the last blog entry, I’m trimming the boiler-plate web project:

  • Delete the Account folder since ADFS will perform those functions.
  • In web.config:
    • Delete the connection string.
    • Delete the authentication xml node.
    • Delete the membership xml node.
    • Delete the profile xml node.
    • Delete the roleManager xml node.

I can now hit F5 and look at my wonderful generic web site!

Deploying to Azure

To integrate with ADFS, I’ll need my web site to be using SSL.  I’ll attach a certificate to my service and enable an https input endpoint.  I’ll start at the properties of my web role.

image

At the certificates tab:

image

I then add a certificate and select one of my local (self-issued) certificates.

image

I then go at the Endpoints tab, check the HTTPS check box and select the SSL certificate name I just added.

image

The Windows Azure Tools for Microsoft Visual Studio 1.2 allow you to deploy to Azure directly, instead of simply packaging (where you have to deploy the package using the web console).  There are plenty of web resources showing how to deploy an Azure service to the cloud.  If you have questions, please ask.

While it’s deploying, I’ll add the same certificate to my service in the cloud.  In my Azure Service Web console:

image

I need a PFX file to upload.  I use IIS Server Certificates module and export the same self-issued certificate I used in Visual Studio.

image

I can then save the certificate locally and protect it with a password.  I can then import it in the Azure web console, using the same password.

I first deploy to staging then to production.  As I mentioned in a previous blog entry, staging isn’t a real staging as Microsoft recommend to have a separate subscription altogether for staging vs production.  This is important here, since staging would be harder to integrate with ADFS given its unpredictable URL (Azure generates staging URLs using a GUID).

Of course I get warnings because my certificate is self-issued and hence unrecognized by IE as trustworthy.

Integrating ADFS in Dev Fabric

The steps here are a repetition of what I covered in the previous blob entry of this series, so I won’t go into much details.

First, I choose to Add STS Reference and set https://localhost:444.

image

(The https in-point port is set to 443, which is the default https port, but this port being used by IIS, the web role responds to port 443 in the dev environment)

For the rest of the Wizard, it’s the same steps I used in the precedent blog of this blog series.

I then run the modified web role in my dev environment.  I get an error from the ADFS site since my application isn’t recognized there yet.  So in the ADFS console, I create a relying trust party named Adfs Authenticated Dev.  The reason I append the name by dev is that we’ll need to create an entry for the production endpoint as well, since it has a different URL.  In the claims rules, I make sure ADFS will send me a few claims (e.g. windows name, email, title, etc.).

I can now use my local web role, after doing the form validation fix I spoke about last time.  I also remove the HTTP input endpoint since ADFS will only trust me if I come from HTTPS.  Finally, I add a grid view showing all the user claims like I did last time.

Integrating ADFS with Azure

I then run the Add STS Reference in Visual Studio again.  This time I use my production Windows Azure URL (https://****.cloudapp.net).  I then go in the federation metadata file and remove reference to localhost:

<EndpointReference xmlns="http://www.w3.org/2005/08/addressing">
    <Address>
https://localhost:444/</Address>
</EndpointReference>

After this, my meta isn’t usable for dev, but I only need to maintain it for production.

I run the ADFS relying party trust wizard again, still pointing to that meta data file.  I call this party Adfs Authenticated Prod.

The problem I’ll run into now is that the w-reply query string will forward me to my production web role.  This is related to how WIF is creating the URL to the STS.  It uses a URL specified in the configuration file.  The fix for this is documented in the Windows Azure training kit:  override the query string using the WS-Federation authentication module by adding the following code in the Global.asax.

/// <summary>
/// Retrieves the address that was used in the browser for accessing
/// the web application, and injects it as WREPLY parameter in the
/// request to the STS
/// </summary>
void WSFederationAuthenticationModule_RedirectingToIdentityProvider(object sender, RedirectingToIdentityProviderEventArgs e)
{
    //
    // In the Windows Azure environment, build a wreply parameter for  the SignIn request
    // that reflects the real address of the application.
    //
    HttpRequest request = HttpContext.Current.Request;
    Uri requestUrl = request.Url;
    StringBuilder wreply = new StringBuilder();

    wreply.Append(requestUrl.Scheme);     // e.g. "http" or "https"
    wreply.Append("://");
    wreply.Append(request.Headers["Host"] ?? requestUrl.Authority);
    wreply.Append(request.ApplicationPath);

    if (!request.ApplicationPath.EndsWith("/"))
        wreply.Append("/");
    e.SignInRequestMessage.Reply = wreply.ToString();
    e.SignInRequestMessage.Realm = wreply.ToString();
}

We also need to add an audience URI in the web.config for 127.0.0.1 (if like me you did register to the STS as localhost).

      <audienceUris>
                <add value="
https://127.0.0.1:444/&quot; />
                <add value="
https://localhost:444/&quot; />

                  <add value="https://***.cloudapp.net/&quot; />
        </audienceUris>

I am now prepared to deploy my application in Windows Azure.

Copy Local

Now here is an issue that took me a lot of time to resolve.  Every time you put dlls that aren’t part of the .NET Framework or your projects, you have to specify Copy Local to true in the assembly property.

image

This is our case for the WIF assemblies.  This setting will force the WIF assembly to be packaged and sent to the cloud with the rest of the custom code.

Deploying ADFS integration to the cloud

I deploy the application to the staging Windows Azure environment.  As mentioned before, this environment won’t fully work since the URL is not constant.

Once it’s in staging I flick it to production.

Et voila!

So, not too trivial to setup, but in the range of the feasible 😉

Advertisements

3 thoughts on “Departmental Application Migration to Azure – Part 4 – ADFS with Azure web app

  1. Pingback: ADFS 2.0 Step by Step and How To guides « Vincent-Philippe Lauzon's blog

  2. Pingback: Single Sign-On from Active Directory to a Windows Azure Application Whitepaper « Vincent-Philippe Lauzon's blog

  3. Pingback: Departmental Application Migration to Azure « Vincent-Philippe Lauzon's blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s