C# 如何使用Web API保护JSONP请求?
我在这里使用自定义JSONP格式化程序:C# 如何使用Web API保护JSONP请求?,c#,security,asp.net-web-api,jsonp,C#,Security,Asp.net Web Api,Jsonp,我在这里使用自定义JSONP格式化程序: 它与最新的Web API配合使用非常好。但是,我如何限制它,使其不适用于我的所有Web API服务,而仅适用于我添加了JSONP属性或其他内容的服务。关于如何将其仅保护到我选择的特定操作,您有什么想法吗?如果您可以按照希望作为JSONP返回的对象类型来考虑,您可以像这样更改格式化程序,只允许将特定类型序列化为JSONP public override bool CanWriteType(Type type) { // Check type he
它与最新的Web API配合使用非常好。但是,我如何限制它,使其不适用于我的所有Web API服务,而仅适用于我添加了JSONP属性或其他内容的服务。关于如何将其仅保护到我选择的特定操作,您有什么想法吗?如果您可以按照希望作为JSONP返回的对象类型来考虑,您可以像这样更改格式化程序,只允许将特定类型序列化为JSONP
public override bool CanWriteType(Type type)
{
// Check type here and return true only for the types you want to allow JSONP
return true;
}
如果无法进行基于类型的筛选,另一个选项是不将格式化程序添加到formatters集合中,并仅在要返回JSONP的操作方法中显式指定JsonpFormatter
return new HttpResponseMessage()
{
Content = new ObjectContent<MyType>(anInstanceOfMyType, new JsonpFormatter())
};
返回新的HttpResponseMessage()
{
Content=新的ObjectContent(instanceofmytype,new JsonpFormatter())
};
然而,一个缺点是,无论conneg提出了什么,它都只返回JSONP。如果您可以按照希望作为JSONP返回的对象类型来考虑,那么可以这样更改格式化程序,只允许将某些类型序列化为JSONP
public override bool CanWriteType(Type type)
{
// Check type here and return true only for the types you want to allow JSONP
return true;
}
如果无法进行基于类型的筛选,另一个选项是不将格式化程序添加到formatters集合中,并仅在要返回JSONP的操作方法中显式指定JsonpFormatter
return new HttpResponseMessage()
{
Content = new ObjectContent<MyType>(anInstanceOfMyType, new JsonpFormatter())
};
返回新的HttpResponseMessage()
{
Content=新的ObjectContent(instanceofmytype,new JsonpFormatter())
};
然而,一个缺点是,不管conneg提出了什么,这只会返回JSONP