| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using log4net;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.Filters;
- using SicboSub.Web.Helpers;
- using System.Security.Cryptography;
- namespace SicboSub.Web.Controllers
- {
- public class BaseController : Controller
- {
- protected static readonly ILog log = LogManager.GetLogger(typeof(BaseController));
- /// <summary>
- /// Generate random auth ID
- /// </summary>
- private string GenerateAuthId()
- {
- #pragma warning disable SYSLIB0023
- using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
- {
- byte[] tokenData = new byte[32];
- rng.GetBytes(tokenData);
- return Convert.ToBase64String(tokenData);
- }
- #pragma warning restore SYSLIB0023
- }
- /// <summary>
- /// Tạo auth token và lưu vào Session + Cookie
- /// </summary>
- protected void CreateAuthToken()
- {
- string authId = GenerateAuthId();
- HttpContext.Session.SetString("AuthorizationCookieId", authId);
- CookieOptions options = new CookieOptions()
- {
- Expires = DateTime.Now.AddMinutes(60)
- };
- HttpContext.Response.Cookies.Append("AuthorizationCookie", authId, options);
- }
- /// <summary>
- /// Kiểm tra auth token hợp lệ
- /// </summary>
- protected bool CheckAuthToken()
- {
- string? cookieValue = HttpContext.Request.Cookies["AuthorizationCookie"];
- string? sessionValue = HttpContext.Session.GetString("AuthorizationCookieId");
- if (cookieValue == null || sessionValue == null || cookieValue != sessionValue)
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// Xóa cache/session/cookies
- /// </summary>
- protected bool ClearCache()
- {
- HttpContext.Session.Clear();
- foreach (var cookieKey in HttpContext.Request.Cookies.Keys)
- {
- HttpContext.Response.Cookies.Delete(cookieKey);
- }
- return true;
- }
- /// <summary>
- /// Check đã đăng nhập chưa (dựa vào token trong Session)
- /// </summary>
- protected bool IsAuthenticated()
- {
- var token = HttpContext.Session.GetComplexData<string>("token");
- return !string.IsNullOrEmpty(token);
- }
- /// <summary>
- /// Lấy MSISDN từ Session
- /// </summary>
- protected string? GetMsisdn()
- {
- return HttpContext.Session.GetComplexData<string>("msisdn");
- }
- /// <summary>
- /// Lấy token từ Session
- /// </summary>
- protected string? GetToken()
- {
- return HttpContext.Session.GetComplexData<string>("token");
- }
- /// <summary>
- /// Lấy ngôn ngữ từ Session
- /// </summary>
- protected string GetLanguage()
- {
- var lang = HttpContext.Session.GetComplexData<string>("culture") ?? "ht";
- HttpContext.Session.SetComplexData("culture", lang);
- return lang;
- }
- /// <summary>
- /// Set ngôn ngữ
- /// </summary>
- protected void SetLanguage(string lang)
- {
- HttpContext.Session.SetComplexData("culture", lang);
- }
- /// <summary>
- /// Redirect đến URL khi chưa đăng nhập
- /// Nếu không có RedirectUrl trong config thì về trang chủ Index
- /// </summary>
- protected IActionResult RedirectToLogin(IConfiguration configuration)
- {
- var redirectUrl = configuration.GetSection("RedirectUrl").Value;
-
- if (!string.IsNullOrEmpty(redirectUrl))
- {
- // Redirect ra URL external
- return Redirect(redirectUrl);
- }
- else
- {
- // Redirect về Index
- return RedirectToAction("Index", "Home");
- }
- }
- /// <summary>
- /// Check authentication - nếu chưa login thì redirect
- /// Dùng trong các action yêu cầu đăng nhập
- /// </summary>
- protected IActionResult? CheckAndRedirectIfNotAuthenticated(IConfiguration configuration)
- {
- if (!IsAuthenticated())
- {
- log.Warn("User not authenticated, redirecting...");
- return RedirectToLogin(configuration);
- }
- return null; // Đã đăng nhập, tiếp tục
- }
- }
- }
|