This commit is contained in:
2024-07-18 03:13:32 +07:00
parent d2eee41ed6
commit d9c3bfa35e
49 changed files with 3037 additions and 232 deletions

View File

@ -1,4 +1,7 @@
namespace Yuna.Website.Server.Infrastructure
using System.Security.Claims;
using Yuna.Website.Server.Model;
namespace Yuna.Website.Server.Infrastructure
{
public static class CookieExtensions
{
@ -30,5 +33,21 @@
context.Response.Cookies.Append(key, dataStr, new CookieOptions() { HttpOnly = true, MaxAge = maxAge ?? TimeSpan.FromDays(365) });
}
public static long? GetUserIdFromCookie(this HttpContext context)
{
var idStr = context.User.FindFirstValue(ClaimTypes.NameIdentifier);
var isParsed = long.TryParse(idStr, out var result);
return isParsed ? result : null;
}
public static bool GetRoleFromCookie(this HttpContext context)
{
if (bool.TryParse(context.User.FindFirstValue(ClaimTypes.Role), out var result))
{
return result;
}
return false;
}
}
}

View File

@ -0,0 +1,21 @@
using System.Text.Json.Serialization;
using Yuna.Website.Server.Model;
namespace Yuna.Website.Server.Infrastructure
{
public class CookieUserModel
{
public CookieUserModel(User user)
{
Id = user.Id;
Username = user.UserName;
}
[JsonPropertyName("username")]
public string Username { get; }
[JsonPropertyName("id")]
public long Id { get; }
}
}