【问题标题】:Cors fail when add app.useauthentication on webapi .net core 3.1在 webapi .net core 3.1 上添加 app.useauthentication 时 Cors 失败
【发布时间】:2021-07-22 17:00:23
【问题描述】:

我加UseAuthentication();到我的startup.cs 允许cors 失败并且不允许cors,请问有什么设置命令吗? (没有 app.UseAuthentication() 没有 cors 问题)

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseCors(
options => options.AllowAnyOrigin()
      .AllowAnyMethod()
      .AllowAnyHeader()
  ); app.UseHttpStatusCodeExceptionMiddleware();
        app.UseAuthentication();

在我拥有的 ConfigureServices 上:

  void ConfigureServices(IServiceCollection services)
{....       


        services.AddCors();
                services.AddSSOAuthentication(Configuration, CurrentEnvironment);
                services.AddControllers();
                services.AddRequestLogging(opt =>
                {
                    opt.ResponseTimeInMs = true;
                    opt.CookiesLoggingEnabled = false;
                });
                services.AddSwaggerGen();
    
            }

【问题讨论】:

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


【解决方案1】:

这对我有用:

readonly string MyAllowSpecificOrigins = "AnyPolicyName";

public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(MyAllowSpecificOrigins,
                                builder =>
                                {
                                    builder.AllowAnyOrigin();
                                });
        });
    
        /** etc. **/
    }

编辑:同时添加

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    /*...*/

    app.UseCors(MyAllowSpecificOrigins);

    /*...*/
}

【讨论】:

    【解决方案2】:

    你添加了吗

    services.AddCors(options =>
                {
                    ...
                });
    

    配置服务?

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2020-08-02
      • 2017-04-12
      • 2020-10-18
      • 2021-07-27
      • 2017-02-15
      • 2021-07-19
      • 2020-11-24
      • 2020-05-22
      相关资源
      最近更新 更多