I have a Singleton model model in my MVC application to determine if a user is logged in with / admin authorization (based on membership in certain AD groups). This model class must be Singleton, so user permissions can be set once at the first login and used throughout the session:
public sealed class ApplicationUser
{
public static ApplicationUser CurrentUser { get { return lazy.Value; } }
private static readonly Lazy<ApplicationUser> lazy =
new Lazy<ApplicationUser>(() => new ApplicationUser());
private ApplicationUser()
{
GetUserDetails();
}
public string Name { get { return name; } }
public bool IsAuthorized { get { return isAuthorized; } }
public bool IsAdmin { get { return isAdmin; } }
}
Singleton is first created in my EntryPointController, from which all other controllers get:
public abstract class EntryPointController : Controller
{
protected ApplicationUser currentUser = ApplicationUser.CurrentUser;
}
These patterns allow me to use ApplicationUser.CurrentUser.Nameor ApplicationUser.CurrentUser.IsAuthorizedetc. throughout my application.
:
, -! , , !
Singleton?