Skip to content

Instantly share code, notes, and snippets.

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 boubkhaled/f479efbef581e1c4a9b89cff9fa96a62 to your computer and use it in GitHub Desktop.
Save boubkhaled/f479efbef581e1c4a9b89cff9fa96a62 to your computer and use it in GitHub Desktop.
Allow CORS Asp.net Framework in Global.asax.cs
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Http;
namespace App
{
public class WebApiApplication : HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
EnableCORS();
}
private void EnableCORS()
{
try
{
var context = HttpContext.Current;
var response = context.Response;
// enable CORS
response.AddHeader("Access-Control-Allow-Origin", "*");
response.AddHeader("X-Frame-Options", "ALLOW-FROM *");
if (context.Request.HttpMethod == "OPTIONS")
{
response.AddHeader("Access-Control-Allow-Methods", "*");
response.AddHeader("Access-Control-Allow-Headers", "*");
response.AddHeader("Access-Control-Max-Age", "1728000");
response.End();
}
}
catch (Exception)
{
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment