UserHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.IO.Compression;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. namespace GreenTree.Nachtragsmanagement.Core.Authentication
  12. {
  13. public class UserHelper : IUserHelper
  14. {
  15. /// <summary>
  16. /// Default serializer settings
  17. /// </summary>
  18. private static JsonSerializerSettings _jsonSerializerSettigs = new JsonSerializerSettings
  19. {
  20. ReferenceLoopHandling = ReferenceLoopHandling.Ignore
  21. };
  22. /// <summary>
  23. /// Evaluates the current user from request cookies
  24. /// </summary>
  25. public User FromCookiesOrSession()
  26. {
  27. return FromCookiesOrSession(8);
  28. }
  29. /// <summary>
  30. /// Evaluates the current user from request cookies
  31. /// </summary>
  32. /// <param name="expirationHoursIncrease">The hours the authCookie will be increased if it exists.</param>
  33. public User FromCookiesOrSession(int expirationHoursIncrease)
  34. {
  35. if (HttpContext.Current == null || HttpContext.Current.Session == null) return null;
  36. User user = null;
  37. if (HttpContext.Current.Session["auth"] != null)
  38. user = HttpContext.Current.Session["auth"] as User;
  39. if (user != null)
  40. return user;
  41. var authCookie = HttpContext.Current.Request.Cookies["auth"];
  42. if (authCookie == null)
  43. return null;
  44. user = JsonConvert.DeserializeObject<User>(StaticHelper.DecompressString(authCookie.Value));
  45. if (user != null)
  46. {
  47. authCookie.Expires = DateTime.Now.AddHours(expirationHoursIncrease);
  48. HttpContext.Current.Response.Cookies.Set(authCookie);
  49. }
  50. return user;
  51. }
  52. /// <summary>
  53. /// Writes the current user to response cookies
  54. /// </summary>
  55. /// <param name="user">The current authenticated user.</param>
  56. public void ToCookiesAndSession(User user)
  57. {
  58. ToCookiesAndSession(user, false);
  59. }
  60. /// <summary>
  61. /// Writes the current user to response cookies
  62. /// </summary>
  63. /// <param name="user">The current authenticated user.</param>
  64. /// <param name="setRequestCookie">Set also the request cookie for further authorization.</param>
  65. public void ToCookiesAndSession(User user, bool setRequestCookie)
  66. {
  67. if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
  68. var authCookie = HttpContext.Current.Request.Cookies["auth"];
  69. var userJson = JsonConvert.SerializeObject(user, _jsonSerializerSettigs);
  70. authCookie = new HttpCookie("auth", StaticHelper.CompressString(userJson))
  71. {
  72. Expires = authCookie.Expires
  73. };
  74. HttpContext.Current.Response.Cookies.Set(authCookie);
  75. if (setRequestCookie)
  76. HttpContext.Current.Request.Cookies.Set(authCookie);
  77. HttpContext.Current.Session["auth"] = user;
  78. }
  79. /// <summary>
  80. /// Writes the current user to response cookies
  81. /// </summary>
  82. /// <param name="user">The current authenticated user.</param>
  83. /// <param name="expirationDate">The cookie expiration date.</param>
  84. public void ToCookiesAndSession(User user, DateTime expirationDate)
  85. {
  86. ToCookiesAndSession(user, expirationDate, false);
  87. }
  88. /// <summary>
  89. /// Writes the current user to response cookies
  90. /// </summary>
  91. /// <param name="user">The current authenticated user.</param>
  92. /// <param name="expirationDate">The cookie expiration date.</param>
  93. /// <param name="setRequestCookie">Set also the request cookie for further authorization.</param>
  94. public void ToCookiesAndSession(User user, DateTime expirationDate, bool setRequestCookie)
  95. {
  96. if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
  97. var authCookie = HttpContext.Current.Request.Cookies["auth"];
  98. var userJson = JsonConvert.SerializeObject(user, _jsonSerializerSettigs);
  99. authCookie = new HttpCookie("auth", StaticHelper.CompressString(userJson))
  100. {
  101. Expires = expirationDate
  102. };
  103. HttpContext.Current.Response.Cookies.Set(authCookie);
  104. if (setRequestCookie)
  105. HttpContext.Current.Request.Cookies.Set(authCookie);
  106. HttpContext.Current.Session["auth"] = user;
  107. }
  108. /// <summary>
  109. /// Clears the authentication cookie
  110. /// </summary>
  111. public void ClearCookieAndSession()
  112. {
  113. if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
  114. var authCookie = HttpContext.Current.Request.Cookies["auth"];
  115. if (authCookie == null) return;
  116. authCookie.Expires = DateTime.Now.AddHours(-1);
  117. HttpContext.Current.Response.Cookies.Set(authCookie);
  118. HttpContext.Current.Session.Remove("auth");
  119. }
  120. }
  121. }