I want to convert all dates received from client into UTC before processing them in server side code. I am thinking to use ActionFilter where I want to find all the date fields from Request payload and convert those into UTC dates. I know Reflection helps in finding properties by it's type but it doesn't work in case of Anonymous objects.
Below is my code sample
//convert body to object
var result = JsonConvert.DeserializeObject<object>(rawRequest);
var type = result.GetType();
//get collection of properties from object
var properties = result.GetType().GetProperties();
foreach (var property in properties)
{
//if data type is DateTime then convert into UTC
if (property.PropertyType == typeof(DateTime?) || property.PropertyType == typeof(DateTime))
{
string TimeZoneId = "India Standard Time";
if (HttpContext.Current.Request.Headers["TimeZoneId"] != null)
{
TimeZoneId = HttpContext.Current.Request.Headers.GetValues("TimeZoneId").FirstOrDefault();
}
var localTimeZone = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId);
var Date = DateTime.SpecifyKind(((DateTime?)property.GetValue(result, null)) ?? DateTime.Now, DateTimeKind.Unspecified);
var localTime = TimeZoneInfo.ConvertTimeToUtc(Date, localTimeZone);
}
}