首页 > 解决方案 > Azure Function V2 JWT - AD 身份验证

问题描述

我正在尝试对 Azure Functions v2 进行身份验证。我得到以下错误 Microsoft.AspNetCore.Authentication.Core: No authentication handler is registered for the scheme 'WebJobsAuthLevel'. The registered schemes are: Bearer. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("WebJobsAuthLevel",...)?.

下面是我在 Startup.cs 中使用的代码

    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddAuthentication()
                    .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme,o =>
                    {
                        o.Audience = "https://*******************.azurewebsites.net/";
                        o.Authority = "http://localhost:****";
                        o.RequireHttpsMetadata = false;
                        o.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                        {
                            RequireSignedTokens = true,
                            ValidAudience = "https://***************.azurewebsites.net/",
                            ValidateAudience = true,
                            ValidIssuer = "https://sts.windows.net/***************-5********2**/",
                            ValidateIssuer = true,
                            ValidateIssuerSigningKey = true,
                            ValidateLifetime = true
                        };
                    });
        }

    }

更改了代码,但仍然得到相同的给定错误。我错过了哪一个?

标签: azurejwtazure-functionsazure-authentication

解决方案


考虑使用Azure 应用服务身份验证/授权功能(也非正式地称为 EasyAuth)。如果您遵循 Azure 门户中的快速流程,它将为您的函数应用程序创建一个 AAD V1 应用程序注册,并自动配置您的应用程序以允许使用 AAD 进行身份验证。如果您随后设置Action to take when request is not authenticatedLogin with Azure Active Directory,则只有经过身份验证的请求才会被授权向您的应用程序发出任何请求。

EasyAuth 已内置支持接受承载令牌以进行 AAD 应用程序注册,以及 AAD 和我们其他支持的身份提供商(Facebook、Google、Twitter)的一些其他 OAuth2/OIDC 流程。通过此功能,您不需要添加任何代码,这一切都将由 Azure 平台处理。


推荐阅读