Asp.net core 为什么我的源代码在asp net core 3中为空,而不是本地主机

Asp.net core 为什么我的源代码在asp net core 3中为空,而不是本地主机,asp.net-core,.net-core,angular8,nswag,Asp.net Core,.net Core,Angular8,Nswag,我一直无法使用生成的Nswag typescript客户端在asp net core API和Angular 8应用程序之间获得CORS请求 刚才我明白了原因。我的来源是“null”。不http://localhost:4200如我所料 因此,当我添加“null”作为允许的原点时,一切都正常。但我想了解为什么,如果我让“null”成为生产中允许的来源,这会有什么影响 目前我的代码是这样的 appsettings.json "CORS-Settings": { "Allow-Origins":

我一直无法使用生成的Nswag typescript客户端在asp net core API和Angular 8应用程序之间获得CORS请求

刚才我明白了原因。我的来源是
“null”
。不<代码>http://localhost:4200如我所料

因此,当我添加“null”作为允许的原点时,一切都正常。但我想了解为什么,如果我让“null”成为生产中允许的来源,这会有什么影响

目前我的代码是这样的

appsettings.json

"CORS-Settings": {
  "Allow-Origins": [
    "http://localhost",
    "http://localhost:4200",
    "https://localhost",
    "https://localhost:4200",
    "null"     // <-- if I remove this my localhost browser gets denied by CORS
  ],
  "Allow-Methods": [ "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE" ]
}
“CORS设置”:{
“允许起源”:[
"http://localhost",
"http://localhost:4200",
"https://localhost",
"https://localhost:4200",
“空”//options
.有来源(允许来源)
.使用方法(允许的方法)
);
//其他东西
}
那么,为什么我需要添加“null”作为允许的来源,如果要将其用于生产,这是否有害


如果有什么不同,我使用的是firefox v72.0.1。

试试看。按照下面的方法操作

  • 请更改您的
    appsettings.json
  • 更改配置方法
  • public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境、IServiceScopeFactory scopeFactory、ILoggerFactory)
    {
    var allowedMethods=Configuration.GetSection(“CORS设置:允许方法”).Get();
    var allowedOrigins=Configuration.GetSection(“CORS设置:允许起源”).Get();
    app.UseCors(
    选项=>选项
    .使用方法(允许的方法)
    .有来源(允许来源)
    );
    //其他东西
    }
    
    似乎没有为本地地址生成CORS头,而是为远程地址生成

    因此,如果您使用的是windows,您可以通过将主机别名添加到主机文件(C:\windows\System32\drivers\etc\hosts)中,指向本地地址来进行操作

    例如:127.0.0.1 test123.com

    然后请更改您的appsettings.json:

    "CORS-Settings": {
      "Allow-Origins": [
        "http://test123.com:4200",
        "https://test123.com:4200"
      ],
      "Allow-Methods": [ "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE" ]
    }
    

    希望这能有所帮助。

    我所能看到的是,您所做的只是从应用程序设置中删除“null”。这对我不起作用,因为dot net core获得的原点是“null”。因此,如果我删除“null”请求将被拒绝。我已删除
    http://localhost
    https://localhost
    行。试试看。如果不起作用,请告诉我。我会告诉其他方法。
    "CORS-Settings": {
      "Allow-Origins": [
        "http://localhost:4200",
        "https://localhost:4200"
      ],
      "Allow-Methods": [ "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE" ]
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceScopeFactory scopeFactory, ILoggerFactory loggerFactory)
    {
        var allowedMethods = Configuration.GetSection("CORS-Settings:Allow-Methods").Get<string[]>();
        var allowedOrigins = Configuration.GetSection("CORS-Settings:Allow-Origins").Get<string[]>();
    
        app.UseCors(
            options => options
            .WithMethods(allowedMethods)
            .WithOrigins(allowedOrigins)
          );
        // other stuff
    }
    
    "CORS-Settings": {
      "Allow-Origins": [
        "http://test123.com:4200",
        "https://test123.com:4200"
      ],
      "Allow-Methods": [ "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE" ]
    }