Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Web API保护JSONP请求?_C#_Security_Asp.net Web Api_Jsonp - Fatal编程技术网

C# 如何使用Web API保护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

我在这里使用自定义JSONP格式化程序:


它与最新的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