为什么当用户注销时,authenticateRequest 会在页面上触发?

发布于 2024-07-19 04:29:16 字数 275 浏览 6 评论 0原文

我的目标是在用户进行身份验证时写入 cookie。 我们正在使用一个蹩脚的框架,它隐藏了其源代码和事件模型,因此当我使用他们的登录控件时,我无法在其上设置会话超时!

不管怎样,我试图在用户登录时写入一个cookie,然后在后续页面浏览时刷新cookie过期时间(滑动过期)。

所以我想我最初可以在 global.asax 中的 Application_AuthenticateRequest 期间创建 cookie,但即使用户尚未登录,它似乎也会触发。

情况应该是这样吗?

My goal is to write a cookie when the user authenticates. We are using a crappy framework that hides its source code and event model so when I use their login control I can't set a session timeout on it!

Anyhow, I am trying to write a cookie when the user is logged in, and then refresh the cookie expire time on subsequent page views (sliding expiration).

So I figured I could initially create the cookie during Application_AuthenticateRequest in teh global.asax but that seems to be firing even when the user hasn't signed in yet.

Is that suppose to be the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉态萌生 2024-07-26 04:29:16

Application_AuthenticateRequest 在每个请求上都会触发,但如果您使用表单身份验证并且用户尚未登录,您会发现 HttpContext 的 User 属性(通过全局应用程序类文件中的 this.User 访问)的计算结果为 null ,而如果用户登录,它将评估为 IPrincipal 对象。

因此您可以执行以下操作:

Private Sub Application_AuthenticateRequest(ByVal pObjSender As Object, ByVal pEaDummy As EventArgs)
    If Me.User IsNot Nothing AndAlso Me.User.Identity.IsAuthenticated Then
        If Me.Request.Cookies("authCookieName") Is Nothing Then
            ' Create cookie
        Else
            ' Update cookie
        End If
    End If
End Sub

其中 authCookieName 是 cookie 名称。

The Application_AuthenticateRequest fires on each request, but if you are using forms authentication and the user haven't logged in yet, you will find that the User property of the HttpContext (accessed through this.User in the global application class file) evaluates to null, while it will evaluate to an IPrincipal object if the user is logged in.

So you can do something like this:

Private Sub Application_AuthenticateRequest(ByVal pObjSender As Object, ByVal pEaDummy As EventArgs)
    If Me.User IsNot Nothing AndAlso Me.User.Identity.IsAuthenticated Then
        If Me.Request.Cookies("authCookieName") Is Nothing Then
            ' Create cookie
        Else
            ' Update cookie
        End If
    End If
End Sub

where authCookieName is the cookie name.

萌︼了一个春 2024-07-26 04:29:16

是的。 每次请求访问网站时都会发生 Application_AuthenticateRequest。 AuthenticateRequest 以及进行身份验证还将检查并返回页面是否要进行授权。 某些页面需要从身份验证和授权检查中排除,例如登录页面。

根据您的情况,您还应该检查页面并排除登录序列中涉及的页面。

Yes. The Application_AuthenticateRequest will occur everytime a request hits the website. The AuthenticateRequest as well as doing the authentication will also check and return if Authorisation is to happen for the page. Some pages need to be excluded from authentication and authorisation checks, such as the login page.

For your situation you should also check the page and exclude those that are involved in the login sequence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文