甚至不需要控制器时,.net核心AuthenticationHandler也会每次调用

时间:2018-10-03 06:03:26

标签: c# .net asp.net-core core

我添加了自定义安全性,它可以很好地进行身份验证。我不知道为什么Task<AuthenticateResult> HandleAuthenticateAsync()要求不需要授权的控制器。

在下面的Startup.cs中是我如何初始化它的代码。

public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors();
        services.AddMvc();

        services.AddAutoMapper();

c.AddSecurityDefinition(MM.AuthenticationScheme, new ApiKeyScheme()
            {
                Description = "API Key Authorization header using the mm Api Key scheme. Example: \"MM-API-KEY: {MM-API-KEY}\"",
                Name = "MM-API-KEY",
                In = "header",
                Type = "apiKey"
            });
auth.AddPolicy(MM.AuthenticationScheme, b =>
            {
                b.RequireAuthenticatedUser();
                b.AuthenticationSchemes = new List<string> { MenulogApiKey.AuthenticationScheme };
            });

services.AddAuthentication(options =>
        {
            // the scheme name has to match the value we're going to use in                 options.DefaultAuthenticateScheme = MM.AuthenticationScheme;
            options.DefaultChallengeScheme = MM.AuthenticationScheme;
        })
            .AddMenulogApiKeyAuth(o => { });


    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
         app.UseAuthentication();
         }

在KeyHandler类中,我这样使用:

internal class MMApiKeyHandler : AuthenticationHandler<MMApiKeyAuthOptions>

1 个答案:

答案 0 :(得分:0)

(盲目猜测)

可以将身份验证过滤器设置为全局过滤器。

搜索类似filters.Add(

或检查使用GlobalFilters.Filters的代码。