WebApi项目需要在输出Json时对Json进行编码处理。考虑使用MVC 的AOP
查阅文档需要HttpMessageHandle
新建类CryptDelegatingHandler
public class CryptDelegatingHandler:DelegatingHandler
{
protected async override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
bool jsonNeedCrypt =Convert.ToBoolean(FastFoodMvcApi.Common.ConfigList.GetValueFromConfig("JsonNeedCrypt"));
if (jsonNeedCrypt)
response.Content =new StringContent(JsonCrypt.EncryptJsonString(response.Content.ReadAsStringAsync().Result));
//解密测试
//Console.Write(JsonCrypt.DecryptJsonString(response.Content.ReadAsStringAsync().Result));
return response;
}
}
加入WebApiConfig中
Register
config.MessageHandlers.Add(new CryptDelegatingHandler());
详细原理转http://www.cnblogs.com/r01cn/archive/2012/12/07/2807059.html
本文介绍了一个WebApi项目中实现JSON输出加密的方法。通过创建CryptDelegatingHandler类并继承DelegatingHandler,实现了对响应内容的加密处理。该处理依赖于配置项JsonNeedCrypt来决定是否开启加密。

被折叠的 条评论
为什么被折叠?



