当前位置: 首页>>代码示例>>C#>>正文


C# EnableCorsAttribute.GetCorsPolicyAsync方法代码示例

本文整理汇总了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;
        }
开发者ID:CasperTDK,项目名称:RSSFeed,代码行数:37,代码来源:Startup.cs

示例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());
        }
开发者ID:mparsin,项目名称:Elements,代码行数:70,代码来源:Startup.cs


注:本文中的System.Web.Http.Cors.EnableCorsAttribute.GetCorsPolicyAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。