azure - 从 AzureAD 安全的 powershell 核心 Azure Function 获取 ClaimsPrincipal/Identity

标签 azure powershell azure-active-directory azure-functions powershell-core

我有一个使用 Powershell Core 6 开发的 Azure Function,并通过启用 Azure AD 身份验证进行保护。如果通过浏览器执行 GET 来发出匿名请求,用户将首先重定向到 Azure AD 登录,然后重定向到我的 Azure Function。 Authentication Configuration

现在,在 Powershell 函数中,我需要知道哪个用户已登录,哪个用户通常可以在 ClaimsPrincipal 中找到,如下所示: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp

但是当我通过 powershell 访问它时,httpRequest.Context.User 为空。下面是我的函数中的代码:

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
})

它基本上会返回所有内容以尝试查找用户身份

在哪里可以找到用户的身份?

我不想使用这些 header ,因为可以轻松操纵它们以使 API 正常工作。

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

最佳答案

您要查找的数据应位于 $Request.Headers 下,例如:

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

PowerShell 函数中的 $Request 对象结构与 C# 函数不同。具体来说,它既没有 Context 也没有 HTTP 属性,这就是您得到空值的原因。为了查看全部内容,请跟踪如下内容:

$Request | ConvertTo-Json

关于azure - 从 AzureAD 安全的 powershell 核心 Azure Function 获取 ClaimsPrincipal/Identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62814166/

相关文章:

javascript - 错误: AADSTS500011: The resource principal named "URL" was not found in the tenant

azure - 如何在广告 B2C 自定义政策中添加类型 "StringCollection"的声明

azure - Azure 管理证书的用途

azure - .Net Core 中 ServiceBus MessagingFactory 的替代品是什么?

powershell - 在 cmd 和 INFO : Could not find files for the given pattern(s) 下执行 powershell 命令

azure-active-directory - 用户在 azure aks 中使用 azure Active Directory 进行身份验证后,重定向 URL 从 https 更改为 http

azure - 如何使用 terraform 将共享卷安装到 Azure 容器应用程序实例上

powershell - 如何从另一个模块中包含的被调用函数调用 PowerShell 模块范围来访问变量?

powershell - Powershell-每天分配一个变量

uwp - 对 Microsoft Store API 的请求始终返回空的产品/订阅列表