Get ClaimsPrincipal/Identity from AzureAD secured powershell core Azure Function

Viewed 673

I have a Azure Function developed using Powershell Core 6 and secured by enabling the Azure AD Authentication. If an anonymous request is made by doing a GET through a browser, the user is redirected to Azure AD login first and then redirected to my Azure Function. Authentication Configuration

Now, in the Powershell function, I need to know which has user logged in which is usually found in the ClaimsPrincipal as indicated here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp

But the httpRequest.Context.User is null when I access it through powershell. Below is the code in my Function:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

$body = @{
    Context = $Request.Context;
    HttpContext = $Request.HTTP.Context;
    Request = $Request;
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

It basically returns everything in an attempt to find the User's Identity

Where can I find the User's Identity?

I don;t want to use these headers because they can easily be manipulated to still make the API work.

$Request.Headers['X-MS-CLIENT-PRINCIPAL-NAME']
$Request.Headers['X-MS-CLIENT-PRINCIPAL-ID']
1 Answers

The data you are looking for should be under $Request.Headers, for example:

$Request.Headers['X-MS-CLIENT-PRINCIPAL-NAME']
$Request.Headers['X-MS-CLIENT-PRINCIPAL-ID']

The $Request object structure in PowerShell Functions is different from C# Functions. Specifically, it does not have neither Context nor HTTP property, this is why you are getting nulls. In order to see the entire content, trace something like:

$Request | ConvertTo-Json
Related