获取调用 Azure 函数时使用的函数键(名称)

Tar*_*tar 3 azure azure-functions

我需要能够识别标头(x-functions-key)中提供的密钥(理想情况下是密钥名称),以便在 Run 方法中向 Azure 函数发送 POST,例如

Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ClaimsPrincipal principal)
Run Code Online (Sandbox Code Playgroud)

能够保护对 Azure 功能的访问,在 Azure 门户面板中添加功能键是很棒的,但我必须能够分辨使用了哪个功能键。理想情况下,可以在每个功能键上关联声明,但只要我至少能弄清楚使用了哪个键,我就会很高兴。

小智 5

只需从 req.HttpContext.User.Claims 对象中获取声明“ http://schemas.microsoft.com/2017/07/functions/claims/keyid ”。它包含密钥 ID,以防使用功能键。

像魅力一样工作,不需要外部查找。

const string KEY_CLAIM = "http://schemas.microsoft.com/2017/07/functions/claims/keyid";
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    var claim = req.HttpContext.User.Claims.FirstOrDefault(c => c.Type == KEY_CLAIM);
    if (claim == null) 
    {
        log.LogError("Something went SUPER wrong");
        throw new UnauthorizedAccessException();
    }
    else
    {
        log.LogInformation( "Processing call from {callSource}", claim.Value);
    }
Run Code Online (Sandbox Code Playgroud)