openiddict 禁用端点路由

openiddict disable endpoint routing

我有一个使用 openiddict 的 Asp.Net 核心项目设置。我使用的其中一个包 (Asp.Net Odata) 不支持端点路由,所以我在 ConfigureServices

中禁用了它

services.AddControllers(c=>c.EnableEndpointRouting = false)

问题是当我执行此操作时 openiddict 扩展方法 GetOpenIddictServerRequest returns null。只要启用端点路由,一切正常

 [HttpPost("~/oauth/token"), Produces("application/json")]
        public async Task<IActionResult> Token()
        {
            var request = HttpContext.GetOpenIddictServerRequest();
...

我注册openiddict如下图

services.AddOpenIddict()
                .AddCore(options =>
                {
                    // Configure OpenIddict to use the Entity Framework Core stores and entities.
                    options.UseEntityFrameworkCore()
                                   .UseDbContext<MiPayOnlineCoreContext>();
                })

                .AddServer(options =>
                {

                    options.SetAccessTokenLifetime(TimeSpan.FromMinutes(5));
                    options.SetRefreshTokenLifetime(TimeSpan.FromMinutes(11));

                    options.SetTokenEndpointUris("/oauth/token");

                    options.AllowPasswordFlow();
                    options.AllowRefreshTokenFlow();                    

                    options.SetIssuer(new Uri(authSettings[nameof(JWTSettings.Issuer)]));
                    options.AddSigningCertificate(certificate);
                    options.AddEncryptionCertificate(certificate);

                    // Mark the "email", "profile" and "roles" scopes as supported scopes.
                    options.RegisterScopes(OpenIddictConstants.Scopes.Email,
                                           OpenIddictConstants.Scopes.Profile,
                                           OpenIddictConstants.Scopes.Roles);


                    options.UseAspNetCore()
                           .EnableStatusCodePagesIntegration()
                           .EnableAuthorizationEndpointPassthrough()
                           .EnableLogoutEndpointPassthrough()
                           .EnableTokenEndpointPassthrough()
                           .EnableUserinfoEndpointPassthrough();

                });

是否可以在禁用端点路由的情况下进行这项工作?

这是您代码中的错误:您没有将 app.UseMvc() 放在正确的位置。

确保它出现在 app.UseAuthentication()app.UseAuthorization() 之后,错误就会消失。