Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Threading.Tasks;  
using Microsoft.AspNetCore.Builder;  
using Microsoft.AspNetCore.Http;  
  
namespace Angular7DemoServices  
{  
    // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project  
    public class CorsMiddleware  
    {  
        private readonly RequestDelegate _next;  
  
        public CorsMiddleware(RequestDelegate next)  
        {  
            _next = next;  
        }  
  
        public Task Invoke(HttpContext httpContext)  
        {  
  
            httpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");  
            httpContext.Response.Headers.Add("Access-Control-Allow-Credentials", "true");  
            httpContext.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept");  
            httpContext.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET,PUT,PATCH,DELETE,OPTIONS");  
            return _next(httpContext);  
        }  
    }  
  
    // Extension method used to add the middleware to the HTTP request pipeline.  
    public static class MiddlewareExtensions  
    {  
        public static IApplicationBuilder UseMiddleware(this IApplicationBuilder builder)  
        {  
            return builder.UseMiddleware<CorsMiddleware>();  
        }  
    }  
}  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.