問題描述
我創建了一個新的函數應用,為它啟用了應用服務身份驗證/授權(使用身份驗證/授權來保護您的應用程序并使用每個用戶的數據")并禁用非身份驗證請求.
I have created a new Function App, enabled App Service Authentication / Authorization for it ("Use Authentication / Authorization to protect your application and work with per-user data") and disabled non-authenticated requests.
到目前為止,一切似乎都運行正常.如果我嘗試請求我的 HttpTrigger
ed 函數,它需要我先登錄;登錄后,所有請求都會按原樣處理.所以保護您的應用程序"部分沒有問題.
Everything seems to be working correctly so far. If I try to request my HttpTrigger
ed function, it requires me to log in first; once I'm logged in, all requests are processed as they should be. So there is no problem with "protect your application" part.
但是,我完全被處理每個用戶的數據"部分所困擾.我的 Azure 函數被調用為
However, I'm totally stuck with the "work with per-user data" part. My Azure Function is invoked as
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
HttpRequestMessage
中沒有與身份驗證相關的內容.(AuthorizationLevel.Anonymous 似乎控制著完全不同的事情——即,函數是否可以由任何人調用,或者只能由具有固定 API 密鑰的人調用).
And there is nothing related to authentication in HttpRequestMessage
. (AuthorizationLevel.Anonymous seems to control the entirely different thing - namely, if the function could be called by anyone or only by those who have a fixed API key).
如何獲取調用該函數的經過身份驗證的用戶的身份?
How do I get the identity of authenticated user who called the function?
推薦答案
使用 Azure Function runtime v2.0.12309,你可以檢索 來自 Run
方法中注入的 "noreferrer">ClaimsPrincipal 實例:
Using the Azure Function runtime v2.0.12309, you can retrieve the authenticated user information from the ClaimsPrincipal instance injected in the Run
method:
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]
HttpRequest httpRequest,
ILogger logger,
ClaimsPrincipal claimsPrincipal)
{
// Explores the authenticated user's claims in claimsPrincipal.
}
這篇關于如何使用 Azure 身份驗證在 Azure Function 中獲取當前用戶身份?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!