【问题标题】:FormsAuthentication Cookie CachingFormsAuthentication Cookie 缓存
【发布时间】:2013-09-07 05:08:24
【问题描述】:

我的 authcookie 没有在浏览器关闭或其他用户登录时被丢弃。

我的 cookie 在这里声明:

If ModelState.IsValid Then

                'check username and password
                If model.pwd = db.users.First(Function(t) t.NT_id = model.NT_id).pwd Then

                    'create an authentication cookie
                    FormsAuthentication.SetAuthCookie(model.NT_id, False) 'set to false to destroy cookie on browser close

                    'redirect action if login is successful
                    Return RedirectToAction("Construction", "Home")
                Else
                    ModelState.AddModelError("", "Invalid Username or Password")
                End If
            End If
            Return View(model)

我知道这不会删除 cookie,因为我有一个变量可以显示 cookie 用户名

Public Shared uNT_id = If(HttpContext.Current.User.Identity.IsAuthenticated, HttpContext.Current.User.Identity.Name, System.Environment.UserName)

【问题讨论】:

  • 我觉得它实际上是在兑现信息,即使我已将 cookie 持久性设置为 false...

标签: vb.net asp.net-mvc-3 variables session-cookies formsauthentication


【解决方案1】:

通过使用 get 和 set 将所有变量转换为属性以阻止它们缓存,解决了这个问题。

Public Class userinfo
        Public Shared Property uNT_id As String
            Get
                If HttpContext.Current.User.Identity.IsAuthenticated Then
                    Return HttpContext.Current.User.Identity.Name
                Else
                    Return System.Environment.UserName
                End If
            End Get
            Set(value As String)
                If HttpContext.Current.User.Identity.IsAuthenticated Then
                    value = HttpContext.Current.User.Identity.Name
                Else
                    value = System.Environment.UserName
                End If
            End Set
        End Property

        Public Shared Property uid As String
            Get
                Return db_apps.app_users.First(Function(t) t.NT_id = uNT_id).app_user_id
            End Get
            Set(value As String)
                value = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).app_user_id
            End Set
        End Property

        Public Shared Property ussn As Integer
            Get
                Return db_apps.app_users.First(Function(t) t.NT_id = uNT_id).ssn
            End Get
            Set(value As Integer)
                value = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).ssn
            End Set
        End Property

        Public Shared Property upwd As String
            Get
                Return db_apps.app_users.First(Function(t) t.NT_id = uNT_id).pwd
            End Get
            Set(value As String)
                value = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).pwd
            End Set
        End Property

        Public Shared Property uname_first As String
            Get
                Return db_apps.app_users.First(Function(t) t.NT_id = uNT_id).name_first
            End Get
            Set(value As String)
                value = db_apps.app_users.First(Function(t) t.NT_id = uNT_id).name_first
            End Set
        End Property
    End Class

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-20
    • 2014-11-28
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2014-08-03
    相关资源
    最近更新 更多