Cross -site requests may bring security risks, especially when accessing resources in different domains in the domain where access and request resources. CORS (transmitted resource sharing) is an important mechanism that allows access to resources from different sources by requesting requests, while maintaining data integrity, thereby reducing these risks.
In ASP.NET core, the use of CORS is essential for allowing client applications to use your webapi from different domains. The errors you encounter show that although Corsoft has followed Microsoft, CORS has not been implemented correctly.
The solution is to simplify your method. Consider the use of the following basic Cors, rather than using more complicated strategic configuration:
Configure the CORS service
In the method of startup.cs, add the Cors service:
ConfigureServices
public void ConfigureServices(IServiceCollection services) { services.AddCors(); // 在AddMvc之前 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }
In the <中> method, apply CORS middleware before registering MVC:
This simplified method provides a basic CORS protection level, allowing it to allow from a specific domain ( https://www.php.cn/link/efe7beaa44d6E14C3043B2522ba2 Configure
.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // 在app.UseMvc()之前 app.UseCors( options => options.WithOrigins("http://example.com").AllowAnyMethod() ); app.UseMvc(); }
The above is the detailed content of How to Easily Enable CORS in ASP.NET Core Web API?. For more information, please follow other related articles on the PHP Chinese website!