Created
November 23, 2015 16:43
-
-
Save morbidcamel101/cebc3bccf15d5ba81c4e to your computer and use it in GitHub Desktop.
Cors Module for ASP.NET MVC
This file contains hidden or 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.Web; | |
| /// <summary> | |
| /// Cross-Origin Request handler. | |
| /// </summary> | |
| public class CorsModule : IHttpModule | |
| { | |
| public void Dispose() | |
| { | |
| // Nothing to dispose | |
| } | |
| public void Init(HttpApplication context) | |
| { | |
| context.BeginRequest += new EventHandler(Context_BeginRequest); | |
| } | |
| private void Context_BeginRequest(object sender, EventArgs e) | |
| { | |
| HttpApplication app = sender as HttpApplication; | |
| if (app.Request.Headers.AllKeys.Select(k => k.ToLower()).Contains("origin")) | |
| { | |
| // TODO - Do security checks | |
| app.Response.AddHeader("Access-Control-Allow-Origin", app.Request.Headers["Origin"]); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| .... | |
| <system.webServer> | |
| <validation validateIntegratedModeConfiguration="false" /> | |
| <modules runAllManagedModulesForAllRequests="true"> | |
| .... | |
| <add type="CorsModule" name="CorsModule" /> | |
| </modules> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment