Skip to content

Instantly share code, notes, and snippets.

@lski
Created July 27, 2015 09:51
Show Gist options
  • Save lski/fe048dd9973ef80d7127 to your computer and use it in GitHub Desktop.
Save lski/fe048dd9973ef80d7127 to your computer and use it in GitHub Desktop.
Allows restriction of cors requests when using the app.UseCors() rather than just CorsOptions.AllowAll
/// <summary>
/// To be used with CorsMiddleware <code>app.UseCors()</code> where origins are restricted to the parameters passed in.
/// </summary>
public partial class CorsOptionsExt {
public static CorsOptions AllowOrigins(params string[] origins) {
return new CorsOptions() {
PolicyProvider = new CorsPolicyProvider() {
PolicyResolver = (context) => {
var policy = new CorsPolicy() {
AllowAnyHeader = true,
AllowAnyMethod = true,
SupportsCredentials = true,
};
foreach (var item in origins) {
policy.Origins.Add(item);
}
return Task.FromResult(policy);
}
}
};
}
}
@lski
Copy link
Author

lski commented Jul 27, 2015

Usage:

app.UseCors(CorsOptionsExt.AllowOrigins("http://localhost:8080", "http://localhost:8000"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment