public IActionResult PunchOut([FromQuery] string timeZone, [FromQuery] string latitude, [FromQuery] string longitude, [FromQuery] string sourceHardware, [FromQuery] string sourceOperatingSystem, [FromQuery] string sourceOperatingSystemVersion, [FromQuery] string sourceBrowser, [FromQuery] string sourceBrowserVersion) { var sourceHostname = ""; // Leave blank // Attempt to get the client IP address. var sourceIpAddress = $"{HttpContext.Connection.RemoteIpAddress}"; var currentUser = CurrentUser(); // Prevent double submission. var submission = _memoryCache.Get($"submission.punchout.{currentUser.Id}") as bool?; if (submission.HasValue) { return(BadRequest("Cannot punch out twice within 5 seconds.")); } try { var repo = new PunchRepository(_context); var punch = repo.PunchOut( currentUser, "", timeZone, latitude, longitude, sourceHardware, sourceHostname, sourceIpAddress, sourceOperatingSystem, sourceOperatingSystemVersion, sourceBrowser, sourceBrowserVersion); // Record the submission. _memoryCache.Set($"submission.punchout.{currentUser.Id}", true, DateTime.UtcNow.AddSeconds(5)); return(Ok(punch)); } catch (DbUpdateException ex) { Trace.TraceError(ex.ToString()); return(BadRequest(ex.Message)); } catch (Exception ex) { Trace.TraceError(ex.ToString()); return(BadRequest(ex.Message)); } }
public IHttpActionResult PunchOut(ODataActionParameters parameters) { var timezone = (string)parameters["OutAtTimeZone"]; var latitudeForOutAt = (string)parameters["LatitudeForOutAt"]; var longitudeForOutAt = (string)parameters["LongitudeForOutAt"]; var sourceHardware = (string)parameters["SourceHardware"]; var sourceHostname = HttpContext.Current.Request.UserHostName; var sourceIpAddress = HttpContext.Current.Request.UserHostAddress; var sourceOperatingSystem = (string)parameters["SourceOperatingSystem"]; var sourceOperatingSystemVersion = (string)parameters["SourceOperatingSystemVersion"]; var sourceBrowser = (string)parameters["SourceBrowser"]; var sourceBrowserVersion = (string)parameters["SourceBrowserVersion"]; var currentUser = CurrentUser(); // Prevent double submission. var submission = cache.Get($"submission.punchout.{currentUser.Id}") as bool?; if (submission.HasValue) { return(BadRequest("Cannot punch out twice within 5 seconds.")); } try { var punch = repo.PunchOut( currentUser, "", timezone, latitudeForOutAt, longitudeForOutAt, sourceHardware, sourceHostname, sourceIpAddress, sourceOperatingSystem, sourceOperatingSystemVersion, sourceBrowser, sourceBrowserVersion); // Record the submission. var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.UtcNow.AddSeconds(5); cache.Set($"submission.punchout.{currentUser.Id}", true, policy); return(Created(punch)); } catch (DbEntityValidationException e) { string message = ""; foreach (var eve in e.EntityValidationErrors) { foreach (var ve in eve.ValidationErrors) { message += string.Format("{0} has error '{1}'; ", ve.PropertyName, ve.ErrorMessage); } } return(Content(HttpStatusCode.BadRequest, message)); } catch (Exception ex) { return(Content(HttpStatusCode.BadRequest, ex.Message)); } }
public IHttpActionResult PunchOut([FromUri] string timeZone, [FromUri] string latitude, [FromUri] string longitude, [FromUri] string sourceHardware, [FromUri] string sourceOperatingSystem, [FromUri] string sourceOperatingSystemVersion, [FromUri] string sourceBrowser, [FromUri] string sourceBrowserVersion) { string sourceHostname = ""; string sourceIpAddress = ""; try { // Attempt to get the client hostname. sourceHostname = HttpContext.Current.Request.UserHostName; } catch { } try { // Attempt to get the client IP address. sourceIpAddress = HttpContext.Current.Request.UserHostAddress; } catch { } var currentUser = CurrentUser(); // Prevent double submission. var submission = cache.Get($"submission.punchout.{currentUser.Id}") as bool?; if (submission.HasValue) { return(BadRequest("Cannot punch out twice within 5 seconds.")); } try { var punch = repo.PunchOut( currentUser, "", timeZone, latitude, longitude, sourceHardware, sourceHostname, sourceIpAddress, sourceOperatingSystem, sourceOperatingSystemVersion, sourceBrowser, sourceBrowserVersion); // Record the submission. var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.UtcNow.AddSeconds(5); cache.Set($"submission.punchout.{currentUser.Id}", true, policy); return(Ok(punch)); } catch (DbEntityValidationException ex) { string message = ""; foreach (var eve in ex.EntityValidationErrors) { foreach (var ve in eve.ValidationErrors) { message += string.Format("{0} has error '{1}'; ", ve.PropertyName, ve.ErrorMessage); } } Trace.TraceError(ex.ToString()); Trace.TraceError(ex.Message); return(BadRequest(message)); } catch (Exception ex) { Trace.TraceError(ex.ToString()); return(BadRequest(ex.Message)); } }