OpenIdDict 3.0中的AllowAnonymous被跳过。

问题描述 投票:0回答:1

我正在使用OpenIdDict 3.0,它工作得很好,但是我有一个控制器使用AllowAnonymous属性,但我仍然收到 "请求被拒绝,因为缺少访问令牌"。我假设可能是我的Startup中的排序问题,使得它在管道中没有被击中,但我不确定。 这是我的启动类。

 public async void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
        {
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();


            app.UseMvcWithDefaultRoute();
        }


 public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options =>
            {
                options.EnableEndpointRouting = false;
            }).AddNewtonsoftJson();

            services.AddAutoMapper(typeof(Startup));

            services.AddDbContext<MyDbContext>(options =>
            {
                options.UseSqlServer(WebConfig.ConnectionString, x => x.MigrationsAssembly("X.Api.DataAccess"));
                options.UseLazyLoadingProxies();
                options.UseOpenIddict();
            });

            services.AddIdentity<ApplicationUser, IdentityRole>(options =>
            {
                options.Password.RequireLowercase = true;
            })
            .AddEntityFrameworkStores<MyDbContext>()
            .AddUserStore<ApplicationUserStore>()
            .AddDefaultTokenProviders();

            services.Configure<IdentityOptions>(options =>
            {
                options.ClaimsIdentity.UserNameClaimType = Claims.Name;
                options.ClaimsIdentity.UserIdClaimType = Claims.Subject;
                options.ClaimsIdentity.RoleClaimType = Claims.Role;
            });

            services.AddOpenIddict()
                .AddCore(options =>
                {
                    options.UseEntityFrameworkCore()
                    .UseDbContext<MyDbContext>();
                })
                .AddServer(options =>
                {
                    options.SetAuthorizationEndpointUris("/connect/authorize")
                     .SetLogoutEndpointUris("/connect/logout")
                     .SetTokenEndpointUris("/connect/token");

                    options.AllowAuthorizationCodeFlow()
                    .AllowRefreshTokenFlow();

                   scopes.
                    options.RegisterScopes(Scopes.Email, Scopes.OpenId, Scopes.OfflineAccess);

                    // Register the signing and encryption credentials.
                    options.AddDevelopmentEncryptionCertificate()
                           .AddDevelopmentSigningCertificate();

                    options.UseAspNetCore()
                    .EnableTokenEndpointPassthrough();
                })
            .AddValidation(options =>
            {
                // Import the configuration from the local OpenIddict server instance.
                options.UseLocalServer();

                // Register the ASP.NET Core host.
                options.UseAspNetCore();
            });

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme;
            });




        }

任何帮助将是非常感激的!

asp.net-core authorization openiddict
1个回答
1
投票

你看到的行为实际上是 "正常的":当你设置了 DefaultAuthenticateScheme认证中间件(也就是 app.UseAuthentication())--通常是在ASP.NET Core管道中很早添加的--自动调用相应的验证处理程序来填充 HttpContext.User.

这种机制总是独立于你的MVC动作是否用了 [Authorize][AllowAnonymous]. 如果OpenIddict无法找到访问令牌在 Authorization 头,它会记录一个错误,并告诉认证栈它不能提供的 AuthenticateResult 的请求。

如果你的MVC动作被装饰成 [Authorize]认证栈将要求OpenIddict返回一个401挑战(除非 [AllowAnonymous] 被使用)。)

也就是说,虽然它并不妨碍事情的正常工作,但我同意它可能是一个重要的噪音来源,所以我会考虑删除这个日志消息。我打开了 https:/github.comopeniddictopeniddict-coreissues941。 追踪。

干杯。

© www.soinside.com 2019 - 2024. All rights reserved.