.NET MVC Write Cookie from ActionFilter

2020-07-23 08:48发布

I built an ActionFilter to host a page hit logger on my MVC site and have the need to save some values into cookie. I've used the following code to write the cookie:

public class LogRequestAttribute : ActionFilterAttribute, IActionFilter
{
   void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
   {
       if(filterContext.HttpContext.Request.UserHostAddress != "127.0.0.1")
           ThreadPool.QueueUserWorkItem(delegate
           {
               string adid = filterContext.HttpContext.Request.QueryString["adid"] != null 
                   ? (string)filterContext.HttpContext.Request.QueryString["adid"] : string.Empty;

               if (!String.IsNullOrEmpty(adid))
               {
                   HttpCookie cookie = new HttpCookie("adid", adid);
                   cookie.Expires = DateTime.Now.AddDays(30);
                   filterContext.HttpContext.Response.Cookies.Add(cookie);
               }

               DomainModel.Concrete.SqlPageHitActivity logger 
                   = new DomainModel.Concrete.SqlPageHitActivity(
                       System.Configuration.ConfigurationManager.ConnectionStrings["TrackingConnectionString"].ConnectionString);

               DomainModel.Entities.PageHitActivity hit = new DomainModel.Entities.PageHitActivity
               {
                   AdIdentifier = filterContext.HttpContext.Response.Cookies["adid"].Value == null ? string.Empty : filterContext.HttpContext.Response.Cookies["adid"].Value,
                   Authenticated = filterContext.HttpContext.Request.IsAuthenticated,
                   Browser = filterContext.HttpContext.Request.UserAgent,
                   CookieId = string.Empty,
                   Parameters = string.Empty,
                   Referer = filterContext.HttpContext.Request.UrlReferrer == null ? string.Empty : filterContext.HttpContext.Request.UrlReferrer.ToString(),
                   RequestDate = DateTime.Now,
                   RequestorIP = filterContext.HttpContext.Request.UserHostAddress,
                   SessionId = string.Empty,
                   SiteId = 1,
                   Target = filterContext.HttpContext.Request.Url != null ? filterContext.HttpContext.Request.Url.AbsoluteUri : string.Empty,
                   UserId = filterContext.HttpContext.Request.IsAuthenticated ? filterContext.HttpContext.User.Identity.Name : string.Empty
               };
               logger.LogHit(hit);
           });
   }
}

The cookie seems to appear in the collection but is not available when a subsequent page hit is logged.

Thanks, Chris

2条回答
对你真心纯属浪费
2楼-- · 2020-07-23 09:07

Did you set the cookie expiration? The default cookie expiration is a session cookie (deletes when the browser is closed). To have the cookie save to disk to must give it a future date.

查看更多
家丑人穷心不美
3楼-- · 2020-07-23 09:14

Try adding a new cookie instead of modifying the existing collection:

HttpCookie cookie = new HttpCookie("adid", MyValue);
cookie.Expires = DateTime.Now.AddDays(30);

filterContext.HttpContext.Response.Cookies.Add(cookie);
查看更多
登录 后发表回答