/* default policy */
// add policy
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.WithOrigins("http://localhost:4200") // or AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin();
})
});
// enable policy
app.UseCors();
/* named policy */
// add policy
var AllowWebClient = "allowWebClient";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: AllowWebClient,
policy =>
{
policy.WithOrigins("http://localhost:4200") // or AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin();
}
);
});
// enable policy
app.UseCors(AllowWebClient);
public static void Register(HttpConfiguration config)
{
var corsAttr = new EnableCorsAttribute("http://example.com", "*", "*");
config.EnableCors(corsAttr);
}
public static void Register(HttpConfiguration config)
{
// New code
config.EnableCors();
}
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("https://domain2.com")
.allowedMethods("PUT", "DELETE")
.allowedHeaders("header1", "header2", "header3")
.exposedHeaders("header1", "header2")
.allowCredentials(true).maxAge(3600);
// Add more mappings...
}
}
Response.AppendHeader("Access-Control-Allow-Origin", "*");