CORS in .NET Core

前端 未结 10 1085
失恋的感觉
失恋的感觉 2020-12-04 23:30

I am trying to enable CORS in .NET Core in this way:

    public IConfigurationRoot Configuration { get; }

    public void ConfigureServices(IServiceCollecti         


        
10条回答
  •  情歌与酒
    2020-12-05 00:12

    This way works normally, just tried it on angular2 with .net core. The issue the OP is having is that this doesnt work with windows authentication. I am assuming the middleware for windows authentication is happening before a request comes through, in which case its breaking. Best bet would be to see if there is a way to enable the windows auth middleware after the cors middleware has processed in Configure.

    Then the order would be

    App.UseCors()

    App.UseWindowsAuth()

    App.UseMVC()

    They must happen in this order for it to work.

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
                                                                        .AllowAnyMethod()
                                                                         .AllowAnyHeader()));     
            services.AddMvc();            
        }
    
        public void Configure(IApplicationBuilder app)
        {
            app.UseCors("AllowAll");
    
            app.UseMvc(routes =>
             {
                 routes.MapRoute(
                     name: "default",
                     template: "{controller=Home}/{action=Index}/{id?}");
             });
    
        }
    

提交回复
热议问题