Created
July 27, 2015 09:51
-
-
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
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
/// <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); | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: