Authentication 指定单个路由的身份验证方案';由中间件处理

Authentication 指定单个路由的身份验证方案';由中间件处理,authentication,jwt,openid-connect,asp.net-core-3.1,hotchocolate,Authentication,Jwt,Openid Connect,Asp.net Core 3.1,Hotchocolate,我有一个新的ASP.NETCore3MVC站点,我已经添加了GraphQL。我让用户使用基于cookie的auth和Auth0登录到MVC端: services.AddAuthentication(选项=> { options.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme; options.defaultsignnscheme=CookieAuthenticationDefaults.Aut

我有一个新的ASP.NETCore3MVC站点,我已经添加了GraphQL。我让用户使用基于cookie的auth和Auth0登录到MVC端:

services.AddAuthentication(选项=>
{
options.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
options.defaultsignnscheme=CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme=CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(“Auth0”,选项=>
{
...
});
但是对于GraphQL请求,我需要JWT auth,我将使用:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(配置=>
{
...
});
他们两人都独立地工作得很好。cookieauth允许控制器使用;jwtauth允许使用GQL。但是我不知道如何获得控制器的cookieauth和
/graphql
路由的JWT

小背景:HotChocolate使用自定义中间件来处理进入
/graphql
路由的请求。它不在控制器上,所以我不能只指定带有
Authorize
属性的方案,因为没有控制器可以将它放在上面

类似问题
  • -这里的答案(“使用框架”)含糊不清,毫无帮助
  • -这是.Net Core 1.1,而不是3.1。API已经发生了很大的变化。尝试对其进行调整,但出现运行时错误,表示需要
    defaultsignnscheme

(还有一些其他的,主要集中在结合REST和MVC上,但这两个都是控制器的,所以场景有点不同。)

我不知道怎么做。最后我把它分成了两个项目。一个提供GraphQLAPI,另一个提供网站。这样做,每个人都可以有自己的身份验证方案。

我也有同样的问题,所以我想我会发布我的解决方案,以防其他人也有同样的问题

热巧克力使用默认的身份验证方案,因此在启动时,我将JWT声明为默认:

    services.AddAuthentication(options =>
    {
        options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

    })
在任何需要Cookie身份验证的地方,我都只需添加:

[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]