身份验证cookie

时间:2010-07-28 01:14:11

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

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

FormsAuth.SignIn(userName, password, rememberMe)

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

知道为什么会这样吗?


更新:

我认为这不是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”。

也许有些配置?我之前从未使用过cookies。

1 个答案:

答案 0 :(得分:0)

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

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

所以,总结......

  1. Response.Cookies创建cookie
  2. cookie存储在浏览器中
  3. Request.Cookies检索cookie的值
  4. 谢谢大家!