我想将ASP.NET Core的所有JSON响应带到众所周知的字符串")]}',n"
,以防止XSSI攻击。(有关更多详细信息
如何实现?我认为我应该使用过滤器或中间件,但不能完全解决正确的方法。
是的,有可能使用中间件或类似的过滤器:
public class EditResponseFilter : Attribute, IAsyncResourceFilter
{
private const string _prefix = ")]}',n";
public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
var originBody = context.HttpContext.Response.Body;
var newBody = new MemoryStream();
//Body replacement is needed to make the response stream readable
context.HttpContext.Response.Body = newBody;
await next();
newBody.Seek(0, SeekOrigin.Begin);
string json = new StreamReader(newBody).ReadToEnd();
context.HttpContext.Response.Body = originBody;
await context.HttpContext.Response.WriteAsync(_prefix + json);
}
}
我想拥有一个类似的东西,而不必对返回JSON的API操作方法放置一个属性。有没有办法检测我们是否返回JSON响应并放置前缀(或后缀(?