简体   繁体   中英

Cookie in asp.net mvc not setting

I am experiencing strange situation. The set cookie is persisted only when the string is hard coded.

public void OnActionExecuted(ActionExecutedContext filterContext)
{
    if (filterContext.HttpContext.Items["Theam"] != null)
    {
        var sTheam = filterContext.HttpContext.Items["Theam"].ToString();

        HttpCookie theamCookie = new HttpCookie("TheamCookie");

        theamCookie.Values.Add("TheamVal", sTheam);
        theamCookie.Expires = DateTime.UtcNow.AddDays(5);
        HttpContext.Current.Response.Cookies.Add(theamCookie);
    }
}

No mater what I do the cookie is not persisted. Only when replace sTheam to a value like "cupid" the value is persisted. That is

theamCookie.Values.Add("TheamVal", "cupid"); 

works and nothing else.

Can any one throw some light as to what is happening? I am exhausted, and completely ran out of options. After over 8 hours of debugging that I realized this. But not sure why is this happening. Please help.

Update: The following is the CookieFilter. This is an ASP.NET MVC app.

public class CookieFilter : IActionFilter
{
    //private const string sTheamCookieName = "TheamCookie";
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.HttpContext.Items["TheamToBrowser"] != null)
        {
            var sTheam = ((string)(filterContext.HttpContext.Items["TheamToBrowser"])).ToString(CultureInfo.InvariantCulture);

            HttpCookie theamCookie = new HttpCookie("TheamCookie");


            theamCookie.Values["TheamVal"] = "shamrock";
            theamCookie.Expires = DateTime.UtcNow.AddDays(5);
            filterContext.HttpContext.Response.Cookies.Add(theamCookie);
        }
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContextBase context = filterContext.HttpContext;
        HttpCookie theamCookie = context.Request.Cookies["TheamCookie"];

        if (theamCookie == null)
            context.Items["TheamFromBrowser"] = "default";
        else
        {
            if (string.IsNullOrEmpty(theamCookie.Value))
            {
                context.Items["TheamFromBrowser"] = "default";
            }
            else
                context.Items["TheamFromBrowser"] = theamCookie.Values["TheamVal"].ToString();
        }
    }
}

Regarding this line:

if (filterContext.HttpContext.Items["Theam"] != null)

HttpContext.Items is a request-level setting. That means you need to reload it on every HTTP request. If you are not setting HttpContext.Items["Theam"] somewhere in the request lifecycle before this piece of code is run, it will always be empty.

I am not sure that is your problem, but it would explain why the rest of your code is working when you set the variable manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM