我正在使用DynamicPolicyProviderFactory来确定是否允许来自域的请求在Asp.Net Web API 2.2应用程序中。为了做出这个决定,我使用了Web.Config中定义的几个RegEx模式
<applicationSettings>
<MyApp.Properties.Settings>
<setting name="AllowedDomains" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>http://localhost</string>
<string>http*://*.domain1.com</string>
<string>http*://*.domain2.com</string>
</ArrayOfString>
</value>
</setting>
</MyApp.Properties.Settings>
</applicationSettings>
然后在我的DynamicPolicyProviderFactory
内创建一个我可以使用的RegEx:
//constructor
public DynamicPolicyProviderFactory(IEnumerable allowedOrigins)//allowedDrigins is the strings passed from the config {
_allowed = new HashSet<Regex>();
foreach (string pattern in allowedOrigins.Cast<string>()
.Select(Regex.Escape)
.Select(pattern => pattern.Replace("*", "w*")))
{
_allowed.Add(new Regex(pattern, RegexOptions.IgnoreCase));
}
if (_allowed.Count > 0)
return;
//if nothing is specified, we assume everything is.
_allowed.Add(new Regex(@"https://\w*", RegexOptions.IgnoreCase));
_allowed.Add(new Regex(@"http://\w*", RegexOptions.IgnoreCase));
}
public ICorsPolicyProvider GetCorsPolicyProvider(HttpRequestMessage request)
{
var route = request.GetRouteData();
var controller = (string)route.Values["controller"];
var corsRequestContext = request.GetCorsRequestContext();
var originRequested = corsRequestContext.Origin;
var policy = GetPolicyForControllerAndOrigin(controller, originRequested);
return new CustomPolicyProvider(policy);
}
//this is where the magic happens
private CorsPolicy GetPolicyForControllerAndOrigin(string controller, string originRequested)
{
// Do lookup to determine if the controller is allowed for
// the origin and create CorsPolicy if it is (otherwise return null)
if (_allowed.All(a => !a.Match(originRequested).Success))
return null;
var policy = new CorsPolicy();
policy.Headers.Add("accept");
policy.Headers.Add("content-type");
policy.Origins.Add(originRequested);
policy.Methods.Add("GET");
policy.Methods.Add("POST");
policy.Methods.Add("PUT");
policy.Methods.Add("DELETE");
policy.Methods.Add("OPTIONS");
return policy;
}
假设请求来源为:http://sub.domain1.com且脚本位置为http://sub.domain1.com:8080。这在Chrome,IE,FireFox和Opera中完美运行,但在Safari中失败了。奇怪的是,Safari在浏览器控制台中提供的错误是
Failed to load resource: Origin http://sub.domain1.com is not allowed by Access-Control-Allow-Origin
可能会发生什么?
我观察到的另一个奇怪的是,如果我的原点和脚本位置都是http:sub.domain1.com:8080 CORS请求由Safari(并传递)发出,但其他浏览器正确地将其视为同一来源。
编辑:我发现如果我在GetPolicyForControllerAndOrigin
返回null的地方放置一个断点,它就永远不会被命中,并且会创建一个策略并正确返回。
编辑2 我检查了服务器对服务器对OPTIONS请求的响应,并发现响应中缺少标题Access-Control-Allow-Headers
和Access-Control-Allow-Origin
标头。此外,在请求方面,Safari未添加Host
标头。这需要吗?这就是它失败的原因吗?
答案 0 :(得分:2)
问题出在Safari的OPTIONS请求中。我只允许accept
和content-type
用于允许的标头,但Safari也要求origin
。