Copied to clipboard

Flag this post as spam?

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


  • Mike B 2 posts 22 karma points
    Mar 13, 2012 @ 13:04
    Mike B
    0

    Sharing back-office authenticated user session in a sub-domain (MVC 3 application) - Umbraco 4.0.1

    Hi all,

    The scenario I have is quite simple to understand, but implementing the desired functionality is proving to be a nightmare.

    We have the main installation of Umbraco CMS (version 4.0.1) at our first domain (e.g. one.domain.com), where back office users can log in an administrate at the one.domain.com/umbraco/ directory without any issues. Our client now wants the back office to be extended by loading an MVC 3 application (hosted at two.domain.com) within an iframe when they click on a node from the navigation tree. The node has been added and the MVC application loads within the iframe as we want, however, there is currently no restrictions in place to prevent users who aren't authenticated from accessing  this page.

    How can I share the back-office authentication across these two sub-domains? I've implemented the UsersMembershipProvider, set the same machine key across applications and copied the Umbraco config files across to the MVC project without any luck. I've never been great with Memberships anyway and was hoping somebody else could shed some light.

    Regards,

    Mike

  • Paul Brown 39 posts 80 karma points
    Jan 08, 2013 @ 21:38
    Paul Brown
    0

    Mike

    Did you ever fix this, looking for same solution also.

    Cheers Paul

  • Mike B 2 posts 22 karma points
    Jan 09, 2013 @ 12:05
    Mike B
    0

    Hi Paul,

    Unfortunately I struggled to get a decent implementation in place. I ended up manually creating a shared user context stored in a cookie by adding the login name and a hashed version of the login name (using a hash only known between the two domains - as the sites were internal to the company security wasn't the highest priority). The cookie's domain is then set to the broader domain.com rather than to any specific sub-domain to make it accessible site-wide. 

    The secondary domain then retrieves the shared cookie from the request object and attempts to authorise the user by comparing the login name and the hashed login name using the same algorithm. This is done by creating a custom AuthorizeAttribute and overriding the AuthorizeCore() method to add your own authorisation (in my case the hash comparison). You can then use the OnAuthorisation method to peform any additional re-directs etc.

     

    Create the shared cookie using a custom-built hashing alogrith known between both domains in one.domain.com

    userDetailsCollection.Add("UserName", currentUser.LoginName);
    userDetailsCollection.Add("CheckValue"SecurityHelper.Hash(currentUser.LoginName));
    
    HttpCookie userCookie = new HttpCookie("SharedContext");
    userCookie.Domain = ConfigurationManager.AppSettings["TopLevelSiteDomain"];
    userCookie.Path = "/";
    userCookie.Secure = false;
    userCookie.Values.Add(userDetailsCollection);
    Response.Cookies.Add(userCookie);

    Implement a custom AuthorizeAattribute in two.domain.com

    public class UmbracoAuthorizationProvider : AuthorizeAttribute
    {
        private bool _authorize;
        private HttpContextBase _httpContext;
    
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            _httpContext = httpContext;
    
            if (httpContext.Request.Cookies["SharedContext"] == null)
            {
                return false;
            }
    
            _authorize = (AuthorizeUser());
    
            return _authorize;
        }
    
        protected bool AuthorizeUser()
        {
            string userName = SecurityHelper.Hash(_httpContext.Request.Cookies["SharedContext"].Values["UserName"]);
            string checkValue = _httpContext.Request.Cookies["SharedContext"].Values["CheckValue"];
    
            return string.Equals(userName, checkValue);
        }
    
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
    
            if (filterContext.Result is HttpUnauthorizedResult)
            {
                //Perform unauthorised code
            }
        }                                                                                                                               }

    I know this is by no means the most elegant or secure solution, but it seems to work for my intended purpose. I hope this sparks some ideas or possibly works for your own scenario.

    Thanks,

    Mike

     

     

Please Sign in or register to post replies

Write your reply to:

Draft