【问题标题】:Is it possible to script the configuration of Azure App Service Authentication?是否可以编写 Azure 应用服务身份验证的配置脚本?
【发布时间】:2016-06-21 03:42:18
【问题描述】:

Azure 应用服务在身份验证/授权设置刀片下包含一个统包身份验证解决方案。这允许我为我的应用服务 Web api 配置 Active Directory 身份验证。我有一个用于设置我的环境的配置脚本,我想通过 ARM 模板或通过 Powershell 命令自动配置应用服务身份验证。

我尝试使用 resource.azure.com 查看我的网站设置,但我看不到与 AD 相关的配置。我尝试搜索执行此操作的 ARM 模板,但没有成功。我也看不到可以执行此操作的 Azure 资源管理器命令行开关。

有谁知道如何自动配置应用服务身份验证,特别是 AD 身份验证?

【问题讨论】:

    标签: powershell azure authentication azure-resource-manager


    【解决方案1】:

    我可以自己回答:这确实可以通过 ARM 模板编写脚本。 (我最初尝试使用resources.azure.com,但它没有显示我网站的所有配置信息;注销并重新登录使其正常运行。)解决方案是在Microsoft.Web/sites 资源中使用嵌套资源您的网络应用类型为config 和名称web 以指定设置,例如:

    {
       "type": "Microsoft.Web/sites",
       ...
       "resources": [
        {
          "apiVersion": "2015-04-01",
          "name": "web",
          "type": "config",
          "dependsOn": [
            "[resourceId('Microsoft.Web/sites', parameters('someName'))]"
          ],
          "properties": {
            "siteAuthEnabled": true,
            "siteAuthSettings": {
              "enabled": null,
              "httpApiPrefixPath": null,
              "unauthenticatedClientAction": null,
              "tokenStoreEnabled": null,
              "allowedExternalRedirectUrls": null,
              "defaultProvider": null,
              "clientId": "REMOVED",
              "clientSecret": null,
              "issuer": "https://sts.windows.net/REMOVED/",
              "allowedAudiences": null,
              "additionalLoginParams": null,
              "isAadAutoProvisioned": false,
              "aadClientId": "REMOVED",
              "openIdIssuer": "https://sts.windows.net/REMOVED/",
              "googleClientId": null,
              "googleClientSecret": null,
              "googleOAuthScopes": null,
              "facebookAppId": null,
              "facebookAppSecret": null,
              "facebookOAuthScopes": null,
              "twitterConsumerKey": null,
              "twitterConsumerSecret": null,
              "microsoftAccountClientId": null,
              "microsoftAccountClientSecret": null,
              "microsoftAccountOAuthScopes": null
            }
          }
        }
      ]
    }
    

    【讨论】:

    • 这对我有用,但我找不到协作文档。
    【解决方案2】:

    这是一种使用直接 Powershell 命令的方法。

    首先,您可以使用以下命令查看当前的身份验证设置:

    $rgName = "ResourceGroupName"
    $resourceType = "Microsoft.Web/sites/config"
    $resourceName = "service-name/authsettings"
    
    $resource = Invoke-AzureRmResourceAction -ResourceGroupName $rgName `
    -ResourceType $resourceType -ResourceName $resourcename -Action list `
    -ApiVersion 2015-08-01 -Force
    
    $resource.Properties
    

    然后,您可以获取这些属性的值并使用它们来设置 PropertyObject(下面显示的属性与 AAD 身份验证相关,使用服务主体):

    $PropertiesObject = @{
        "enabled" = "True";
        "unauthenticatedClientAction" = "0";
        "defaultProvider" = "0";
        "tokenStoreEnabled" = "True";
        "clientId" = "<your client ID here>";
        "issuer" = "https://sts.windows.net/<your AAD ID here>/";
        "allowedAudiences" = "{https://<service name>.azurewebsites.net}";
        "isAadAutoProvisioned" = "True";
        "aadClientId" = "<your client ID here>";
        "openIdIssuer" = "https://sts.windows.net/<your AAD ID here>/";
    }
    
    New-AzureRmResource -PropertyObject $PropertiesObject `
    -ResourceGroupName $rgName -ResourceType $resourceType `
    -ResourceName $resourcename -ApiVersion 2015-08-01 -Force
    

    我发现在门户中启用身份验证、查看属性、然后使用这些值设置 PropertyObject 更容易。

    【讨论】:

    • 这对我来说是很有前途的方法(作为 PS 中唯一的方法?)。但我收到“消息”:“发生错误。”。启用调试后 ($DebugPreference="Continue") 我看到来自 API management.azure.com 的 InternalServerError。
    • 我目前的解决方法是使用@PotatoFarmer 回答并使用 CLI,不幸的是......
    【解决方案3】:

    2020/06 年编辑:我发现这个工作的一个基本示例非常神秘。这是让 WebApp 使用 Azure AD 进行身份验证的详细方法


    参考:az ad app create / az ad app permission / az webapp auth update

    第一步:定义一些基本变量

    RSGROUP="MyResourceGroup"
    webappname="MyWebSite"
    
    WebAppFDQN=$(az webapp show --name "$webappname" -g "$RSGROUP" --query "defaultHostName" --out tsv);
    prodURL="https://myapp.customdomainblah.com";
    AADsuffix="/.auth/login/aad/callback" # AD Online is hardcoded to redirect to this path!!
    urls="https://${WebAppFDQN}${AADsuffix} ${prodURL}${AADsuffix}";
    
    AADappName="$webappname"
    

    第 2 步 - 创建 Azure Active Directory (AAD) 应用注册

    az ad app create \
      --display-name "$AADappName" \
      --homepage="https://${WebAppFDQN}" \
      --reply-urls $urls \
      --oauth2-allow-implicit-flow true
    

    第 3 步 - 添加 AD 应用权限

    似乎需要具有读取权限的 Microsoft Graph API。

    AADappId=$(az ad app list --display-name "$AADappName" --query [].appId -o tsv);
    MSGraphAPI="00000003-0000-0000-c000-000000000000" #UID of Microsoft Graph
    Permission="e1fe6dd8-ba31-4d61-89e7-88639da4683d=Scope" # ID: Read permission, Type: Scope
    
    az ad app permission add \
     --id "$AADappId" \
     --api "$MSGraphAPI" --api-permissions "$Permission"
    
    # Appears to be safe to ignore resulting warning: 
    #  "Invoking "az ad app permission grant --id $AADappId --api $MSGraphAPI" is needed to make the change effective"
    

    第 4 步 - 网络:启用身份验证

    表现出幂等性(在每次部署期间都可以安全执行)

    az webapp auth update \
      -g "$RSGROUP" -n "$webappname" --enabled true \
      --action LoginWithAzureActiveDirectory \
      --aad-client-id "$AADappId"
    

    上一个答案:

    现在是 merged into Azure CLI,可在 az webapp auth 下使用。

    {编辑:大部分无用的剪辑文档 - 可以在这里看到:az webapp auth}

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 2020-03-06
      • 2017-02-03
      • 2019-12-07
      • 1970-01-01
      相关资源
      最近更新 更多