Determining If a Viewer Is Logged Into The Backoffice Without Hitting Database
Hi
I was wondering if it's possible to determine whether or not a viewer is logged into the backoffice or not without hitting the database.
For a long time I've been using the following code in the small amount of circumstances in which I've needed to know it:
var auth = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket();
if (auth == null)
{
return false;
}
var backofficeUser = ApplicationContext.Current.Services.UserService.GetByUsername(auth.Name);
return backofficeUser != null;
However I now need to determine if a viewer is logged into the backoffice or not on all pages, meaning a method that does not hit the database (UserService initializes and uses a database repository) would be far more ideal.
I've attempted UmbracoContext.Current.Security.ValidateCurrentUser() however this returns false on the front-end (It depends on HttpContext.Current.GetCurrentIdentity(bool) which returns null on the front-end for some reason).
Determining If a Viewer Is Logged Into The Backoffice Without Hitting Database
Hi
I was wondering if it's possible to determine whether or not a viewer is logged into the backoffice or not without hitting the database.
For a long time I've been using the following code in the small amount of circumstances in which I've needed to know it:
However I now need to determine if a viewer is logged into the backoffice or not on all pages, meaning a method that does not hit the database (UserService initializes and uses a database repository) would be far more ideal.
I've attempted
UmbracoContext.Current.Security.ValidateCurrentUser()
however this returns false on the front-end (It depends onHttpContext.Current.GetCurrentIdentity(bool)
which returns null on the front-end for some reason).Hi rj I wrote a blog post about it here. https://codeshare.co.uk/blog/how-to-get-the-current-logged-in-umbraco-user-and-restrict-front-end-content/
You can ignore the line about getting the actual user and just see if they are logged in like this.
Cheers
Paul
I spent hours going through the Umbraco code to try and find a solution and it was right there in front of me!
Thank you Paul, I don't know how I haven't died from drinking expired milk or something yet.
I hate it when that happens, but I'm glad you got it sorted.
Cheers
Paul
is working on a reply...