Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net core 2.1跨组织策略。添加为CORS时,只有一个url有效_C#_.net Core_Cors_Asp.net Core 2.1 - Fatal编程技术网

C# asp.net core 2.1跨组织策略。添加为CORS时,只有一个url有效

C# asp.net core 2.1跨组织策略。添加为CORS时,只有一个url有效,c#,.net-core,cors,asp.net-core-2.1,C#,.net Core,Cors,Asp.net Core 2.1,我正在尝试添加多个rul,这些rul应该被列为CORS的白名单 问题是只有一个url工作。我的代码如下 public class StartupShutdownHandler { private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private const

我正在尝试添加多个rul,这些rul应该被列为CORS的白名单

问题是只有一个url工作。我的代码如下

public class StartupShutdownHandler
{
    private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private const string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
    public StartupShutdownHandler(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddMvc(options => { options.RespectBrowserAcceptHeader = true; }).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters();

        CorsRelatedPolicyAddition(services);
    }
    private void CorsRelatedPolicyAddition(IServiceCollection services)
    {
/*        var lstofCors = ConfigurationHandler.GetSection<List<string>>(StringConstants.AppSettingsKeys.CORSWhitelistedURL);
*/
var lstofCors = new  List<string> {"url1", "url2"}

        if (lstofCors != null && lstofCors.Count > 0 && lstofCors.Any(h => !string.IsNullOrWhiteSpace(h)))
        {
            services.AddCors(options =>
            {
                //https://stackoverflow.com/questions/43985620/asp-net-core-use-multiple-cors-policies
                foreach (var entry in lstofCors.FindAll(h => !string.IsNullOrWhiteSpace(h)))
                {
                    options.AddPolicy(MyAllowSpecificOrigins, builder => { builder.WithOrigins(entry); });
                }
            });
        }            
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseCors(MyAllowSpecificOrigins);

        // CheckAndEnableDetailLogs(app);
        app.UseMvc();                    
    }       
}
公共类StartupShutdownHandler
{
私有静态只读log4net.ILog Logger=log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
私有常量字符串MyAllowSpecificCorigins=“\u MyAllowSpecificCorigins”;
公用StartupShutdownHandler(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddSingleton();
services.AddMvc(options=>{options.rementBrowserAcceptHeader=true;}).AddXmlSerializerFormatters().AddXmlDataContractSerializerFormatters();
公司相关政策补充(服务);
}
私人作废公司申报政策添加(IServiceCollection服务)
{
/*var lstofCors=ConfigurationHandler.GetSection(StringConstants.AppSettingsKeys.CORSWhitelistedURL);
*/
var lstofCors=新列表{“url1”、“url2”}
如果(lstofCors!=null&&lstofCors.Count>0&&lstofCors.Any(h=>!string.IsNullOrWhiteSpace(h)))
{
services.AddCors(选项=>
{
//https://stackoverflow.com/questions/43985620/asp-net-core-use-multiple-cors-policies
foreach(lstofCors.FindAll(h=>!string.IsNullOrWhiteSpace(h))中的var条目)
{
options.AddPolicy(myAllowSpecificCorigins,builder=>{builder.WithOrigins(entry);});
}
});
}            
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序、IHostingEnvironment环境、IApplicationLifetime应用程序LifeTime)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
应用程序UseCors(MyAllowSpecificCorigins);
//CheckandEnableDetailogs(应用程序);
app.UseMvc();
}       
}

您正在覆盖数组中每个条目的选项

只需将条目添加为字符串数组。

编辑:
我添加了AllowAnyMethod(),因为看起来只有get方法起作用了

非常感谢
    var lstofCors = new  string[] {"url1", "url2"};
    services.AddCors(options =>
        {
           options.AddPolicy(MyAllowSpecificOrigins, builder => { builder.WithOrigins(lstofCors.ToArray()).AllowAnyMethod(); });
        });
}