cors - 如何允许 OWIN token 请求使用特定的 CORS 来源、 header 和方法?

标签 cors owin asp.net-web-api2

我们正在使用 Web api 和 OWIN 创建我们自己的身份验证和授权提供程序。一切都很好,但现在我们的任务是允许 CORS 请求。

CorsHttpConfigurationExtensions 适用于我们的 api,并且我们还使用 CorsExtensions 处理 token 请求。不过,事情是这样的:这一行

 app.UseCors(CorsOptions.AllowAll);

Startup.ConfigureAuth() 中允许所有来源、 header 项和方法,不是吗?相当于

config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

WebApiConfig.Configure()中 - 对吗? CorsOptions 只有一个枚举值 AllowAll。并且没有地方可以用任何类型的属性来装饰 token 路由。

我不太关心标题和动词(方法);据推测,OWIN 程序集正在处理这些问题,例如限制为 POST。但我们确实希望将原始 header 限制为特定域。有谁知道我们如何才能做到这一点?

最佳答案

using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Cors;
using System.Web.Cors;

[assembly: OwinStartup(typeof(SignalRSelfHost.Startup))]

namespace SignalRSelfHost
{
    public class Startup
    {

        public void Configuration(IAppBuilder app)
        {
            var policy = new CorsPolicy()
            {
                AllowAnyHeader = true,
                AllowAnyMethod = true,
                SupportsCredentials = true
            };

            // list of domains that are allowed can be added here
            policy.Origins.Add("domain"); 
            //be sure to include the port:
            //example: "http://localhost:8081"

            app.UseCors(new CorsOptions
            {
                PolicyProvider = new CorsPolicyProvider
                {
                    PolicyResolver = context => Task.FromResult(policy)
                }
            });

            app.MapSignalR();
        }
    }
}

请引用此stackoverflow link

关于cors - 如何允许 OWIN token 请求使用特定的 CORS 来源、 header 和方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25006812/

相关文章:

asp.net-mvc-5 - OWIN 是否调用 MVC (ASP.NET MVC)?

c# - WebApi DI Autofac - 确保 Controller 具有无参数公共(public)构造函数

java - CORS 允许来源限制不会导致服务器拒绝请求

signalr - 如何限制SignalR服务器连接?

javascript - 'Access-Control-Allow-Origin' header 是如何工作的?

c# - owin启动类visual studio 2012出错

entity-framework - 如何发布到 odata webapi 中的相关集合

authentication - 具有自定义身份验证的 Web API 2 OAuth 不记名 token

http - 从安全角度来看,允许特定域使用 CORS 有意义吗?

html - 单击 anchor 标记上的链接是跨源请求吗?