【问题标题】:asp.net save username as cookie, then autofillasp.net 将用户名保存为 cookie,然后自动填充
【发布时间】:2013-01-22 04:58:03
【问题描述】:

我正在尝试将用户在用户名文本框中输入的内容作为 cookie 保存,然后在后续访问时让登录表单自动使用 cookie 值填充文本框。我做错了什么?

这会将用户名保存为cookie值,但是当返回登录页面时,cookie值似乎返回null。

我目前有:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim UserName As TextBox = DirectCast(LoginUser.FindControl("UserName"), TextBox)
    If User.Identity.IsAuthenticated Then
        Response.Redirect("~/media")
    End If
    Dim UserNameCookie As New HttpCookie("User_Name")
    UserNameCookie.Secure = True
    UserNameCookie.HttpOnly = True
    UserNameCookie.Expires = DateTime.Today.AddDays(7)
    UserNameCookie.Value = UserName.Text
    Response.Cookies.Add(UserNameCookie)

    If Request.Cookies("UserName") IsNot Nothing Then
        UserName.Text = Request.Cookies("UserName").Value.ToString()
    End If

End Sub

【问题讨论】:

  • 您可以使用 IE 开发者工具或 FireCookie/FireBug 查看您的 cookie 吗?
  • 是的,我可以看到cookie,并且cookie值作为输入的用户名。

标签: asp.net cookies login


【解决方案1】:

将用户名更改为用户名。 Cookie 名称必须相同。

If Request.Cookies("User_Name") IsNot Nothing Then
   UserName.Text = Request.Cookies("User_Name").Value.ToString()
End If

【讨论】:

    猜你喜欢
    • 2017-12-16
    • 2019-06-26
    • 2018-05-14
    • 2020-08-06
    • 2023-03-31
    • 2019-07-04
    • 2010-11-29
    • 2017-12-24
    • 2023-03-14
    相关资源
    最近更新 更多