Copied to clipboard

Flag this post as spam?

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


  • John Bergman 460 posts 1066 karma points
    May 23, 2018 @ 03:23
    John Bergman
    0

    Access Umbraco back office user from the front end

    I have a few pages that the product owner wants to ensure that the user is logged into the back office when accessing the page.

    I have searched around for several hours and have not been able to find a solution that works directly in the razor markup

    Below are a few of the things I have tried:

    var userService = ApplicationContext.Current.Services.UserService;

    var b = umbraco.BusinessLogic.User.GetCurrent(); var user = UmbracoContext.Current.Security.CurrentUser;

    //var user2 = userService.GetByUsername(HttpContext.Current.User.Identity.Name).Id;

    IUser currentUser = null; var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket(); if (userTicket != null) { currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name); }

    var uu = umbraco.helper.GetCurrentUmbracoUser();

    None of the above code yields any type of indicating that I am also currently logged into the backoffice. Anyone have any other ideas as to how to get this to work?

  • Lee See Raa 6 posts 96 karma points
    May 23, 2018 @ 07:10
    Lee See Raa
    0

    Why you want access User Back Office from Front End?

    Maybe no way to Access this!

  • Marcio Goularte 356 posts 1248 karma points
    May 23, 2018 @ 14:34
    Marcio Goularte
    100

    For a specific page, made a custom attribute Authorize and created a RenderMvcController

    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Core.Security;
    
    namespace MyNamespace
    {
    
        public class BackOfficeAuthorize : AuthorizeAttribute
        {
    
            protected override bool AuthorizeCore(HttpContextBase httpContext)
            {
                bool authorize = true;
                var auth = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket();
                if (auth == null)
                {
                    return false;
                }            
    
                return authorize;
            }
            protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
        }
    }
    
        [BackOfficeAuthorize]
        public class productController : RenderMvcController
        {    
            public override ActionResult Index(RenderModel model)
            {   
                return base.Index(model);
            }
        }
    

    UPDATE: You can also try to use the IsBackOffice attribute

    [Umbraco.Web.WebApi.IsBackOffice]
    public class productController : RenderMvcController
            {    
                public override ActionResult Index(RenderModel model)
                {   
                    return base.Index(model);
                }
            }
    
  • John Bergman 460 posts 1066 karma points
    May 24, 2018 @ 21:23
    John Bergman
    0

    Thanks Marcio… just what I needed :-)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies