127 lines
3.6 KiB
C#
127 lines
3.6 KiB
C#
namespace LaaProductionWeb.App_Infrastructure
|
|
{
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
using System.Net;
|
|
using System.Web;
|
|
|
|
public class SessionUser
|
|
{
|
|
public SessionUser()
|
|
=> this.UserId = -1;
|
|
|
|
public SessionUser(int userId, DateTime expires)
|
|
{
|
|
this.UserId = userId;
|
|
this.Expires = expires;
|
|
}
|
|
|
|
public int UserId { get; }
|
|
|
|
public DateTime Expires { get; }
|
|
|
|
public bool IsExpired => this.Expires < DateTime.Now.AddMinutes(30) || this.Expires > DateTime.Now;
|
|
}
|
|
|
|
public static class SessionUserExtensions
|
|
{
|
|
public static int UserId(this HttpContextBase httpContext)
|
|
{
|
|
var cookies = httpContext.Request.Cookies;
|
|
var userCookie = cookies.Get(nameof(SessionUser));
|
|
var cookieUserId = userCookie?.Value ?? string.Empty;
|
|
|
|
if (int.TryParse(cookieUserId, out int userId))
|
|
{
|
|
userCookie.Expires = DateTime.Now.AddSeconds(ApplicationSettings.LoginTimeout);
|
|
|
|
cookies.Set(userCookie);
|
|
|
|
return userId;
|
|
}
|
|
|
|
return -1;
|
|
}
|
|
|
|
public static bool SignIn(this HttpContextBase httpContext, int userId)
|
|
{
|
|
if (userId > 0)
|
|
{
|
|
httpContext.Response.AppendCookie(new HttpCookie(nameof(SessionUser), $"{userId}")
|
|
{
|
|
Expires = DateTime.Now.AddSeconds(ApplicationSettings.LoginTimeout)
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static void SignOut(this HttpContextBase httpContext)
|
|
{
|
|
var cookieUserId = httpContext
|
|
.Response
|
|
.Cookies
|
|
.Get(nameof(SessionUser))
|
|
?.Value ?? string.Empty;
|
|
}
|
|
|
|
const string DATA = nameof(DATA);
|
|
|
|
public static void SetUserData(this HttpContextBase httpContext, object data)
|
|
{
|
|
if (data != null)
|
|
{
|
|
var userId = httpContext.UserId();
|
|
var json = JsonConvert.SerializeObject(data);
|
|
var encoded = WebUtility.UrlEncode(json);
|
|
var cookie = new HttpCookie($"{DATA}_{userId}")
|
|
{
|
|
Value = encoded,
|
|
Expires = DateTime.Now.AddHours(7)
|
|
};
|
|
|
|
httpContext.Response.Cookies.Set(cookie);
|
|
}
|
|
}
|
|
|
|
public static T GetUserData<T>(this HttpContextBase httpContext)
|
|
{
|
|
var userId = httpContext.UserId();
|
|
var cookie = httpContext.Request.Cookies.Get($"{DATA}_{userId}");
|
|
|
|
if (cookie != null && cookie.Value != null)
|
|
{
|
|
try
|
|
{
|
|
var json = WebUtility.UrlDecode(cookie.Value);
|
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
}
|
|
|
|
return default(T);
|
|
}
|
|
|
|
const string URL_REFERER = nameof(URL_REFERER);
|
|
|
|
public static void SetUrlReferer(this HttpContextBase httpContext)
|
|
=> httpContext
|
|
.Response
|
|
.Cookies
|
|
.Add(new HttpCookie(URL_REFERER, $"{httpContext.Request.Url}"));
|
|
|
|
public static string GetUrlReferer(this HttpContextBase httpContext)
|
|
=> httpContext
|
|
.Request
|
|
.Cookies
|
|
.Get(URL_REFERER)
|
|
?.Value ?? "/";
|
|
}
|
|
} |