Encrypt request / response in MVC4 WebApi
I have an old api that receives an encrypted request and encrypts the response after it completes. I am trying to switch this to mvc4 webapi and it went smoothly until I hit this encryption. I need to decrypt the request when it arrives, so that mvc will act on it as expected. Also, once the process is complete, encrypt the response before sending it. I don't want to put parts of encryption in every action.
Note: The body is still properly formatted as a separate element, so I would push the whole thing to one action with a selector, but would prefer a more correct rest style implementation.
source to share
You should be able to do this with a MessageHandler. There are some examples on how to create MessageHandlers in WebAPIContrib
source to share
you can implement your own linking device
public class DecObjModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
//make a instance of your object
var myObj = new DecObj()
//bind the properties from my obj
myObj.Title= bindingContext
.ValueProvider
.GetValue("Title") // The Property name sent from the browser
.ToString();
/* then the property you want to decrypt */
var encBody = bindingContext
.ValueProvider
.GetValue("EncBody") // The Property name sent from the browser
.ToString();
/* decryption logic here to the encBody then after assign the decrypted value to myObj*/
return myObj;
}
and then you register ModelBinder
to Global.asx in Application_Start with:ModelBinders.Binders.Add(typeof(DecObj), new DecObjModelBinder());
source to share