ln.application/ApplicationContext.cs

57 lines
1.5 KiB
C#

using System;
using System.Threading;
namespace ln.application
{
public class ApplicationContext
{
static ThreadLocal<ApplicationContext> currentApplicationContext = new ThreadLocal<ApplicationContext>();
public static void SetCurrentContext(ApplicationContext applicationContext) => currentApplicationContext.Value = applicationContext;
public static void ClearCurrentContext() => currentApplicationContext.Value = null;
public Guid ContextID { get; }
public DateTime Created { get; }
public DateTime LastAccess { get; protected set; }
public TimeSpan Age => LastAccess - Created;
public TimeSpan Untouched => DateTime.Now - LastAccess;
public Application Application { get; }
public AuthenticatedUser CurrentUser { get; protected set; }
public ApplicationContext(Application application)
{
ContextID = Guid.NewGuid();
Created = DateTime.Now;
LastAccess = Created;
Application = application;
CurrentUser = AuthenticatedUser.Anonymous;
}
protected void UpdateLastAccess()
{
LastAccess = DateTime.Now;
}
public virtual bool AuthenticateUser(string username,object prove)
{
AuthenticatedUser authenticatedUser = Application.AuthenticateUser(username, prove);
if (authenticatedUser != null)
{
CurrentUser = authenticatedUser;
return true;
}
else
{
CurrentUser = AuthenticatedUser.Anonymous;
return false;
}
}
public virtual void DeauthenticateUser()
{
CurrentUser = AuthenticatedUser.Anonymous;
}
}
}