【问题标题】:How to use JWT Validation within Azure Function Merthod?如何在 Azure 函数方法中使用 JWT 验证?
【发布时间】:2021-09-15 13:51:02
【问题描述】:

我从 API 移植了一些逻辑,我需要能够在 Azure 函数中使用这些逻辑

我需要对 JWT 令牌进行验证

我的函数将具有用户必须具有的特定角色才能从函数中获得响应

我在我的函数启动中得到了这个

        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);
            });

在 API 上下文中,我可以通过中间件包和 [Authorize(Roles="role1")] 来处理这个问题

但是,Azure 函数似乎不支持该功能

如何在 Azure 中实现相同的目标?

我有一个请求,上面已经有一个可供检查的令牌

保罗

【问题讨论】:

    标签: c# azure authentication jwt


    【解决方案1】:

    【讨论】:

    • 非常感谢我让它与函数属性一起工作
    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 2020-07-08
    • 2016-12-02
    • 2017-09-25
    • 2021-08-14
    • 2020-10-29
    • 2017-03-31
    • 2021-11-16
    相关资源
    最近更新 更多