frontend
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user