azure - 调用经过用户身份验证的 http 触发器时发生错误

标签 azure authentication jwt azure-functions

我正在使用 Azure Functions v3

我正在尝试使用身份验证,并且我已将函数的 HttpTriggers 设置为用户级安全性

下面的逻辑在我的函数启动时被调用

protected override void SetupAuthentication(
    IServiceCollection services, IConfiguration configuration)
{
    var tokenOptions = configuration.GetSection("JwtIssuerOptions")
                    .Get<TokenConfiguration>();
                
    var tokenValidationParameters = new TokenValidationParameters
        {
            // The signing key must match!
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = tokenOptions.SecurityKey,
            // Validate the JWT Issuer (iss) claim
            ValidateIssuer = true,
            ValidIssuer = tokenOptions.Issuer,
            // Validate the JWT Audience (aud) claim
            ValidateAudience = true,
            ValidAudience = tokenOptions.Audience,
            // Validate the token expiry
            ValidateLifetime = true,
            // If you want to allow a certain amount of clock drift, set that here:
            ClockSkew = TimeSpan.Zero
        };
                
    services.Configure<IdentityConfiguration>(configuration.GetSection("IdentityConfiguration"));
    services.AddScoped<CustomJwtBearerEvents>();
    
    services
        .AddAuthentication(o =>
         {
              o.DefaultForbidScheme = JwtBearerDefaults.AuthenticationScheme;
              o.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
         })
        .AddJwtBearer(options =>
         {
              options.TokenValidationParameters = tokenValidationParameters;
              options.EventsType = typeof(CustomJwtBearerEvents);
         });
    
    }

当我从外部调用该函数时,出现错误

No authentication handler is registered for the scheme 'WebJobsAuthLevel'. 

注册的方案是:Bearer。您是否忘记调用 AddAuthentication().AddSomeAuthHandler?。

我错过了什么?

我需要模仿与网络应用程序相同的约定

[FunctionName("GetPayments")]
public async Task<List<PaymentDto>> GetPaymentsAsync(
    [HttpTrigger(AuthorizationLevel.User, "post", Route = "payments/get-payments")]
     HttpRequest req, 
     ILogger log)
{
    var data = await req.ReadAsStringAsync();
            
    //THis is where I have my logic which I only want to be able to access if the user has permissions
}

我看过下面的链接

https://damienbod.com/2020/09/24/securing-azure-functions-using-azure-ad-jwt-bearer-token-authentication-for-user-access-tokens/comment-page-1/?unapproved=127819&moderation-hash=3fdd04b596812933c4c32e8e8c8cf26a#comment-127819

它最初看起来是我需要的,但我无法弄清楚如何调整它,以便它只使用身份 token 验证端

如有任何帮助,我们将不胜感激

保罗

最佳答案

看看:https://www.nuget.org/packages/DarkLoop.Azure.Functions.Authorize

最新版本可确保在您添加自己的身份验证或扩展内置身份验证之前,所有内置身份验证均已到位。顺便说一句,JTW Bearer 已由 Functions 运行时配置。

您需要做的就是调用您的方法

services.AddFunctionsAuthentication();
services.AddFunctionsAuthorization();

然后在 AddFunctionsAuthentication() 调用之后链接您需要配置的任何其他方案。生成的身份验证构建器旨在处理对 jwt 承载的修改 (AddJwtBearer(options => ...),并且不会中断告诉您 Bearer 方案已存在。

此包还使您能够使用 FunctionsAuthorize 属性来处理 HTTP 函数的精细授权要求。这是一篇包含详细信息的博客文章:https://blog.darkloop.com/post/functionauthorize-for-azure-functions-v3

关于azure - 调用经过用户身份验证的 http 触发器时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68246605/

相关文章:

java - 如何使用 Java SDK 从远程 Azure 应用程序配置检索服务总线队列的队列名称?

go - 如何在未经验证的情况下从JWT token 获取声明

javascript - 在 http 响应中与 html 文档一起发送 JWT

internet-explorer - 是否可以在 Internet Explorer 中启用 HTTP 基本身份验证?

security - 使用docker swarm转发JWT token 吗?

azure - APIM 策略中的泛型破坏了 Terraform

azure - 如何更改azure web应用服务的主机名

c# - 如何检查设备是否已加入 AD 或 Azure AD 加入/注册?

facebook - 自最近引入登录审核以来,我该如何测试 Facebook 应用程序?

ruby-on-rails - PostgreSQL 8.4的rake db :create fails,身份验证问题