Created
March 22, 2019 16:13
-
-
Save naik899/716cc7b2decf44ac168e750e03310df0 to your computer and use it in GitHub Desktop.
Create a custom cors policy to allow dynamic list of origins in Asp.Net
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
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)] | |
public class MyCorsPolicy : Attribute, ICorsPolicyProvider | |
{ | |
private CorsPolicy _policy; | |
public MyCorsPolicy() | |
{ | |
// Create a CORS policy. | |
_policy = new CorsPolicy | |
{ | |
AllowAnyMethod = true, | |
AllowAnyHeader = true | |
}; | |
// Add allowed origins. | |
var origins = ConfigurationManager.AppSettings["CORSOrigin"].Split(','); | |
foreach (var origin in origins) | |
{ | |
_policy.Origins.Add(origin); | |
} | |
} | |
public Task<CorsPolicy> GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
return Task.FromResult(_policy); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment