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{}

  • 相关阅读:
    SqlCacheDependency [转]
    C#导出Word [ZT]
    ADO.NET Entity Framework 学习(1) [ZT]
    AJAX, JSON.js,Newtonsoft.Json.dll,nunit.framework.dll 源代码
    ADO.NET 1.1和2.0事务的区别
    Sql Server 2000 中游标的使用示例 [ZT]
    如何检测是否安装了.NET 2.0和.NET 3.0 [ZT]
    ORACLE 常用函数 [ZT]
    Resource 学习笔记
    GridView 双击选择行 [ZT]
  • 原文地址:https://www.cnblogs.com/ruiying/p/13139766.html
Copyright © 2011-2022 走看看