Copied to clipboard

Flag this post as spam?

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


  • Carl Jackson 139 posts 478 karma points
    Jun 17, 2015 @ 08:57
    Carl Jackson
    0

    Logged in user from http handler.

    Hi,

    I am trying to create a custom http handler to allow only logged in users to access certain media files (umbraco form uploads)

    I have registered it ok and have got an Umbraco context setup however the context does not contain the curremt logged in user.

    In the following code 'user' is always null

        public class FormFileHandler : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable
        {
            get { return false; }
        }
    
        public void ProcessRequest(HttpContext context)
        {
    
    
            UmbracoContext.EnsureContext(
            new HttpContextWrapper(HttpContext.Current),
            ApplicationContext.Current,
            true);  
    
            var user = UmbracoContext.Current.Security.CurrentUser;
    
    
            if (user ==null)
            {
                context.Response.AddHeader("Location", "/umbraco/");
                context.Response.StatusCode = 401;
                context.Response.End();
            }
        }
    }
    

    Has anyone done anything similar and managed to access the current logged in user from a httphandler?

    Thanks

    Carl

  • Carl Jackson 139 posts 478 karma points
    Jun 17, 2015 @ 13:13
    Carl Jackson
    100

    After a bit more digging I found a post about how the logged in user is not set unless you are actually in the back end of Umbraco

    So... You have to do something like the following :

                var auth = new HttpContextWrapper(HttpContext.Current).GetUmbracoAuthTicket();
            if (auth == null)
            {
                context.Response.AddHeader("Location", "/umbraco/");
                context.Response.StatusCode = 302;
                context.Response.End();
            }
    

    any interest in this as a package when I've finished? as I have the whole file response to add in so will be too big to post all the code.

    Thanks

    Carl.

Please Sign in or register to post replies

Write your reply to:

Draft