【发布时间】: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