Skip to content

Instantly share code, notes, and snippets.

@naik899
Created March 22, 2019 16:13
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 naik899/716cc7b2decf44ac168e750e03310df0 to your computer and use it in GitHub Desktop.
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
[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