【发布时间】:2013-09-19 15:45:10
【问题描述】:
这是一个 webform asp.net 4 应用程序。使用Formsauthentication方法。
web.config:
<sessionState
mode="InProc"
cookieless="false"
timeout="1"/>
<authentication mode="Forms">
<forms defaultUrl="~/Default.aspx"
loginUrl="~/Login.aspx"
slidingExpiration="true"
timeout="25" />
</authentication>
问题是,当用户注销时,我需要执行一些操作(例如,在数据库中记录某些内容)。
用户点击“退出”链接的情况非常简单。
现在我正在处理由于超时而注销的问题,我面临两种不同的情况:
- 会话到期,授权未到期
- 授权令牌过期,会话仍然有效
在场景 #1 中,我尝试了以下操作:
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
If Not String.IsNullOrEmpty(Session("Username")) Then
Try
' custom action..
FormsAuthentication.SignOut()
Session.Clear()
Session.Abandon()
Catch ex As Exception
' log the exception
End Try
End If
End Sub
但在这里我有两个大问题: User 在这种情况下不可用(即,我无法检查 User.Identity.isAuthenticated,因此我正在检查 Session("Username") ) 和 FormsAuthentication.SignOut() 引发 nullreferenceException。 如何从 FormsAuthentication“区域”注销用户?
场景 #2 更复杂,因为我读到授权过期时不会触发明确的事件。我的“意愿”是能够在到期前不久为用户执行相同的自定义操作。 有没有可能?
某种自定义身份验证提供程序能否让我以更好、更可靠的方式处理这些情况?
【问题讨论】:
标签: asp.net vb.net session forms-authentication asp.net-4.0