簡體   English   中英

授予 Azure 應用服務訪問 Azure Function 的權限

[英]Grant permission for Azure App Service to Access an Azure Function

I have published an Azure Function to Azure, I need to allow my WebApps to access it but not the public web. 當用戶未登錄時,我已啟用“使用 Azure Active Directory 登錄”:

天藍色的屏幕截圖

如果我導航到我的 function 的 URL,我可以看到它在登錄后成功執行:

在此處輸入圖像描述

接下來,我在 Azure Active Directory App Registrations 中注冊了 function。 現在我想允許多個 WebApp 訪問這個 function 但我不知道如何讓它工作。 我去了 function 的應用程序注冊並添加了“授權客戶端應用程序”:

在此處輸入圖像描述

但是,當我的客戶端 WebApp 在 function 上執行 http 時,我收到 401 錯誤,“您無權查看此目錄或頁面。”

您無權查看此目錄或頁面。

您沒有顯示代碼,我假設您在沒有廣告令牌的代碼中發送請求。

你需要這樣做:

import requests
from azure.identity import ClientSecretCredential 

client_id = 'xxxxxx'
tenant_id = 'xxxxxx'
client_secret = 'xxxxxx'


credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
accesstoken = str(credential.get_token('https://yourfunctionappname.azurewebsites.net/.default'))[19:1269]
bearertoken = "Bearer "+accesstoken

r = requests.post("https://yourfunctionappname.azurewebsites.net/api/HttpTrigger1",headers={'Authorization': bearertoken})

上面的代碼在我這邊工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM