First, add ConfigureServices
public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("any", builder => { // builder.AllowAnyOrigin () // allow host access from any source builder .WithOrigins ( " HTTP: //*.*.*.* " ) // .SetIsOriginAllowedToAllowWildcardSubdomains () // set the domain allowed access .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials();// }); }); services.AddControllers(); }
Then add
public class CorsMiddleware { private readonly RequestDelegate _next; public CorsMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { if (!context.Response.Headers.ContainsKey("Access-Control-Allow-Origin")) { context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); } await _next(context); } }
Then use the middleware
app.UseMiddleware<CorsMiddleware>();