Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • rj 8 posts 98 karma points
    Sep 21, 2018 @ 01:59
    rj
    0

    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).

  • Paul Seal 400 posts 2167 karma points MVP 2x c-trib
    Sep 21, 2018 @ 08:12
    Paul Seal
    100

    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.

    var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
    
    var isLoggedInUser = userTicket != null;
    

    Cheers

    Paul

  • rj 8 posts 98 karma points
    Sep 23, 2018 @ 19:36
    rj
    0

    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.

  • Paul Seal 400 posts 2167 karma points MVP 2x c-trib
    Sep 23, 2018 @ 22:05
    Paul Seal
    0

    I hate it when that happens, but I'm glad you got it sorted.

    Cheers

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft