2016-03-07 21 views
8

Azure应用服务包括认证/授权设置刀片下的交钥匙认证解决方案。这使我可以为我的App Service web api配置Active Directory身份验证。我有一个配置脚本来设置我的环境,我想通过ARM模板或Powershell命令自动配置App Service身份验证。是否可以编写Azure App Service身份验证的配置脚本?

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

有谁知道如何自动化应用服务认证的配置,专门用于AD认证?

回答

6

我可以自己回答:这确实可以通过ARM模板编写脚本。 (我最初尝试使用resources.azure.com,但它没有显示我网站的所有配置信息;注销并再次返回使其行为。)解决方案是在Microsoft.Web/sites资源中为您的Web应用程序使用嵌套资源类型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命令去做。

首先,可以使用查看当前AUTH设置:

$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。

1

这是现在的merged into Azure CLI,并且在az webapp auth下可用。

Azure CLI Ref Page

az webapp auth update --name 
         --resource-group 
         [--aad-allowed-token-audiences] 
         [--aad-client-id] 
         [--aad-client-secret] 
         [--aad-token-issuer-url] 
         [--action {AllowAnonymous, LoginWithAzureActiveDirectory, LoginWithFacebook, LoginWithGoogle, LoginWithMicrosoftAccount, LoginWithTwitter}] 
         [--allowed-external-redirect-urls] 
         [--enabled {false, true}] 
         [--facebook-app-id] 
         [--facebook-app-secret] 
         [--facebook-oauth-scopes] 
         [--google-client-id] 
         [--google-client-secret] 
         [--google-oauth-scopes] 
         [--microsoft-account-client-id] 
         [--microsoft-account-client-secret] 
         [--microsoft-account-oauth-scopes] 
         [--runtime-version] 
         [--slot] 
         [--token-refresh-extension-hours] 
         [--token-store {false, true}] 
         [--twitter-consumer-key] 
         [--twitter-consumer-secret] 
相关问题