Servicestack-为验证插件的错误响应启用CORS?
由于验证失败(400次返回),我遇到CORS头没有正确设置(所有CORS头都丢失)。我正在使用验证功能插件,但我没有使用CORS插件。相反,我在逐个服务的基础上使用EnableCors属性 来自我的auth端点的401响应的标题设置正确-我想我是通过以下方式实现的:Servicestack-为验证插件的错误响应启用CORS?,cors,servicestack,Cors,servicestack,由于验证失败(400次返回),我遇到CORS头没有正确设置(所有CORS头都丢失)。我正在使用验证功能插件,但我没有使用CORS插件。相反,我在逐个服务的基础上使用EnableCors属性 来自我的auth端点的401响应的标题设置正确-我想我是通过以下方式实现的: typeof(Authenticate).AddAttributes( new RestrictAttribute(RequestAttributes.HttpPost | RequestAttributes
typeof(Authenticate).AddAttributes(
new RestrictAttribute(RequestAttributes.HttpPost | RequestAttributes.HttpOptions),
new EnableCorsAttribute(allowedMethods: "POST", allowedHeaders: "Authorization,Content-Type"));
验证失败时,服务响应中缺少CORS标头:
[EnableCors(allowedMethods:"POST", allowedHeaders: "Authorization,Content-Type")]
public sealed class MyService : Service
{
...
[Authenticate]
public CustomResponse Post(CustomRequest request)
{
//some logic
return result;
}
...
}
通过Dto上的属性注册路由:
[Route("/myroute","POST, OPTIONS")]
public class CustomRequest: IReturn<CustomResponse>
{
...
}
我特别需要这个响应头,因为如果没有设置,响应体将被一些浏览器阻止。再次感谢您的反馈。
[EnableCors]
是一个请求筛选器属性,在全局筛选器之后执行,其中使用全局请求筛选器执行as验证筛选器。要在验证筛选器之前执行[EnableCors]
,它需要一个优先级我使用的是4.5.6,并且优先级是只读的-这是否正在更改?@liz它已经在v4.5.7中转换为a,这是正确的。
this.Plugins.Add(new ValidationFeature()
{
ErrorResponseFilter = (validationRes, resp) =>
{
if (resp is HttpError)
{
var obj = resp as HttpError;
obj.Headers.Add("Access-Control-Allow-Origin", "*");
}
return resp;
}
});
typeof(MyService).AddAttributes(
new EnableCorsAttribute(allowedMethods: "POST",
allowedHeaders: "Authorization,Content-Type") {
Priority = -1
}
);