Using Auth0 with Microsoft Azure

Auth0 is as simple to integrate in an application deployed on Microsoft Azure as it is for any other environment. To get started, please see:


Tip: change Auth0 configuration when deploying to Microsoft Azure

You'll need to make some configuration changes when deploying to Microsoft Azure. Auth0 recommends creating one application per environment (e.g. Development, Test, Production). This is because each environment should have and use a different Client Id and Client Secret, as well as the appropriate Callback URL.

For ASP.NET applications, we recommend utilizing Web.config transformations to make configuration changes targeted for each environment. Application settings that appear in the transformed web.config will depend on the build target name used at compilation and deployment.

The following is an example of how you can compile and deploy your application. The example focuses on deploying to Production, but you can use it to create builds in your ASP.NET application targeting your other environments.

This is the base configuration in Web.config:

<add key="auth0:ClientId" value="YOUR_DEV_CLIENT_ID" />
<add key="auth0:ClientSecret" value="YOUR_DEV_CLIENT_SECRET" />
<add key="auth0:CallbackUrl" value="http://localhost:port/LoginCallback.ashx" />

The following snippet, Web.Release.config, contains the necessary transformations. We want to utilize the Release build and are targeting Production.

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="auth0:ClientId" value="YOUR_PROD_CLIENT_ID" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    <add key="auth0:ClientSecret" value="YOUR_PROD_CLIENT_SECRET" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    <add key="auth0:CallbackUrl" value="http://mysite.azurewebsites.net/LoginCallback.ashx" xdt:Transform="Replace" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>

If you need to refer to the Client Id, Client Secret or Callback URL, you can do so using the ConfigurationManager class. Below is an example of using the ConfigurationManager within an ASP.NET MVC Razor view.

<script src="https://cdn.auth0.com/js/lock/11.6.1/lock.min.js"></script>
<script type="text/javascript">
    var lock = new Auth0Lock('@System.Configuration.ConfigurationManager.AppSettings["auth0:ClientId"]', 'YOUR_AUTH0_DOMAIN');

    lock.show({
      callbackURL: '@System.Configuration.ConfigurationManager.AppSettings["auth0:CallbackUrl"]'
    });
</script>

Running web.config transformations are helpful whether deploying to a Microsoft Azure App Service or a Microsoft Azure Cloud Service.

Use the Web.config Transformation Tester to verify the results of any web.config transformations.