問(wèn)題描述
我創(chuàng)建了一個(gè)新的函數(shù)應(yīng)用,為它啟用了應(yīng)用服務(wù)身份驗(yàn)證/授權(quán)(使用身份驗(yàn)證/授權(quán)來(lái)保護(hù)您的應(yīng)用程序并使用每個(gè)用戶的數(shù)據(jù)")并禁用非身份驗(yàn)證請(qǐng)求.
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.
到目前為止,一切似乎都運(yùn)行正常.如果我嘗試請(qǐng)求我的 HttpTrigger
ed 函數(shù),它需要我先登錄;登錄后,所有請(qǐng)求都會(huì)按原樣處理.所以保護(hù)您的應(yīng)用程序"部分沒(méi)有問(wèn)題.
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.
但是,我完全被處理每個(gè)用戶的數(shù)據(jù)"部分所困擾.我的 Azure 函數(shù)被調(diào)用為
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
中沒(méi)有與身份驗(yàn)證相關(guān)的內(nèi)容.(AuthorizationLevel.Anonymous 似乎控制著完全不同的事情——即,函數(shù)是否可以由任何人調(diào)用,或者只能由具有固定 API 密鑰的人調(diào)用).
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).
如何獲取調(diào)用該函數(shù)的經(jīng)過(guò)身份驗(yàn)證的用戶的身份?
How do I get the identity of authenticated user who called the function?
推薦答案
使用 Azure Function runtime v2.0.12309,你可以檢索 來(lái)自 Run
方法中注入的 "noreferrer">ClaimsPrincipal 實(shí)例:
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.
}
這篇關(guān)于如何使用 Azure 身份驗(yàn)證在 Azure Function 中獲取當(dāng)前用戶身份?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!