Innovenergy_trunk/csharp/App/Backend/Model/Relations/Session.cs

35 lines
949 B
C#

using SQLite;
namespace Innovenergy.Backend.Model.Relations;
public class Session : Relation<String, Int64>
{
[Indexed] public String Token { get => Left ; set => Left = value;}
[Indexed] public Int64 UserId { get => Right; set => Right = value;}
[Indexed] public DateTime ExpiresAt { get; set; }
[Ignore] public Boolean Valid => ExpiresAt > DateTime.Now;
[Ignore] public Boolean Expired => !Valid;
[Obsolete("To be used only by serializer")]
public Session()
{}
public Session(User user) : this(user, TimeSpan.FromDays(7))
{
}
public Session(User user, TimeSpan validFor)
{
Token = CreateToken();
UserId = user.Id;
ExpiresAt = DateTime.Now + validFor;
}
private static String CreateToken()
{
var token = new Byte[16]; // 128 bit
Random.Shared.NextBytes(token);
return Convert.ToBase64String(token);
}
}