【问题标题】:Azure Functions v2 & Google AuthAzure Functions v2 和 Google 身份验证
【发布时间】:2018-11-18 19:37:32
【问题描述】:

我已经按照本文https://blogs.msdn.microsoft.com/stuartleeks/2018/02/19/azure-functions-and-app-service-authentication/中描述的方式设置了我的 Azure Functions 2

我可以使用 Google 令牌调用 /.auth/login/google 端点并取回简易身份验证令牌。

我可以调用 /.auth/me 端点并取回我的个人资料信息。

但在我的代码中,Thread.CurrentPrincipal.Identity 为空。我似乎找不到任何解决方案。我已验证我的请求已将 x-zumo-auth 标头设置为从登录端点检索到的简单身份验证令牌。当然,函数设置为匿名。

我希望我错过了一些简单的东西。感谢您的帮助。

【问题讨论】:

    标签: azure azure-functions


    【解决方案1】:

    自 2018 年 11 月 28 日起,此功能现在处于预览状态。此功能仅适用于 Functions 2.x 运行时。它目前也仅适用于 .NET 语言。

    ClaimsPrincipal 可作为请求上下文的一部分使用 如下例所示:

    using System.Net; 
    using Microsoft.AspNetCore.Mvc; 
    using System.Security.Claims;
    
    public static IActionResult Run(HttpRequest req, ILogger log)
    {
        ClaimsPrincipal identities = req.HttpContext.User;
        // ...
        return new OkResult();
    }
    

    或者,ClaimsPrincipal 可以简单地作为 函数签名中的附加参数:

    using System.Net;
    using Microsoft.AspNetCore.Mvc;
    using System.Security.Claims;
    using Newtonsoft.Json.Linq;
    
    public static void Run(JObject input, ClaimsPrincipal principal, ILogger log)
    {
        // ...
        return;
    }
    

    参考:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook#working-with-client-identities

    【讨论】:

      【解决方案2】:

      找到了答案,但我仍然对如此缺乏清晰度感到惊讶。

      https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-overview

      对于 Azure Functions,ClaimsPrincipal.Current 不适合 .NET 代码,但您仍然可以在请求标头中找到用户声明。

      但是我看到了无数的例子,人们在 Azure Functions 中引用它,好像它应该是水合的。

      我确实在标题中找到了预期的声明并将处理它,但我觉得我仍然缺少一些东西......

      【讨论】:

      • 您正在查看的示例可能来自设置了 ClaimsPrincipal.Current 的 Azure Functions 1.x。这很恶心,因为线程局部变量不能很好地处理异步。在 Azure Functions 2.x 运行时,如果要访问 IPrincipal 而不是原始标头,可以使用 request.HttpContext.User。应该与您在 Functions 1.x 中的 ClaimsPrincipal.Current 中找到的对象相同。更多细节在这里:stackoverflow.com/questions/51730988/…
      猜你喜欢
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 2018-07-09
      • 2018-07-23
      • 2015-02-02
      • 2018-02-24
      • 2016-01-15
      • 1970-01-01
      相关资源
      最近更新 更多