Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save boubkhaled/9c6ba217ea53a1b6548e0041b18ca779 to your computer and use it in GitHub Desktop.

Select an option

Save boubkhaled/9c6ba217ea53a1b6548e0041b18ca779 to your computer and use it in GitHub Desktop.
Allow Cross-origin resource sharing - CORS - In WebApi2 In Global.asax.cs
using System.Linq;
private readonly string[] authorizedSites = new string[]
{
"https://site1.com",
"https://site2.com"
};
private void SetAccessControlAllowOrigin()
{
string origin = HttpContext.Current.Request.Headers.Get("Origin");
if (authorizedSites.Contains(origin))
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin);
}
protected void Application_BeginRequest()
{
SetAccessControlAllowOrigin();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment