Benutzer:MovGP0/ASP.NET Core/CORS

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      

Cross-Origin Resource Sharing

[Bearbeiten | Quelltext bearbeiten]
  • Allows AJAX calls to other domains that the domain where the JavaScript script was loaded from
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("CorsPolicy", builder => builder
            .WithOrigins("http://example.com")
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
    });
}


public void Configure(IApplicationBuilder app)
{
    app.UseCors(c => c.WithOrigins("http://example.com")); // default policy
    app.UseCors("CorsPolicy"); // named policy

    // ensure UseCors() is called before this
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}
  • use [EnableCors("CorsPolicy")] to enable policies on specific controllers.

|}