70 lines
1.8 KiB
C#
70 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace ln.http.session
|
|
{
|
|
public class Session : IDisposable
|
|
{
|
|
public Guid SessionID { get; private set; }
|
|
public long SessionAge => DateTimeOffset.Now.ToUnixTimeSeconds() - LastTouch;
|
|
public long LastTouch { get; private set; }
|
|
|
|
public HttpUser CurrentUser { get; set; }
|
|
|
|
|
|
private readonly Dictionary<string, object> elements = new Dictionary<string, object>();
|
|
|
|
public Session()
|
|
{
|
|
SessionID = Guid.NewGuid();
|
|
CurrentUser = new HttpUser();
|
|
}
|
|
|
|
public object this[string name]
|
|
{
|
|
get => this.elements[name];
|
|
set => this.elements[name] = value;
|
|
}
|
|
|
|
public T Get<T>(string name) where T:class
|
|
{
|
|
if (elements.ContainsKey(name))
|
|
return elements[name] as T;
|
|
return null;
|
|
}
|
|
|
|
public String[] ElementNames => this.elements.Keys.ToArray();
|
|
|
|
public void Touch()
|
|
{
|
|
LastTouch = DateTimeOffset.Now.ToUnixTimeSeconds();
|
|
}
|
|
|
|
public virtual void Authenticate(HttpRequest httpRequest)
|
|
{
|
|
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (object o in elements.Values)
|
|
{
|
|
// if (o is IDisposable disposable)
|
|
// {
|
|
// try
|
|
// {
|
|
// disposable.Dispose();
|
|
// }
|
|
//#pragma warning disable RECS0022 // catch-Klausel, die System.Exception abfängt und keinen Text aufweist
|
|
// catch (Exception)
|
|
//#pragma warning restore RECS0022 // catch-Klausel, die System.Exception abfängt und keinen Text aufweist
|
|
// {
|
|
|
|
// }
|
|
//}
|
|
}
|
|
}
|
|
}
|
|
}
|