简体   繁体   English

加密Web API请求正文内容并在服务器上解密

[英]Encrypt a web api request body content and decrypt on server

I am looking to create a simple security solution for which web API body content is not simply displayed to all whom wishes to see via intercepting the request with Fiddler or something. 我正在寻找一种简单的安全解决方案,该Web解决方案的Web API正文内容不会简单地显示给希望通过Fiddler或其他工具拦截请求的所有人。 I am restricted in that I cannot use a SSL. 我受到限制,因为我不能使用SSL。 I have already implemented a HMAC type of authentication and am wanting to take it a step further by creating a encrytpion of the body content on the client and sending that request to server were the server will then decrypt the body and forward to action as expected but decrypted. 我已经实现了HMAC类型的身份验证,并希望通过在客户端上创建主体内容的加密并将该请求发送到服务器来使它进一步前进,否则服务器随后将解密主体并按预期进行操作,但是解密。 I used a filter for the server side HMAC and a delagatingHandler on the client. 我为服务器端HMAC和客户端上的delagatingHandler使用了过滤器。

I am not very familiar with working with http requests and do not fully understand how I might intercept all body content then encrypt it and put it back into the httpcontent. 我对使用HTTP请求不是很熟悉,并且不完全了解如何截取所有正文内容,然后将其加密并放回httpcontent中。

Any ideas or help would be greatly appreciated. 任何想法或帮助将不胜感激。

In order to decrypt data before Model Mapping occurs in WEB API you can Hijack the AuthorizeAttribute because ActionFilterAttribute occurs after model mapping. 为了在WEB API中发生模型映射之前解密数据,您可以劫持AuthorizeAttribute,因为ActionFilterAttribute 模型映射之后发生。

I know that the AuthorizeAttribute is meant for another reason , but hijacking it worked perfectly for me (I wanted to decompress zip content). 我知道AuthorizeAttribute是出于另一个原因,但是劫持它对我来说非常有效(我想解压缩zip内容)。

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

Then Decorate all your WebAPI controllers with this attribute. 然后使用此属性装饰所有WebAPI控制器。

In order to compress and embed to body i used a Delegating Handler 为了压缩并嵌入到主体,我使用了委托处理程序

    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);
    }
}

Then just register it 然后只需注册

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM