无法通过Az模块或AzCLI设置未认证的ClientAction。

问题描述 投票:0回答:1

我设计了一个PowerShell脚本,它能够正确地配置一个系统的各种设置。功能应用 (包括CORS等).Function App可以工作,并且可以从Api Manegement服务中调用。

当需要配置 Azure AD,我已经用 AzCLI 复制 恰恰 我用 门户网站用户界面 (而我手动设置的东西完美地工作).但它停止工作,APIM返回了 HTTP状态码401 (未经授权)。

脚本中配置Azure AD的部分如下。

# $add is a simple class that contains value to be configured
# actually AllowedTokens is always empty
if ($aad) {
    'Setting Function App AAD configuration.' | Write-Verbose
    $allowedTokens = if ($aad.AllowedTokens) { "--aad-allowed-token-audiences $($aad.AllowedTokens -join ' ')"  } else { '' }
    "az webapp auth update --name $name --resource-group $group --enabled $($aad.Enabled.ToString().ToLower())" +
        " --action LoginWithAzureActiveDirectory --aad-client-id $($aad.ClientId) --aad-client-secret $($aad.ClientSecret)" +
        " --token-store true" +
        " --aad-token-issuer-url $($aad.TokenIssuerUrl) $allowedTokens" |
        Invoke-Expression
    'Function App AAD configuration set.' | Write-Verbose
}

第一件奇怪的事是 如果我禁用认证授权的话

enter image description here

我保存设置,启用并再次保存,一切开始工作。

所以我再次启动并启动脚本。我检查了资源。

az auth showunauthenticatedClientAction 设置为 RedirectToLoginpage.

az resource showunauthenticatedClientAction 设置为 null.

当我按上面的方法做的时候

az auth showunauthenticatedClientAction 设置为 AllowAnonymous.

az resource showunauthenticatedClientAction 设置为 null.

因此,我认为这是重要的区别,使功能应用程序正常工作(或者更好的是这是正确配置它的方法)。

因为我已经成功地使用这种方法在其他设置,我已经尝试设置这个属性与AzCLI。

az resource update --name web --resource-group $group --namespace Microsoft.Web --resource-type config `
    --parent "sites/$funcName" --set properties.siteAuthSettings.unauthenticatedClientAction=AllowAnonymous

作为响应返回的JSON显示没有任何改变。检查资源确认了这一点。

还有一件事,当我导出资源组时,我看不到任何的 unauthenticatedClientAction 在任何一个Function App模板中。

正确的设置方式是什么?unauthenticatedClientActionAllowAnonymous?

任何帮助将是非常感激!

azure powershell azure-active-directory
1个回答
1
投票

首先,回答你的问题,要设置 unauthenticatedClientActionAllowAnonymous,只需使用

az webapp auth update --name <functionapp-name> --resource-group <group-name> --action AllowAnonymous

enter image description here


其实,当你在门户网站上做了这一招,就会改变。

enter image description here

enter image description here

这就是 unauthenticatedClientAction 的设置。

那么问题来了,我不知道你到底要不要用Azure AD来保障你的azure功能。因为当你设置 unauthenticatedClientActionAllowAnonymous,你的函数将允许匿名请求,Azure AD auth将不会生效。

当需要配置Azure AD时,我已经使用AzCLI完全复制了我使用Portal UI所做的事情(我手动设置的东西完全有效)。但是它停止工作了,APIM返回HTTP状态码401(未授权)。

是的,401错误代表Azure AD auth toke效果,如果你启用了Azure AD auth(set Log in with Azure Active Directory),你需要为你的客户用户获取访问令牌,然后使用该令牌来访问该函数。(不知道如何在APIM中实现,我不是APIM专家:-))

更多的细节,你可以看看这个 博客 而这 岗位.

© www.soinside.com 2019 - 2024. All rights reserved.