Skip to content

Instantly share code, notes, and snippets.

@morbidcamel101
Created November 23, 2015 16:43
Show Gist options
  • Select an option

  • Save morbidcamel101/cebc3bccf15d5ba81c4e to your computer and use it in GitHub Desktop.

Select an option

Save morbidcamel101/cebc3bccf15d5ba81c4e to your computer and use it in GitHub Desktop.
Cors Module for ASP.NET MVC
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"]);
}
}
}
....
<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