【问题标题】:Prepend to a WCF Service output附加到 WCF 服务输出
【发布时间】:2011-12-12 18:52:38
【问题描述】:

我正在使用 ASP.NET 4 WCF 服务进行一些数据事务。为了防止 CSRF(跨站点请求伪造),我想在输出中添加一些数据。关于如何做到这一点的任何建议?

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class TestService : ServiceBase
{
    [WebGet(
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/test.json")
    ]
    public MyResponse Test ()
    {
        MyResponse resp;
        try
        {
            Response.Write("for(;;){}"); // <-- Fix needed
            resp = new MyResponse();
        }
        catch (Exception ex)
        {
            AjaxException aex = new AjaxException() { 
                message = string.Format("Test failed. Exception: {0}.", ex.Message)
            };
            throw new WebFaultException<AjaxException>(aex, HttpStatusCode.InternalServerError);
        }
        return resp;
    }
}

[DataContract]
public class MyResponse {
    public MyResponse() { }
    [DataMember()]
    public long time = ServiceUtility.Convert(DateTime.Now);
    [DataMember()]
    public string secret { get; set; }
}

【问题讨论】:

    标签: asp.net wcf csrf


    【解决方案1】:

    我会建议你避免在 WCF 操作上使用 HTTP GET 方法来改变状态。据我所知,当前的浏览器不允许使用 JSON 内容类型进行跨站点 POST 请求 - 所以这应该可以防止 CSRF 攻击。

    为了提高安全性,您可以检查 HTTP Referrer 标头以查看服务调用是否来自允许的站点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多