我正在寻找一种简单的安全解决方案,该Web解决方案的Web API正文内容不会简单地显示给希望通过Fiddler或其他内容拦截请求的所有人。我受到限制,因为我不能使用SSL。我已经实现了HMAC类型的身份验证,并希望通过在客户端上创建主体内容的加密并将该请求发送到服务器来使它进一步前进,否则服务器随后将解密主体并按预期进行操作,但是解密。我为服务器端HMAC和客户端上的delagatingHandler使用了过滤器。

我对使用http请求不是很熟悉,也不完全了解如何截取所有正文内容,然后将其加密并放回httpcontent中。

任何想法或帮助将不胜感激。

最佳答案

为了在WEB API中发生模型映射之前解密数据,您可以劫持AuthorizeAttribute,因为ActionFilterAttribute在模型映射之后发生。

我知道AuthorizeAttribute是出于另一个原因,但是劫持它对我来说效果很好(我想解压缩zip内容)。

    public class DecryptAttribute : AuthorizeAttribute
    {
      public override void OnAuthorization(HttpActionContext actionContext)
      {
              actionContext.Request.Content =  DecryptContect(actionContext.Request.Content);
      }
    }


然后使用此属性装饰所有WebAPI控制器。

为了压缩并嵌入到主体,我使用了委托处理程序

    public class EncryptHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>((responseToCompleteTask) =>
        {
            HttpResponseMessage response = responseToCompleteTask.Result;
                response.Content = new EncryptContent(response.Content);

            return response;
        },
        TaskContinuationOptions.OnlyOnRanToCompletion);
    }
}


然后只需注册

GlobalConfiguration.Configuration.MessageHandlers.Add(new EncryptHandler());

10-06 16:02
查看更多