AuthToken.cs 2.4 KB

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