Benutzer:MovGP0/ASP.NET Core/CORS
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.
Quellen
[Bearbeiten | Quelltext bearbeiten]- Mike Wasson, Shayne Boyer: Enabling Cross-Origin Requests (CORS). In: ASP.NET Core Docs. Microsoft, 14. Oktober 2016, abgerufen am 15. Mai 2017.
|}