.NET Core3.1 WebApi跨域处理

一、引入cors组件

  右击项目——在终端中打开——dotnet add package Microsoft.AspNetCore.Cors --version 2.0.1

 

 

 二、配置cors

  在项目根目录的Startup.cs

  (1)ConfigureServices方法  

    services.AddCors(options =>
    {
      options.AddPolicy("userLogin",
        builder => {
          builder.WithOrigins("http://10.10.10.13:1337", "http://10.10.10.13:5000", "http://10.10.10.13", "http://10.10.10.13:80", "http://localhost:80", "http://localhost:1337", "http://localhost").AllowAnyMethod().AllowAnyHeader().AllowCredentials();

      });
    });

  (2)Configure方法

     app.UseCors();

三、使用

  在前端需要调用的api接口前加上[EnableCors("userLogin")]

  

 

posted @ 2022-12-21 09:23  小严不言慢  阅读(337)  评论(0)    收藏  举报