Reputation: 9298
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.
Upvotes: 2
Views: 2683
Reputation: 462
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.
Upvotes: 0
Reputation: 4082
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.
Upvotes: 1