Angular4 ASP.NET Core 1.2 Windows Authentication CORS for PUT and POST Gives 401

后端 未结 5 948
心在旅途
心在旅途 2020-12-06 02:18

My IDE is Visual Studio 2017. I\'ve got an Angular4 client talking to a WebAPI backend in Core, and CORS is working as configured EXCEPT for the PUT and POST methods. The G

5条回答
  •  庸人自扰
    2020-12-06 02:42

    I got it working with very simple approach. I uses dotNet Core 2.0.

    In configureServices method of startup
    
    services.AddCors(options =>
            {
                options.AddPolicy("app-cors-policy",
                    builder =>
                    {
                        builder
                            .AllowAnyOrigin()
                            .AllowAnyHeader()
                            .AllowAnyMethod()
                            .AllowCredentials()
                        ;
                    });
    
            });
    
    
    In configure method of startup
    
    app
       .UseCors("app-cors-policy") //Must precede UseMvc
       .UseMvc();
    

    Then, enable both - Anonymous authentication and Windows authentication as well. This allows OPTIONS requests (preflight) which are anonymous always, to go through. All xhr requests has to be withCredentials else will fail.

提交回复
热议问题