AuthToken.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Dfs.WayneChina.SpsDataCourier
  6. {
  7. public class AuthToken
  8. {
  9. #region Standard OAuth token format
  10. /// <summary>
  11. /// The access token string issued by the authorization server.
  12. /// </summary>
  13. [JsonProperty("access_token")]
  14. public string AccessToken { get; set; }
  15. /// <summary>
  16. /// Token type, simply `bearer`.
  17. /// </summary>
  18. [JsonProperty("token_type")]
  19. public string TokenType { get; set; }
  20. /// <summary>
  21. /// Duration of the time the access token is granted for, in total seconds.
  22. /// </summary>
  23. [JsonProperty("expires_in")]
  24. public int ExpiresIn { get; set; }
  25. /// <summary>
  26. /// Access token to be used to get a new token, optional.
  27. /// </summary>
  28. [JsonProperty("refresh_token")]
  29. public string RefreshToken { get; set; }
  30. /// <summary>
  31. /// The error information
  32. /// </summary>
  33. [JsonProperty("error")]
  34. public string Error { get; set; }
  35. #endregion
  36. #region Custom extension
  37. /// <summary>
  38. /// When the token is retrieve from the auth server, to simplify, set the local received time.
  39. /// </summary>
  40. public DateTime TokenRetrievedTime { get; set; }
  41. /// <summary>
  42. /// Check if the token has expired or not, if not, then it's valid.
  43. /// </summary>
  44. /// <returns>valid or not</returns>
  45. public bool IsTokenValid()
  46. {
  47. //Current Time = Now + 5 minutes, Expiry Time = token retrieved time + expiry time span
  48. //If current time is not later than expiry time, then the token is valid
  49. if (DateTime.Now + new TimeSpan(0, 5, 0) < TokenRetrievedTime + new TimeSpan(0, 0, ExpiresIn))
  50. return true;
  51. return false;
  52. }
  53. #endregion
  54. }
  55. }