【问题标题】:Asp.net Core, JWT, and CORS HeadersAsp.net Core、JWT 和 CORS 标头
【发布时间】:2016-05-02 11:24:00
【问题描述】:

当我在同一服务上同时启用 JWT 承载身份验证和 CORS 时,我在从服务器获取适当的 Access-Control-Allow-Origin 标头时遇到问题。 当我从配置中删除 UseJwtBearerAuthentication 时,一切正常。

public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAllOrigins", builder =>
            {
                builder.AllowAnyOrigin();
                builder.AllowAnyHeader();
                builder.AllowAnyMethod();
                builder.AllowCredentials();
            });
        });

        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseJwtBearerAuthentication(options =>
        {
            options.AutomaticAuthenticate = true;
            options.RequireHttpsMetadata = false;
            options.Audience = "c2cf422a-a432-2038-b183-cda64e16239e";
            options.Authority = "domain.com";
        });

        app.UseCors("AllowAllOrigins");

        app.UseIISPlatformHandler();

        app.UseMvc();
    }

我尝试更改配置的顺序,但似乎没有任何效果。我还尝试将 [EnableCors("AllowAllOrigins")] 添加到我正在调用的控制器中。

我已根据 cmets 中的建议更改了配置顺序,并确定了导致问​​题的属性:

app.UseIISPlatformHandler();

        app.UseCors("AllowAllOrigins");

        app.UseJwtBearerAuthentication(options =>
        {
            options.AutomaticAuthenticate = true;
            options.RequireHttpsMetadata = false;
            options.Audience = "c8cf662a-ac73-4050-b285-cda90e22992e";
            options.Authority = "iwdwk.com";
        });

        app.UseMvc();

在上面的代码中,下面的行似乎是导致问题的原因:

options.AutomaticAuthenticate = true;

不幸的是,我需要启用它,这样我才能通过 JWT 令牌进行授权...除非有其他方法可以做到这一点?

【问题讨论】:

  • 嗯。我们是否 100% 确定这不仅仅是订购问题?我相信正确的顺序应该是日志记录,然后是UseIISPlatformHandler,然后是UseCors,然后是UseJwtBearerAuthentication,最后是UseMvc。可以试试吗?
  • 不确定...我觉得我尝试了所有的组合,但我没有系统地完成它们。今天早上上班的时候我会试试你的建议。
  • 仍然不行...我正在更新问题,因为我确实发现了导致问题的属性。

标签: oauth-2.0 cors asp.net-core


【解决方案1】:

我猜,来自浏览器的 OPTIONS 调用被身份验证拒绝,因为它们可能不包含不记名令牌。我不确定,但必须有一种方法可以在调用 UseJwtBearerAuthentication 时跳过 OPTIONS 调用的身份验证。如果您希望在此之前确认这一点,请尝试从邮递员调用您的端点,因为它会跳过 OPTIONS 调用并查看您是否获得了实际 GET/POST 调用的访问控制标头

【讨论】:

    【解决方案2】:

    如果服务器抛出异常,它们会清除标头,因此您只会看到 CORS 错误。我过去查看实际情况的方法是首先使用 Chrome 开发工具从我的一个失败请求中获取Bearer Token。

    接下来,我复制了该令牌并粘贴到 Postman 作为请求中授权标头的值。当我这样做时,我终于收到了那个很好的开发人员异常页面,它准确地告诉我我做错了什么。那就是我使用的是 ClientId GUID Azure AD 给出的,而不是 App URI。

    因此,如果您使用的是 Azure AD,那么您的 JWT 选项应如下所示:

        app.UseJwtBearerAuthentication(options =>
        {
            options.AutomaticAuthenticate = true;
            options.AutomaticChallenge = true;
            options.Authority = "https://login.microsoftonline.com/yourtenant.onmicrosoft.com"
            options.Audience = "https://yourtenant.onmicrosoft.com/AppURI"
        });
    

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题。当进行 POST/GET 并且浏览器触发带有 OPTIONS 请求的 Preflight 时,我的 Angular 应用程序无法理解这是 401 响应。它返回为 0 - 未知错误。

      我的问题是我的服务顺序。我所要做的就是在 Startup.cs 文件中的 Configure 方法中,更改顺序。我首先添加了 CORS 配置,然后是 JWT 中间件,然后是 UseAuthentication

      【讨论】:

        【解决方案4】:

        意识到这个问题已经过时了,但这可能是因为您正在使用 AllowCredentials=true 和 AllowAnyOrigin=true 跨 CORS 执行身份验证。这是不允许的。尝试指定一个特定的来源。

        【讨论】:

          猜你喜欢
          • 2017-04-15
          • 2016-06-17
          • 1970-01-01
          • 2019-03-18
          • 2018-05-08
          • 2020-08-30
          • 2019-12-07
          • 2019-08-22
          • 2021-10-27
          相关资源
          最近更新 更多