c# - 本地 Azure 函数 ClaimsPrincipal

标签 c# azure-functions

我正在使用 Azure AD 身份验证的 Azure 函数。在 Azure 上部署时一切正常。

    public static async Task<IActionResult> ExportT(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ClaimsPrincipal principal,
        ILogger log)
    {
        var c = principal.Claims.Select(x => x.Value);

        return new OkObjectResult(c);

    }

返回 :
[ "38a1e83b-c1d3-4fd7-bdc6-ef2447", "https://sts.windows.net/635e38ef-108e-4a26-b718-bbd532/", "1564501257", "1564501257", "1564505157", "42FgYFCuNWnL3ROWG9x3p4EzN3+c/6u8Gh5jV7sT0A", "pwd", "test", "test", "92.169.93", "test", "36fbab-425b-9d65-b2425ef3d9bf", "a1c1ee35-67ab-4f3a-2877c5580b1e", "ES_SALARIED", "XboeusxsxyvnjhCT_vJHkzncPE2JBU58Q50", "635e38ef-26-b718-bbd960991532", "testdedev@ins.coop", "testdedev@ins.coop", "PJ0vEo70o0G__HrwX8ghAA", "1.0" ]

但是当使用 VS2019 在本地执行时,我得到:
[ "Admin" ]

任何的想法 ?

问候,

最佳答案

我能找到的最接近它的方法是使用来自这个 repo 的这些示例。 ( commit at the time of writing )。

在示例 AuthenticationService 中,我添加了 context.User = claimsPrincipal;确认 claim 后。

从那里,我可以对我的 HTTP 触发器执行以下操作:

[Authorize]
[FunctionName("AuthTest")]
public async Task<IActionResult> AuthTest(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "test")] HttpRequest req)
{
    // Current user identity returned
    // (quickly thrown together as you get a circular reference error when converting to JSON):
    return new OkObjectResult(
       req.HttpContext.User.Identities.Select(x =>
          new {
                 Claims = x.Claims.Select(y => new
                    {
                        y.Type,
                        y.Value,
                        y.Issuer,
                        y.Properties
                    }),
                 x.Name,
                 x.Actor,
                 x.AuthenticationType,
                 x.NameClaimType
                })
            );
 }

寒来袭?

如果您有访问 token ,则可以执行 GET: http://localhost:7071/api/test在您的 header 中带有 JWT token 。

例如:

enter image description here

你还需要
  • Register an app
    使用 Azure AD
  • 使用 MSAL client
    在您的前端获取 token

  • 注意事项

    这不是使用“简单的身份验证”。我选择了这种方法,因为我想使用 MSAL.NET支持 AAD V2。我发现尚不支持 AAD V2:

    At this time, AAD V2 (including MSAL) is not supported for Azure App Services and Azure Functions. Please check back for updates. Ref



    走到这一步,我经历了一场真正的战斗。如果您需要在推送到生产环境之前测试您的应用程序,那么 Easy auth 似乎不是一个选项。即使在这个答案之后,我也不知道如何使授权像 normal Web API policy 一样简单方法。

    关于c# - 本地 Azure 函数 ClaimsPrincipal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275737/

    相关文章:

    c# - foreach 神秘地卡在 ResourceSet 的第一项上

    azure - 在 Visual Studio 中调试 Azure Function 时创建的日志文件在哪里

    c# - 从 Azure 函数针对 FTP 服务器(主动模式)运行 C# FTP 客户端 - "500 Syntax error, command unrecognized"

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

    azure - 当 azure 函数从队列中拉出消息后抛出异常时会发生什么?

    azure - 在 FunctionsStartup 中使用 ConfigurationBuilder

    c# - 从 Dotnet Core 调用 WCF 服务

    c# - CSS 类应用于没有该类名的元素,有什么想法吗?

    c# - 如何在 ASP.NET Core 2.2 中使用 IValidateOptions 验证配置设置?

    c# - 确保封装的建议