本文整理汇总了C#中System.Web.Http.Cors.EnableCorsAttribute.GetCorsPolicyAsync方法的典型用法代码示例。如果您正苦于以下问题:C# EnableCorsAttribute.GetCorsPolicyAsync方法的具体用法?C# EnableCorsAttribute.GetCorsPolicyAsync怎么用?C# EnableCorsAttribute.GetCorsPolicyAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Http.Cors.EnableCorsAttribute
的用法示例。
在下文中一共展示了EnableCorsAttribute.GetCorsPolicyAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureCors
private static EnableCorsAttribute ConfigureCors(IAppBuilder app)
{
var appSettings = WebConfigurationManager.AppSettings;
// Load CORS settings from Web.config
var corsPolicy = new EnableCorsAttribute(
appSettings["cors:Origins"],
appSettings["cors:Headers"],
appSettings["cors:Methods"]);
// Enable CORS for ASP.NET Identity
app.UseCors(new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = request =>
{
if (request.Path.Value == "/token")
{
return corsPolicy.GetCorsPolicyAsync(null, CancellationToken.None);
}
else
{
return Task.FromResult(new CorsPolicy
{
AllowAnyHeader = true,
AllowAnyMethod = true,
AllowAnyOrigin = true,
SupportsCredentials = true,
});
}
}
}
});
return corsPolicy;
}
示例2: Configuration
/// <summary>
/// Configures the application.
/// </summary>
/// <param name="app">
/// The app builder.
/// </param>
/// <remarks>
/// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
/// </remarks>
public static void Configuration(IAppBuilder app)
{
EnsureValidCulture();
// Web API routes
var config = new HttpConfiguration { DependencyResolver = GlobalConfiguration.Configuration.DependencyResolver };
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
//config.EnableCors();
var appSettings = WebConfigurationManager.AppSettings;
// If CORS settings are present in Web.config
if (!string.IsNullOrWhiteSpace(appSettings["cors:Origins"]))
{
// Load CORS settings from Web.config
var corsPolicy = new EnableCorsAttribute(appSettings["cors:Origins"], appSettings["cors:Headers"], appSettings["cors:Methods"]);
// Enable CORS for ASP.NET Identity
app.UseCors(
new CorsOptions
{
PolicyProvider =
new CorsPolicyProvider
{
PolicyResolver =
request =>
request.Path.Value == "/token"
? corsPolicy.GetCorsPolicyAsync(null, CancellationToken.None)
: Task.FromResult<CorsPolicy>(null)
}
});
// Enable CORS for Web API
config.EnableCors(corsPolicy);
}
config.MapHttpAttributeRoutes();
ConfigureAuth(app);
app.UseWebApi(config);
SetupJSonFormatter(config);
config.Filters.Add(new CslaPrincipalAuthenticationFilter());
}