2

I have a mobile site with a link to view the main site.

By default, when a mobile user goes to my main site it detects the mobile device and redirects to the mobile site.

Once on the mobile site, if the user then clicks, "View main site" it creates a cookie and redirects back to the main site. The main site detects the cookie and as a result doesn’t redirect them back; it always accepts the main site as their preferred choice.

The problem is, on the main site, it can detect the cookie, but the value is always null and the expiry is always DateTime.MinValue.

Mobile URL = mobile.mysite.co.uk

Main Site = mysite.co.uk

Here is my code...

Link To Main Site From Mobile Site

    public ActionResult ViewMainSite()
    {
        string CookieValue = "Always Show the Main Site";
        HttpCookie Cookie = new HttpCookie("ShowMainSite");

        // Set the cookie value and expiry.
        Cookie.Value = CookieValue;
        Cookie.Expires = DateTime.Now.AddYears(1);

        // Add the cookie.
        Response.Cookies.Add(Cookie);

        return Redirect("mainSiteURL");
    }

Main Site Action Filter - Detect Mobile User

        /// <summary>
    /// Redirect If Mobile Action filter class
    /// </summary>
    public class RedirectIfMobile : ActionFilterAttribute
    {
        /// <summary>
        /// override the OnactionExecuting Method
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
                // check to see if the have the main site cookie
                var Cookie = filterContext.HttpContext.Response.Cookies.Get("ShowMainSite");

                if (Cookie != null)
                {
                    if (Cookie.Value == null || Cookie.Expires < DateTime.Now)
                    {
                        filterContext.HttpContext.Response.Redirect("MobileURL");
                    }
                }

            base.OnActionExecuting(filterContext);
        }
    }

can any one see why this is happening ?

any help is most appriciated.

JGilmartin
  • 8,683
  • 14
  • 66
  • 85
  • I thing You can't add cookie and make redirect on same header. you can't use redirect when you want to save cookie on page.Somebody let me know if I am wrong. – Anirudha Gupta Aug 02 '12 at 10:37
  • see http://stackoverflow.com/questions/5366635/is-it-possible-to-set-a-cookie-during-a-redirect-in-asp-net – Anirudha Gupta Aug 02 '12 at 10:41

2 Answers2

1

Have you tried Cookie.Expires = Now.AddYears(1); Or possibly DateTime expires = Now.AddYears(1); Cookie.Expires = expires;

At the moment your datetime now just seems to be festering/doing not very much.

Pharap
  • 3,826
  • 5
  • 37
  • 51
  • using DateTime.Now.AddYears(1) = {02/08/2013 11:34:50} – JGilmartin Aug 02 '12 at 10:36
  • I know that, but you made the variable now, and then did nothing with it. Also it could be an error that can be fixed with a variable/object reference, sometimes things work better when stored to a variable first. It's a bit odd, but odd exceptions like that have been known to happen. – Pharap Aug 02 '12 at 10:41
  • i know which bit you mean, it was a variable that did nothing, ive removed it, but its certainly not the cause of the issue – JGilmartin Aug 02 '12 at 10:52
  • I know it's not the cause, it was just an observation whilst trying to figure out the cause. Did you try assigning a variable to the value Now.AddYears(1) and then assigning the Cookie.Expires to that variable? It sounds odd, but sometimes things like that work for some reason. – Pharap Aug 02 '12 at 10:58
0

Are you sure that browser send appropriate cookie from sub-domain to main-domain application? please use a network request monitor such as chrome/firefox developer tools to find out what is exactly receiving and sending. I guess cookie is not sent due to cross-domain policies.

Ali Dehghan
  • 462
  • 3
  • 6