Logging in to Azure

Authentication and logging-in to Azure

"<TenantId>"      is something like "adadadad-adad-adad-adad-adadadadadad"
"<ApplicationId>" is something like "40302010-feda-deaf-beef-deadbeef0123"

Setup service principal in Powershell

Install Azure Powershell according the docs

# Install the Azure Resource Manager modules from the PowerShell Gallery
Install-Module AzureRM
Install-AzureRM
Install-Module Azure

Import-AzureRM
Import-Module Azure

Create a certificate using makecert.exe

Fiddle with PowerShell

Fill in your Azure details

Powershell / X509

Powershell / Password

Azure XPLAT CLI / X509

Azure XPLAT CLI / Password

In my customer engagements, I usually push early for deployment automation of some sort. My preferred way to deploy to Azure is using Azure Resource Manager JSON Templates, alongside with developer-side automated scripts. Personally I also appreciate the notion of Service Principals, i.e. using "strong" credentials such as an X.509 Certificate to authenticate to Azure Resource Manager (ARM) API.

In order to make it a bit more interesting, this article uses the "Microsoft Azure Germany" environment, instead of the 'regular' Azure.

Registering Azure Germany under the hood

When you install the latest Powershell for Azure (v1.5.0 at time of this writing), the command Get-AzureEnvironment | select Name should look like this:

The last line AzureGermanCloud indicates that Powershell already knows the specific management endpoints for Germany.

If you do not have that, you might consider re-installing the Powershell module

For the azure-cli side of things, the output of azure account env list should look like this:

If you miss that last line, you can add the environment yourself:

Setup of a Service Principal in Azure Active Directory (AAD)

The following Powershell script can be used to

  1. Login interactively to Azure

  2. Create a new application in Azure Active Directory. An application is a process which is cryptographically known to Azure AD (AAD).

  3. Promote that application to become a service principal, i.e. giving it the right to request authN tokens from AAD.

  4. Registering that new service principal as a Contributor to my Azure Subscription.

Loggin in interactively

A few variables to start with

The initial log-in to Azure Germany happens with a regular Azure AD user, in my case that's [email protected].

Get the user's interactive password into the Powershell environment

Login to Azure with the interactive credential

Register the application

In order to authenticate to Azure later, I want my service principal to use an X.509 Certificate. You can just bake yourself an own one using makecert.exe if you like. In my case, I saved a copy of the actual certificate on my local harddisk, which I then read into Powershell:

Create the Azure AD application

Each application must have a name and a URL. In case your application is an actual web application, that URL would correspond to the real web site address. In my case, that's just some non-existent dummy URL:

Promote the app to become a service principal

As part of a larger script, you should pause execution for a few seconds, as it might take 1-2 seconds for that service principal information to propagate through AAD.

Tell Azure that the service principal can manage my subscription

Use that service principal to log-in to Azure

Use that service principal to log-in to Azure using Powershell

The following code assumes that you imported the certificate into your Windows Certificate store. As you can see, the CurrentUser\My certificate store contains the X509 cert, and I also own the private key:

Output is

With this information I can now login with the service principal's identity:

Output is

Use that service principal to log-in to Azure using node.js / azure-cli

The same thing can be done using the azure-cli. The main difference is that the azure-cli isn't aware of Windows certificate stores, but still requires access to the certificate's private key. In this case, the private key is in a PEM-file on my laptop's harddisk:

Output is

Add mgmt cert to Azure Germany via ASM API

Update Azure CLI 2.0 (Python) and change cloud

Use fiddler or mitm

Signin via service principal and debug a session

See the latest accessToken

Set Windows Proxy information

Last updated