Copied to clipboard

Flag this post as spam?

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


  • Mikkel 14 posts 115 karma points
    Mar 27, 2019 @ 14:09
    Mikkel
    0

    Show content for backend admin only

    Hey!

    I've been trying to have specific tools on my website, only visibile if you are logged in as the admin account in the backoffice. I've had this working on previous versions of Umbraco 7, but can't for the life of me figure out how it works in Umbraco 8.

    For context, it's used as a plugin installed in /App_Plugins/. Then fetched into the pages using:

    @Html.Partial("~/App_Plugins/Toolbox/Toolbox.cshtml")`
    

    So far I've tried the following, without much success:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using ClientDependency.Core.Mvc
    @using Umbraco.Core
    @using Umbraco.Core.Security
    @using Umbraco.Core.Models.Membership
    @{
        HttpContextBase wrapper = new HttpContextWrapper(HttpContext.Current);
        UmbracoBackOfficeIdentity user = wrapper.GetCurrentIdentity(true);
        bool isUmbracoLoggedIn = user != null;
    }
    @if (isUmbracoLoggedIn)
    {
        // Tools go here
    }
    

    I've also tried the following, with no luck:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using ClientDependency.Core.Mvc
    @using Umbraco.Core
    @using Umbraco.Core.Security
    @using Umbraco.Core.Models.Membership
    @{
    var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
        if (userTicket != null) {     
            var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
            var isLoggedInUser = true;
        }
    }
    
    @if(isLoggedInUser) {
        // Tools go here
    }
    

    Aswell as trying to get information about the logged in user using:

    bool isLoggedOn = HttpContext.Current.User.Identity.IsAuthenticated;
    

    However this always returns false.

    I've tried Googling for the past few hours, and tried everything I've come across. All solutions suggesting using GetUmbracoAuthTicket() hasn't worked at all for me.

    Let me know if you need anything else, hope some sharp minds in here are able to help! Thanks in advance!

    • Mikkel
  • rj 8 posts 98 karma points
    Jul 17, 2019 @ 02:24
    rj
    0

    I have done some testing and the following is working in my Umbraco 8.1.0 project

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using ClientDependency.Core.Mvc
    @using Umbraco.Web.Security
    @{
        bool isloggedin = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket() != null;
    }
    
    @if (isloggedin)
    {
        <strong>Logged in</strong>
    } else {
        <strong>NOT Logged in</strong>
    }
    

    Ensure that you are actually logged into the backoffice while you're testing this, I know it's stupid of me to point out the obvious but it's something I actually did forget to do.


    Logged in Not logged in

Please Sign in or register to post replies

Write your reply to:

Draft