我们在ServiceStack 4中使用宁静的API的CORS功能遇到了一些麻烦。
由于SS会话位于cookie中,因此我们希望将cookie绑定到api,因此在命中该API的角度客户端中,我们使用“ WithCredentials” = true进行AJAX调用。
由于Chrome(至少)不喜欢带有WithCredentials的Access-Control-Allow-Origin通配符,因此我们在Access-Control-Allow-Origin标头中添加了预请求过滤器,以回显请求者的来源,如下所示:
private void ConfigureCors()
{
Plugins.Add(new CorsFeature(
allowedHeaders: "Content-Type",
allowCredentials: true,
allowedOrigins: ""));
PreRequestFilters.Add((httpReq, httpRes) =>
{
string origin = httpReq.Headers.Get("Origin");
if (origin != null)
{
httpRes.AddHeader(HttpHeaders.AllowOrigin, origin);
}
else
{
// Add the dev localhost header.
httpRes.AddHeader(HttpHeaders.AllowOrigin, "http://localhost:9000");
}
});
PreRequestFilters.Add((httpReq, httpRes) =>
{
//Handles Request and closes Responses after emitting global HTTP Headers
if (httpReq.Verb == "OPTIONS")
{
httpRes.EndRequest();
}
});
}
但是,我们在OPTIONS请求上遇到了麻烦,因为当请求结束时,SS服务不会向后发出Access-Control-Allow-Origin标头。这使Chrome拒绝了通话。
我们尝试将显式标头放入OPTIONS的请求前过滤器中,但仍未为OPTIONS调用返回ACAO标头:
PreRequestFilters.Add((httpReq, httpRes) =>
{
//Handles Request and closes Responses after emitting global HTTP Headers
if (httpReq.Verb == "OPTIONS")
{
httpRes.AddHeader(HttpHeaders.AllowOrigin, "*");
httpRes.EndRequest();
}
});
似乎必须已经解决了这个问题,但是我们在StackOverflow上找不到类似的东西。
我们使用OPTIONS预请求过滤器做错什么了吗?为什么不返回Access-Control-Allow-Origin标头?
c#大神给出的解决方案
您应在allowOriginWhitelist
中添加列入白名单的域,例如:
Plugins.Add(new CorsFeature(allowedHeaders:"Content-Type",
allowCredentials:true,
allowOriginWhitelist:new[]{"http://localhost:9000"}));
v4.0.35中引入了一个问题,因为PreRequestFilters were being written in Custom HttpHandlers导致CorsFeature两次写出Access-Control-Allow-Origin标头,导致浏览器拒绝它。现在,已解决此问题in this commit,可从v4.0.36 +(现在为available on MyGet)获得。
最新版本已部署到http://test.servicestack.net演示中,该演示在此jsbin中显示了与ServiceStack的跨域身份验证:http://jsbin.com/korijigucu/1/edit
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<script>
var apiBase = "http://test.servicestack.net";
var app = angular.module('app', []);
app.run(['$rootScope', '$http', function ($rootScope, $http) {
$rootScope.success = "running...";
$http
.post(apiBase + '/auth/credentials',
{ "UserName": "test", "Password": "test" },
{ withCredentials: true })
.success(function (data) {
$rootScope.success = "Login successful: " + JSON.stringify(data);
})
.error(function (data, status, headers, config) {
$rootScope.error = 'ERR:login';
});
}]);
</script>
</head>
<body>
<div style='color:green'>{{success}}</div>
<div style='color:red'>{{error}}</div>
</body>
</html>
CorsFeature registration in above Test project的源代码:
Plugins.Add(new CorsFeature(
allowOriginWhitelist: new[] {
"http://localhost", "http://localhost:56500",
"http://test.servicestack.net", "http://null.jsbin.com" },
allowCredentials: true,
allowedHeaders: "Content-Type, Allow, Authorization"));
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
将python scikit学习模型导出到pmml - python我想将python scikit-learn模型导出到PMML。哪个python软件包最合适?我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。 python大神给出的解决方案 SkLearn2PMML是 JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和…
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …
在熊猫中,如何从单词列表或单词集中选择数据框中的短语? - python在Python3和熊猫中,我具有数据框:df_projetos_api_final.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 93631 entries, 1 to 93667 Data columns (total 21 columns): AnoMat…
为什么此javascript使用getTime函数显示负数? - javascript我无法弄清楚,希望有一个简单的解决方案,但看不到该脚本在哪里失败。该脚本是wp插件的一部分,但是如果失败,则没有wp代码。它所做的是算到结束日期并在屏幕上打印出来。该行:数量=(dateFuture.getTime()-dateNow.getTime()+ 5);显示负数,所以不可能,将来的日期是“ 2014-10-23 23:00:00”,而我所在国家/地…