| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- using GreenTree.Nachtragsmanagement.Core.Domain.User;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- namespace GreenTree.Nachtragsmanagement.Core.Authentication
- {
- public class UserHelper : IUserHelper
- {
- /// <summary>
- /// Evaluates the current user from request cookies
- /// </summary>
- public User FromCookies()
- {
- return FromCookies(2);
- }
- /// <summary>
- /// Evaluates the current user from request cookies
- /// </summary>
- /// <param name="expirationHoursIncrease">The hours the authCookie will be increased if it exists.</param>
- public User FromCookies(int expirationHoursIncrease)
- {
- if (HttpContext.Current == null || HttpContext.Current.Session == null) return null;
- var authCookie = HttpContext.Current.Request.Cookies["auth"];
- if (authCookie == null)
- return null;
- var user = JsonConvert.DeserializeObject<User>(StaticHelper.DecompressString(authCookie.Value));
- if (user != null)
- {
- authCookie.Expires = DateTime.Now.AddHours(expirationHoursIncrease);
- HttpContext.Current.Response.Cookies.Set(authCookie);
- }
- return user;
- }
- /// <summary>
- /// Writes the current user to response cookies
- /// </summary>
- /// <param name="user">The current authenticated user.</param>
- public void ToCookies(User user)
- {
- ToCookies(user, false);
- }
- /// <summary>
- /// Writes the current user to response cookies
- /// </summary>
- /// <param name="user">The current authenticated user.</param>
- /// <param name="setRequestCookie">Set also the request cookie for further authorization.</param>
- public void ToCookies(User user, bool setRequestCookie)
- {
- if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
- var authCookie = HttpContext.Current.Request.Cookies["auth"];
- var userJson = JsonConvert.SerializeObject(user);
- authCookie = new HttpCookie("auth", StaticHelper.CompressString(userJson))
- {
- Expires = authCookie.Expires
- };
- HttpContext.Current.Response.Cookies.Set(authCookie);
- if (setRequestCookie)
- HttpContext.Current.Request.Cookies.Set(authCookie);
- }
- /// <summary>
- /// Writes the current user to response cookies
- /// </summary>
- /// <param name="user">The current authenticated user.</param>
- /// <param name="expirationDate">The cookie expiration date.</param>
- public void ToCookies(User user, DateTime expirationDate)
- {
- ToCookies(user, expirationDate, false);
- }
- /// <summary>
- /// Writes the current user to response cookies
- /// </summary>
- /// <param name="user">The current authenticated user.</param>
- /// <param name="expirationDate">The cookie expiration date.</param>
- /// <param name="setRequestCookie">Set also the request cookie for further authorization.</param>
- public void ToCookies(User user, DateTime expirationDate, bool setRequestCookie)
- {
- if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
- var authCookie = HttpContext.Current.Request.Cookies["auth"];
- var userJson = JsonConvert.SerializeObject(user);
- authCookie = new HttpCookie("auth", StaticHelper.CompressString(userJson))
- {
- Expires = expirationDate
- };
- HttpContext.Current.Response.Cookies.Set(authCookie);
- if (setRequestCookie)
- HttpContext.Current.Request.Cookies.Set(authCookie);
- }
- /// <summary>
- /// Clears the authentication cookie
- /// </summary>
- public void ClearCookie()
- {
- if (HttpContext.Current == null || HttpContext.Current.Session == null) return;
- var authCookie = HttpContext.Current.Request.Cookies["auth"];
- authCookie.Expires = DateTime.Now.AddHours(-1);
- HttpContext.Current.Response.Cookies.Set(authCookie);
- }
- }
- }
|