我们正在使用 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/