是否可以在ASP.NET的重定向过程中设置cookie? [英] Is it possible to set a cookie during a redirect in ASP.NET?

查看:93
本文介绍了是否可以在ASP.NET的重定向过程中设置cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET.我要么添加或设置一个cookie(取决于HttpRequest是否包含具有指定键的cookie),然后立即调用Response.Redirect.未设置cookie.这是正确的行为吗?在具有302状态代码的http响应期间设置cookie是否相互排斥?

I am using ASP.NET. I either add or set a cookie (depending on whether the HttpRequest contains a cookie with specified key), and immediately afterward call Response.Redirect. The cookie is not set. Is this correct behavior? Is there something mutually exclusive about setting a cookie during an http response with a 302 status code?

        if (context.HttpContext.Request.Browser.Cookies)
        {
            var cookies = context.HttpContext.Request.Cookies;
            var stateCookie = new HttpCookie(SR.session, clientState.SessionId.ToString());
            if (cookies.AllKeys.Contains(SR.session))
            {
                context.HttpContext.Response.Cookies.Set(stateCookie);
            }
            else
            {
                context.HttpContext.Response.Cookies.Add(stateCookie);
            }
        }

这是Response标头

  • X-AspNetMvc版本-2.0
  • 连接-关闭
  • 缓存控制-私有
  • 内容类型-文本/html
  • 日期-2011年3月20日,星期日,格林尼治标准时间
  • 位置- http://localhost:3599/Home/Redirected
  • 服务器-ASP.NET开发服务器/9.0.0.0
  • X-AspNet版本-2.0.50727
  • Here are the Response headers

    • X-AspNetMvc-Version - 2.0
    • Connection - Close
    • Cache-Control - private
    • Content-Type - text/html
    • Date - Sun, 20 Mar 2011 03:48:04 GMT
    • Location - http://localhost:3599/Home/Redirected
    • Server - ASP.NET Development Server/9.0.0.0
    • X-AspNet-Version - 2.0.50727
    • 推荐答案

      稍作搜索后,似乎可以,在重定向响应中设置cookie可能会出现问题,因为某些浏览器可能会忽略它. (这可能是有道理的,因为响应实际上是在告诉客户端忽略资源,而是获取其他资源).

      After googling a bit it seems that yes, there can be problems with setting the cookie in the redirect response as it may be ignored by a few browsers. (It may make some sense, as the response is really telling the client to ignore the resource and get some other resource instead).

      这已在此处讨论:在302重定向期间发送浏览器cookie

      因此,我将以一种允许重定向页面设置cookie的方式更改体系结构.

      So I would change the architecture in a way that allows the page being redirected to to set the cookie.

      这篇关于是否可以在ASP.NET的重定向过程中设置cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆