简体   繁体   English

Cors 在 webapi 上添加 app.useauthentication 时失败 .net 核心 3.1

[英]Cors fail when add app.useauthentication on webapi .net core 3.1

I add UseAuthentication() ;我添加UseAuthentication() to my startup.cs The Allow cors fail and the cors is not allowed, there's any order to setup please?到我的startup.cs允许cors 失败并且不允许cors,请问有什么设置命令吗? (without app.UseAuthentication() no cors problem) (没有 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();

on ConfigureServices i have:在 ConfigureServices 我有:

  void ConfigureServices(IServiceCollection services)
{....       


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

在此处输入图像描述

Did you add你加了吗

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

to ConfigureServices?配置服务?

This is working for me:这对我有用:

readonly string MyAllowSpecificOrigins = "AnyPolicyName";

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

EDIT: Also add编辑:还添加

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

    app.UseCors(MyAllowSpecificOrigins);

    /*...*/
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM