Skip to content

Instantly share code, notes, and snippets.

@NeelBhatt
Created December 9, 2018 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeelBhatt/d6777600a8ae1659281d7947c295e2a7 to your computer and use it in GitHub Desktop.
Save NeelBhatt/d6777600a8ae1659281d7947c295e2a7 to your computer and use it in GitHub Desktop.
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