Get Azure Function keys from an Azure Function at deployment time?

后端 未结 5 1264
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 14:41

I\'m sending an email in Azure Functions using the SendGrid bindings. As part of the contents of that email, I\'d like to include a link to one of the HTTP methods in the Azure

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-06 15:18

    try the following two steps:

    1. get the host master key:

      GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourcegroupName}/providers/Microsoft.Web/sites/{functionApp}/functions/admin/masterkey?api-version=2016-08-01
      
    2. Get the function keys:

      GET https://{functionApp}.azurewebsites.net/admin/functions/{functionName}/keys?code={masterKeyFromStep1}
      

    response from the step 2:

        {
          "keys": [
            {
              "name": "default",
              "value": "xxxxxxxxxxxxxxxxxxxxxx"
            }
          ],
          "links": [
            {
              "rel": "self",
              "href": "https://myFncApp.azurewebsites.net/admin/functions/myFunction/keys"
            }
          ]
     }
    

    Update:

    Note, that the step 1 requires an authorization header in the format:

    Authorization: Bearer bearerToken
    

    where a bearerToken string can be obtained from Azure Active Directory (AAD), see the following code snippet of the example:

        private string AccessToken(string clientID)
        {
            string redirectUri = "https://login.live.com/oauth20_desktop.srf";
            authContext = new AuthenticationContext("https://login.windows.net/common/oauth2/authorize", TokenCache.DefaultShared);
            var ar = authContext.AcquireTokenAsync("https://management.azure.com/", clientID, new Uri(redirectUri), new PlatformParameters(PromptBehavior.SelectAccount)).Result;
            return ar.AccessToken;
        }
    

    Note, that the clientID is the quid of your registered application in the AAD with an API access permission for Windows Azure Service Management API.

提交回复
热议问题