UserHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /// Evaluates the current user from request cookies
  17. /// </summary>
  18. public User FromCookies()
  19. {
  20. return FromCookies(2);
  21. }
  22. /// <summary>
  23. /// Evaluates the current user from request cookies
  24. /// </summary>
  25. /// <param name="expirationHoursIncrease">The hours the authCookie will be increased if it exists.</param>
  26. public User FromCookies(int expirationHoursIncrease)
  27. {
  28. if (HttpContext.Current == null || HttpContext.Current.Session == null) return null;
  29. var authCookie = HttpContext.Current.Request.Cookies["auth"];
  30. if (authCookie == null)
  31. return null;
  32. var user = JsonConvert.DeserializeObject<User>(StaticHelper.DecompressString(authCookie.Value));
  33. if (user != null)
  34. {
  35. authCookie.Expires = DateTime.Now.AddHours(expirationHoursIncrease);
  36. HttpContext.Current.Response.Cookies.Set(authCookie);
  37. }
  38. return user;
  39. }
  40. /// <summary>
  41. /// Writes the current user to response cookies
  42. /// </summary>
  43. /// <param name="user">The current authenticated user.</param>
  44. public void ToCookies(User user)
  45. {
  46. ToCookies(user, false);
  47. }
  48. /// <summary>
  49. /// Writes the current user to response cookies
  50. /// </summary>
  51. /// <param name="user">The current authenticated user.</param>
  52. /// <param name="setRequestCookie">Set also the request cookie for further authorization.</param>
  53. public void ToCookies(User user, bool setRequestCookie)
  54. {
  55. if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
  56. var authCookie = HttpContext.Current.Request.Cookies["auth"];
  57. var userJson = JsonConvert.SerializeObject(user);
  58. authCookie = new HttpCookie("auth", StaticHelper.CompressString(userJson))
  59. {
  60. Expires = authCookie.Expires
  61. };
  62. HttpContext.Current.Response.Cookies.Set(authCookie);
  63. if (setRequestCookie)
  64. HttpContext.Current.Request.Cookies.Set(authCookie);
  65. }
  66. /// <summary>
  67. /// Writes the current user to response cookies
  68. /// </summary>
  69. /// <param name="user">The current authenticated user.</param>
  70. /// <param name="expirationDate">The cookie expiration date.</param>
  71. public void ToCookies(User user, DateTime expirationDate)
  72. {
  73. ToCookies(user, expirationDate, false);
  74. }
  75. /// <summary>
  76. /// Writes the current user to response cookies
  77. /// </summary>
  78. /// <param name="user">The current authenticated user.</param>
  79. /// <param name="expirationDate">The cookie expiration date.</param>
  80. /// <param name="setRequestCookie">Set also the request cookie for further authorization.</param>
  81. public void ToCookies(User user, DateTime expirationDate, bool setRequestCookie)
  82. {
  83. if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
  84. var authCookie = HttpContext.Current.Request.Cookies["auth"];
  85. var userJson = JsonConvert.SerializeObject(user);
  86. authCookie = new HttpCookie("auth", StaticHelper.CompressString(userJson))
  87. {
  88. Expires = expirationDate
  89. };
  90. HttpContext.Current.Response.Cookies.Set(authCookie);
  91. if (setRequestCookie)
  92. HttpContext.Current.Request.Cookies.Set(authCookie);
  93. }
  94. /// <summary>
  95. /// Clears the authentication cookie
  96. /// </summary>
  97. public void ClearCookie()
  98. {
  99. if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
  100. var authCookie = HttpContext.Current.Request.Cookies["auth"];
  101. if (authCookie == null) return;
  102. authCookie.Expires = DateTime.Now.AddHours(-1);
  103. HttpContext.Current.Response.Cookies.Set(authCookie);
  104. }
  105. }
  106. }