I'm building a WebAPI for an external mobile company ( all I do is exposing services).
Right now , our DB uses non-encrypted values for columns like :
- ObjectID
- PolicyID
- etc..
But now , when we expose it , and I need to encrypt values. ( only server can decrypt values , the mobile company doesn't care about the actual values).
I don't want to start digest every Stored Procedure response manually and replace values with encrypted ones. ( don't forget that our internal server does not uses encrypted values - it uses data regularly).
OK Here's a live example :
I have this controller code :
[HttpGet]
[ActionName("test2")]
public HttpResponseMessage test2(int id)
{
var Data = GetDataFromSource_1();
// or from GetDataFromSource_2();
return Request.CreateResponse(HttpStatusCode.OK, Data);
}
Where GetDataFromSource_1
is via dynamic list (just to simulate a source)
public IEnumerable GetDataFromSource_1()
{
List<dynamic> lst = new List<dynamic>();
lst.Add(new
{
objId = 1,
myOtherColumn = 5
});
lst.Add(new
{
objId = 2,
myOtherColumn = 8
});
return lst;
}
And
GetDataFromSource_2
is via DataTable ( just to simulate another source)
public DataTable GetDataFromSource_2()
{
DataTable dt = new DataTable("myTable");
dt.Columns.Add("objId", typeof(int));
dt.Columns.Add("myOtherColumn", typeof(int));
DataRow row = dt.NewRow();
row["objId"] = 1;
row["myOtherColumn"] = 5;
dt.Rows.Add(row);
row = dt.NewRow();
row["objId"] = 2;
row["myOtherColumn"] = 8;
dt.Rows.Add(row);
return dt;
}
Both yields this json response:
{"Result":{"Success":true,"Message":""},"Data":[{"objId":1,"myOtherColumn":5},{"objId":2,"myOtherColumn":8}]}
Question
How (and where) can I scan the content of the response ( which is going to be sent) and replace for every column in ( and only for them) :
- ObjectID
- PolicyID
- etc..
to an encrypted value ?
For example :
I Would like the output to be :
{
"Result": {
"Success": true,
"Message": ""
},
"Data": [{
"objId": "XXX_the_encrypted_valueXXX",
"myOtherColumn": 5
}, {
"objId": "XXX_the_encrypted_valueXXX": ,
"myOtherColumn": 8
}]
}
(where "XXX_the_encrypted_valueXXX"
is an encrypted value of the old value.)
NB it's assumable that I have Utils.Encrypt(string st)
method.
Also , we dont have entities so i can not decorate an entity . I need to plugin when the json is created