azure - 调用经过用户身份验证的 http 触发器时发生错误
问题描述
我正在使用 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?
我错过了什么?
我需要模仿与 Web 应用程序相同的约定
[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 ://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
推荐阅读
- node.js - Node.js 错误链接未传播到调用函数
- jenkins - Jenkins 启动时安装插件失败
- python - Python selenium循环打开页面
- java - 如何从内部停止线程?
- node.js - 如何通知用户 NPM 包版本更新?
- ruby-on-rails - 为什么工头完成后ruby进程继续存在?
- oauth-2.0 - Freshdesk OAuth SSO:Freshdesk 登录页面不 Ping 我的身份验证页面?
- mysql - 如何构建内部查询以查找趋势前 3
- c++ - 如何将 printf 输出转换为 char 数组?
- clickhouse - 无法启动 ClickHouse。退出代码 203