ln.http/session/Session.cs

70 lines
1.8 KiB
C#
Raw Normal View History

2019-02-26 22:00:37 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-11-15 13:47:01 +01:00
namespace ln.http.session
2019-02-26 22:00:37 +01:00
{
2019-11-15 13:47:01 +01:00
public class Session : IDisposable
2019-02-26 22:00:37 +01:00
{
public Guid SessionID { get; private set; }
public long SessionAge => DateTimeOffset.Now.ToUnixTimeSeconds() - LastTouch;
public long LastTouch { get; private set; }
2019-03-13 08:24:24 +01:00
public HttpUser CurrentUser { get; set; }
2019-02-26 22:00:37 +01:00
private readonly Dictionary<string, object> elements = new Dictionary<string, object>();
public Session()
{
SessionID = Guid.NewGuid();
2019-03-13 08:24:24 +01:00
CurrentUser = new HttpUser();
2019-02-26 22:00:37 +01:00
}
public object this[string name]
{
get => this.elements[name];
set => this.elements[name] = value;
}
2019-11-15 13:47:01 +01:00
public T Get<T>(string name) where T:class
{
if (elements.ContainsKey(name))
return elements[name] as T;
return null;
}
2019-02-26 22:00:37 +01:00
public String[] ElementNames => this.elements.Keys.ToArray();
public void Touch()
{
LastTouch = DateTimeOffset.Now.ToUnixTimeSeconds();
}
2019-03-13 08:24:24 +01:00
public virtual void Authenticate(HttpRequest httpRequest)
{
}
2019-11-15 13:47:01 +01:00
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
// {
// }
//}
}
}
2019-02-26 22:00:37 +01:00
}
}