Created
November 28, 2023 10:16
-
-
Save boubkhaled/f479efbef581e1c4a9b89cff9fa96a62 to your computer and use it in GitHub Desktop.
Allow CORS Asp.net Framework in Global.asax.cs
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.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