【问题标题】:authentication cookie身份验证 cookie
【发布时间】:2010-07-28 01:14:11
【问题描述】:

使用 Asp.Net MVC 1,我有我的“登录”控件/页面...我选中“记住我”复选框并点击提交.. 在我的控制器中我有:

FormsAuth.SignIn(userName, password, rememberMe)

这个方法创建了持久化的cookie .ASPXAUTH,此时一切都很好。我在其他控制器中设置了一个断点,我注意到一旦我登录到网站......对于下一个“回发”或页面刷新... cookie .ASPXAUTH 已从 cookie 集合中消失...所以.. 当我回到站点时,即使我选择了“记住我”选项.. 它会再次使用登录表单询问我的凭据...

知道为什么会这样吗?


更新:

我认为这不是 MVC 的问题。我用 WebForms 创建了一个新应用程序,该页面包含 1 个文本框、1 个生成 cookie 的按钮……以及将 cookie 写入标签的其他按钮……http://screencast.com/t/MjQ1MTVmYWU

这是我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    HttpCookie cookie = new HttpCookie("test");
    cookie.Value = TextBox1.Text;
    cookie.Expires = DateTime.Now.AddMinutes(1);

    Response.Cookies.Add(cookie);
}//with a breakpoint here, I "watch" the Response.Cookies collection and I can see my "test" item there.

protected void Button2_Click(object sender, EventArgs e)
{//with a breakpoint here. the Response.Cookies collection is empty.
    Label1.Text = Response.Cookies["test"].Value ?? "null";
}

我得到了相同的结果.. cookie 已正确添加到 Response.Cookies 集合中,但在第二次回发中,该集合再次为空..

结果..标签的值为“null”。

也许是一些配置?我以前从未使用过 cookie。

【问题讨论】:

  • 我也尝试过添加自己的 cookie:Response.Cookies.Add(mycookie);并且添加成功,但是对于下一次“回发”,cookie 集合为空
  • 所有浏览器都会出现这种情况吗?
  • 什么浏览器?你不是在隐身模式吗?
  • 我在 IE8 和 Mozilla Firefox 3.6.8 都试过
  • 我注意到了别的东西..我正在使用 Mozilla 的工具提琴手.. 而 Visual Studio 2008 调试器告诉我 Cookies 集合是空的...提琴手捕获的请求/响应向我展示了 cookie……但我无法在 IE 和 localhost 中使用提琴手……

标签: asp.net-mvc authentication cookies webforms login


【解决方案1】:

正如我之前所说,这是我第一次使用 cookie...

问题是......为了检索值我必须使用集合 Request.Cookies 而不是 Response.Cookies......这听起来很合理,因为请求是从浏览器发送的......(我的错 xP)

所以,总而言之……

  1. Response.Cookies 来创建 cookie
  2. cookie 存储在浏览器中
  3. Request.Cookies 检索 cookie 的值

谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 2023-04-05
    • 2016-03-02
    • 2020-02-27
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多