211 lines
6.3 KiB
C#
211 lines
6.3 KiB
C#
namespace LaaProduction.Web.App_Infrastructure
|
|
{
|
|
using LaaProductionHttp.Interfaces;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Security.Claims;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Principal;
|
|
using System.Text;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
|
|
/// <summary>
|
|
/// Static class with extension methods that run on controller contexts.
|
|
/// </summary>
|
|
public static class HttpExtensions
|
|
{
|
|
const string ORDER_DATA = nameof(ORDER_DATA);
|
|
const string URL_REFERER = nameof(URL_REFERER);
|
|
const string LAA_USER = nameof(LAA_USER);
|
|
|
|
public static TDST Map<TSRC, TDST>(this TSRC model, Func<TSRC, TDST> mapper)
|
|
where TDST : class
|
|
where TSRC : class
|
|
{
|
|
if (model is null)
|
|
{
|
|
return default(TDST);
|
|
}
|
|
|
|
return mapper(model);
|
|
}
|
|
|
|
public static bool IsInRole<T>(this IPrincipal principal, T role)
|
|
=> principal.IsInRole($"{role}");
|
|
|
|
public static string GetNameIdentifier(this IPrincipal principal)
|
|
{
|
|
if (principal is ClaimsPrincipal claimsPrincipal)
|
|
{
|
|
return claimsPrincipal
|
|
.FindFirst(ClaimTypes.NameIdentifier)
|
|
?.Value;
|
|
}
|
|
|
|
return default(string);
|
|
}
|
|
|
|
public static string FullName(this IPrincipal principal)
|
|
{
|
|
if (principal is ClaimsPrincipal claimsPrincipal)
|
|
{
|
|
return claimsPrincipal
|
|
.FindAll(nameof(FullName))
|
|
.LastOrDefault()?.Value;
|
|
}
|
|
|
|
return "<unknown>";
|
|
}
|
|
|
|
public static string ClaimId(this IPrincipal principal)
|
|
{
|
|
if (principal is ClaimsPrincipal claimsPrincipal)
|
|
{
|
|
return claimsPrincipal
|
|
.FindAll(ClaimTypes.NameIdentifier)
|
|
.LastOrDefault()?.Value;
|
|
}
|
|
|
|
return "<unknown>";
|
|
}
|
|
|
|
public static string GetUrlReferer(this HttpContextBase httpContext)
|
|
=> httpContext
|
|
.Request
|
|
.Cookies
|
|
.Get(URL_REFERER)
|
|
?.Value ?? "~/";
|
|
|
|
public static void SetUrlReferer(this HttpContextBase httpContext)
|
|
=> httpContext
|
|
.Response
|
|
.Cookies
|
|
.Add(new HttpCookie(URL_REFERER, $"{httpContext.Request.Url}"));
|
|
|
|
public static void SetUserData(this HttpContextBase httpContext, object data)
|
|
{
|
|
if (data != null)
|
|
{
|
|
var userId = httpContext.User.ClaimId();
|
|
var json = JsonConvert.SerializeObject(data);
|
|
var encoded = WebUtility.UrlEncode(json);
|
|
var cookie = new HttpCookie($"{ORDER_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.User.ClaimId();
|
|
var cookie = httpContext.Request.Cookies.Get($"{ORDER_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);
|
|
}
|
|
|
|
public static string GetSessionUser(this HttpContextBase httpContext)
|
|
{
|
|
var sessionUser = httpContext.Request.Cookies.Get(LAA_USER);
|
|
|
|
if (sessionUser is null)
|
|
{
|
|
return default(string);
|
|
}
|
|
|
|
sessionUser.Expires = DateTime.UtcNow.AddMinutes(Appsettings.LoginTimeout);
|
|
|
|
httpContext.Response.Cookies.Set(sessionUser);
|
|
|
|
return sessionUser.Value;
|
|
}
|
|
|
|
public static void SignIn(this HttpContextBase httpContext, string token)
|
|
{
|
|
httpContext.Response.Cookies.Add(new HttpCookie(LAA_USER, token)
|
|
{
|
|
Expires = DateTime.UtcNow.AddMinutes(Appsettings.LoginTimeout)
|
|
});
|
|
}
|
|
|
|
public static void SignOut(this HttpContextBase httpContext)
|
|
{
|
|
var sessionUser = httpContext.Request.Cookies.Get(LAA_USER);
|
|
|
|
if (sessionUser != null)
|
|
{
|
|
sessionUser.Expires = DateTime.UtcNow.AddMinutes(Appsettings.LoginTimeout * -1);
|
|
|
|
httpContext.Response.Cookies.Set(sessionUser);
|
|
}
|
|
}
|
|
|
|
public static string ComputeHash(this string value)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(value))
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes(value);
|
|
var hash = SHA256.Create().ComputeHash(bytes);
|
|
var base64 = Convert.ToBase64String(hash);
|
|
|
|
return base64;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public static void AddModelErrors(this ModelStateDictionary modelState, IHttpErrorResponse response)
|
|
{
|
|
if (response != null)
|
|
{
|
|
if (response.Errors != null)
|
|
{
|
|
foreach (var error in response.Errors)
|
|
{
|
|
modelState.AddModelError(error.Key, error.Value);
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(response.Message))
|
|
{
|
|
modelState.AddModelError(nameof(response.Message), response.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static short GetAppId(this IPrincipal principal)
|
|
{
|
|
if (principal is ClaimsPrincipal claimsPrincipal)
|
|
{
|
|
if (short.TryParse($"{claimsPrincipal.FindFirst("AppId")?.Value}", out short appId))
|
|
{
|
|
return appId;
|
|
}
|
|
}
|
|
|
|
return default(short);
|
|
}
|
|
}
|
|
} |