Created
December 9, 2018 17:31
-
-
Save NeelBhatt/d6777600a8ae1659281d7947c295e2a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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