Created
August 3, 2022 10:38
-
-
Save boubkhaled/9c6ba217ea53a1b6548e0041b18ca779 to your computer and use it in GitHub Desktop.
Allow Cross-origin resource sharing - CORS - In WebApi2 In Global.asax.cs
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.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