为什么不把我的饼干吗? [英] Why is my cookie not set?

查看:105
本文介绍了为什么不把我的饼干吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩弄再次与ASP.NET,并试图设置一个动作一个cookie将在另一个动作读取。

I am playing around again with ASP.NET, and tried to set a cookie in one action which will be read in another action.

奇怪的是:该cookie获取设置,但访问其他页面时失去它的价值。这是我简单的控制器code:

The strange thing is: the cookie gets set, but looses its value when accessing another page. Here is my simple controller code:

public class HomeController : Controller
{
    public ActionResult About()
    {
        var cookie = Response.Cookies.Get("sid");
        ViewData["debug"] = "Id: " + cookie.Value;

        return View();
    }

    public ActionResult DoLogin()
    {
        var cookie = new HttpCookie("sid", Guid.NewGuid().ToString());
        cookie.HttpOnly = true;
        Response.Cookies.Add(cookie);

        return RedirectToAction("About");
    }
}

流程是这样的:首先,我访问 /主页/ DoLogin ,然后我得到重定向到 /首页/关于应该在 SID 的cookie的实际输出值。但cookie不具有任何值。

The flow is like this: first I access /Home/DoLogin, then I get redirected to /Home/About which should actually output the value of the sid cookie. But the cookie does not have any value.


  • Cookies没有在我的浏览器中禁用

  • 我知道,ASP.NET有它自己的会话处理机制,只是打周围,偶然发现了这个cookie的问题

感谢您的任何提示!

推荐答案

在你的关于操作,使用 Request.Cookies时代替。

In your About action, use Request.Cookies instead.

作为一个简短的说明:当你设置 Response.Cookies 的东西,这cookie发送到存储它的客户端。在每个后续请求相同的命名空间,直到到期日,客户端发送该cookie到服务器,存储它 Request.Cookies时

As a short explanation: When you set something in Response.Cookies, that cookie is sent to the client which stores it. On each subsequent Request to the same namespace, until the expiry date is reached, the client sends that cookie to the server, which stores it in Request.Cookies.

这篇关于为什么不把我的饼干吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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