/azure-vm-webapp-activedirectory

Example of how to enable Active Directory authentication on a web application hosted in an Azure VM.

Primary LanguageJavaScriptMIT LicenseMIT

azure-vm-webapp-activedirectory


Page Type Languages & Frameworks Services
Sample PowerShell
C#
JavaScript
ASP.NET
Virtual Machine
Key Vault
Active Directory

Enable Active Directory Authentication on a web application hosted on an Azure VM

This sample codebase demonstrates how to host a web application on an Azure Virtual Machine and authenticate users to it using Azure Active Directory via an app registration.

Prerequisite Tools

Running this sample

Setting Up the Azure Resources

The core infrastructure needs to be set up before an application can be published and registered with AD services.

  1. To begin, replace the variables with your desired resource names and run the commands in the script found at infra/config.ps1. The script contains Azure PowerShell commands that set up a resource group, key vault, and virtual machine. Additional commands create a self-signed certificate, load it into the Key Vault, and install IIS on the VM.

    • This script closely follows the commands laid out in this tutorial.
    • Please note that the cloud infrastructure can be manually provisioned in the Azure Portal; using infra/config.ps1 is completely optional.
    • You may implement your preferred web server technology - this sample uses IIS throughout.
  2. Create a rule in the Network Security Group (which was automatically created with the VM in Step 1) to allow inbound traffic on Port 443 (HTTPS).

  3. Get the fully qualified domain name (FQDN) of the VM you just set up - you'll use it in the upcoming steps.

Publishing the Application

Once the resources have been provisioned, the application and authentication solution can be set up.

  1. Create an App Registration.
  2. Add the FQDN of the VM as a Redirect URI in the newly created app registration.
  3. On the Quickstart blade, download the quickstart codebase that aligns to your requirements.
    • This project leverages the ASP.NET framework. Codebases for other frameworks (e.g., Java, Node, etc.) are available.
    • The application settings of the quickstart codebase will be preconfigured to reference the credentials of the app registration you created, which the app code will use to authenticate via Azure AD. If you leverage the code in this repository, you will need to update the ClientId in the app/AppModelv2-WebApp-OpenIDConnect-DotNet/Web.config file.
  4. Install the codebase's dependencies and publish the codebase on the VM.
  5. Serve the application via a web server technology (this project uses IIS).
  6. Test the application by navigating to https://<FQDN of your VM>. If you used a self-signed certificate, you'll need to accept the security warning.

Application Architecture

Architecture

Security Considerations

Additional Considerations

  • While the general premise will be compatible no matter which framework you use, the details of "Publishing the Application" may differ slightly if a framework other than ASP.NET is chosen.

Resources