Azure Active Directory App Only Authentication with OfficeDev PnP PowerShell

(Notice: this is the second post in a series of 2. See http://www.erwinmcm.com/azure-active-directory-authentication-with-officedev-pnp-powershell for the first post).

If you read the first post and managed to connect succesfully to SharePoint Online with your Azure AD application client ID, you have noticed that you are prompted for a username and password. While that might be okay for incidental use, if you want to automate and/or schedule a PowerShell script this is obviously not a solution.

In this post I will go through the steps to setup a Azure AD application that will allow you to authenticate with a certificate.

Setup the app in your Azure AD

  • Navigate to https://manage.windowsazure.com and open your Azure AD.
  • Select the “Applications” tab and click “Add” in the bottom of the page.
  • Select “Add an application my organization is developing.
  • Pick a name and select “WEB APPLICATION AND/OR WEB API”:
    apponly1
  • Navigate to step 2 of the wizard and enter a URI in both fields. This can be any URI, it doesn’t have to point to an existing site:

    apponly2

  • Confirm and after a few seconds you will see a confirmation screen.
  • Navigate to the “Configuration” tab:
    apponly3
  • Click the big green “Add Application” button.
  • Select “Office 365 SharePoint Online” and confirm your selection.
  • From the application permissions drop down, pick the appropriate rights and click “SAVE” in the bottom of the page.
    apponly4
  • From the toolbar in the bottom, select “MANAGE MANIFEST” and “Download Manifest”:
    apponly5

Creating the certificate

We are now going to create a self signed certificate. Obviously you can also use a commercial certificate if you want. You might be tempted to use the New-SelfSignedCertificate cmdlet that is available since Windows 8 and Windows Server 2012 but I found that it does not create a certificate that is suitable for our scenario. So we have to fall back to makecert.exe.

Check out the following PowerShell Script to create a self signed certificate and export the CER and PFX files:

The script will generate a certicate that is valid from the day you run the script and is valid for 2 years. Take notice of the location of both the PFX and CER files.

Modifying the manifest file

  • Locate the downloaded manifest file (usually named <guid>.json) on your local machine and edit the file with your prefered text editor
  • Now switch back to PowerShell with the OfficeDev PnP PowerShell Cmdlets installed and enter:
  • This will copy a string which is alike the following to your clipboard:

    I shortened the value property, in the actual version on your clipboard it is a much larger value.
  • In the manifest file that you opened earlier, find the line that starts with “keyCredentials” and replace the full line, including the beginning “keyCredentials”, with the contents of your clipboard.
  • Save the file, and upload it using the “Upload Manifest” menu option in “MANAGE MANIFEST”:
    apponly5

Connecting with the cmdlets

On the Azure Application configuration page, locate the CLIENT ID value and copy it to your clipboard:

apponly8

  • Open PowerShell, and enter:

    So in the case of the configuration above, the command would look like:
  • Press enter. PowerShell will ask you to enter the password to the PFX file.

If all went okay, will you have a valid ClientContext and you can perform tasks with the cmdlets and the client context that fit within the permissions granted to the application. Try for instance Get-SPOList to retrieve all lists in the site.

But… I still have to enter a password?

Yes, but there is a solution to that.

  • Navigate to your Credential Manager in your Windows Control Panel.
  • Click “Windows Credentials”
  • Click “Add a Generic Credential”:
    apponly6
  • Pick an easy to remember name and enter the information for instance like:
    apponly7
    The username is not important, but in the password field you should enter the password you picked when exporting the certificates.
  • Click OK to store the entry.
  • Now you can connect with Connect-SPOnline as shown in the following script:

    So in the case of the configuration above, the command would look like:
  • Notice that with a ‘normal’ use of Connect-SPOnline you can also make use of the credential manager. No need to retrieve a credential first, just enter the name of the Credential you created in the credential manager as a value for the -Credentials parameter. In that scenario you will of course have to specify a valid username too.

Concluding

Is this easy to set up? No, I don’t think so, but it enables scenarios where an Azure AD admin defines what kind of access you get with a certificate. That same admin can then that access at will, without the developer using the certificate having access to the AD itself. That as such can be a valid use case.

You may also like...