zoukankan      html  css  js  c++  java
  • Asp.net core 跨域设置

    public void ConfigureServices(IServiceCollection services)
    {

    services.AddCors(Options =>
    {
    //无任何限制  
    Options.AddPolicy("any", builder =>
    {
    builder.AllowAnyHeader().AllowAnyMethod().AllowAnyHeader();
    });

    //②允许所有域名的请求
    Options.AddPolicy("allow2", builder =>
    {
    builder.AllowAnyOrigin();
    });

    //③允许某域名的请求,且限制该请求的类型
    Options.AddPolicy("allow3", builder =>
    {
    builder.WithOrigins("https://localhost:44384").WithMethods("POST", "HEAD");
    //builder.WithOrigins("https://localhost:44384").AllowAnyMethod();//允许任何http请求的方法
    });

    //④允许某域名的请求,带请求头部信息
    Options.AddPolicy("allow4", builder =>
    {
    builder.WithOrigins("https://localhost:44384").WithHeaders("accept", "content-type", "origin", "x-custom-header");
    //builder.WithOrigins("https://localhost:44384").AllowAnyHeader();//
    //builder.WithOrigins("https://localhost:44384").WithExposedHeaders("x-custom-header");//指定的说明文头信息
    });
    //⑤允许某域名的请求,预检时间多久可以被缓存
    Options.AddPolicy("allow5", builder =>
    {
    builder.WithOrigins("https://localhost:44384").SetPreflightMaxAge(TimeSpan.FromSeconds(2520));
    });

    Options.AddPolicy("allow6", builder =>
    {
    builder.WithOrigins("https://localhost:44384");
    });

    });

    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {

    //....
    app.UseRouting();

    //添加需要的设置
      app.UseCors("any"); 

    //...
    app.UseAuthorization();

    //...

    }

    //使用

    [Microsoft.AspNetCore.Cors.EnableCors("any")]
    public class APPController :Controller{}

  • 相关阅读:
    【python】raise_for_status()抛出requests.HTTPError错误
    【python】python打包生成的exe文件运行时提示缺少模块
    单点登录原理与简单实现
    Java并发之AQS详解
    Java更新
    各种java面经资源
    HashMap与HashTable的区别
    Http 与Https
    SpringMVC运行原理
    Spring
  • 原文地址:https://www.cnblogs.com/ruiying/p/13139766.html
Copyright © 2011-2022 走看看