【发布时间】:2021-09-15 16:08:13
【问题描述】:
我正在使用 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'.
注册的方案是:承载。您是否忘记调用 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
}
我看过下面的链接
它最初看起来是我需要的,但我不知道如何调整它以便它只使用身份令牌验证端
任何帮助将不胜感激
保罗
【问题讨论】:
-
嘿,保罗,你运气好吗?我对 IS4 有同样的问题
标签: azure authentication jwt azure-functions