Copied to clipboard

Flag this post as spam?

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


  • Dave Clarke 15 posts 127 karma points
    May 10, 2017 @ 09:04
    Dave Clarke
    0

    Restrict access to backoffice users

    Does anyone know of any way to restrict access to the Umbraco site - to only those users who are logged in to the backoffice?

    We're setting up an internal site and want to lock the whole site down to the backoffice users (also authors).

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 10, 2017 @ 09:49
    Alex Skrypnyk
    1

    Hi Dave

    You can add checking in the master template or render controller is current customer authenticated user or no.

    How to get current authenticated user look in this topic - https://our.umbraco.org/forum/developers/api-questions/50075-how-to-get-the-current-user-with-UserService

    Thanks,

    Alex

  • Dave Clarke 15 posts 127 karma points
    May 10, 2017 @ 11:56
    Dave Clarke
    101

    Hi Alex,

    Thanks for quick response - as you suggested I added a separate controller and made this the default controller. Including my code here for future reference!

    public class AuthorisedUmbracoController : RenderMvcController
    {
    
        public override ActionResult Index(RenderModel model)
        {
            var ticket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
    
            if (ticket != null)
            {
                var current = ApplicationContext.Services.UserService.GetByUsername(ticket.Name);
    
                return base.Index(model);
            }
    
            //get return URL, check local
            var returnUrl = Request.Url.PathAndQuery; // assuming deployed at root)
    
            if (!Url.IsLocalUrl(returnUrl))
            {
                returnUrl = "/umbraco"; //login screen
            }
    
            return new RedirectResult("/umbraco#/login/false?returnPath=" + returnUrl, false);
        }
    }
    

    The following sets the new controller as the default.

    public class ApplicationStartingEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(AuthorisedUmbracoController));
        }
    }
    

    Thanks for your help!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 10, 2017 @ 12:19
    Alex Skrypnyk
    1

    What a great solution, glad that you solved it!

    Have a nice evening.

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft