在会话结束时访问成员资格用户 [英] Access Membership User On Session End

查看:89
本文介绍了在会话结束时访问成员资格用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试编写一些代码,以在用户放弃会话(通过超时或注销)后进行一些整理,但是想访问User对象,因此我知道会话属于谁

I'm currently trying to write a little bit of code to do some tidying up once a user abandons their session (either via time out or logging out) however would like to access the User object so I know who the session belongs to.

不幸的是HttpContext为null,因此我无法直接访问HttpContext.User或HttpContext.User.IsInRole甚至直接访问auth cookie.我了解造成这种情况的原因,但想知道会话超时时是否还有其他方法可以访问此信息?

Unfortunately HttpContext is null so I can't access HttpContext.User or HttpContext.User.IsInRole or even the auth cookie directly. I understand the reasons behind this but wonder if there is there any other way to get access to this information when a session times out?

除了在会话中复制某些信息这一明显答案之外,

That is apart from the obvious answer of duplicating some of the information in the session.

谢谢.

推荐答案

您可以使用 Global.asax Session_End 事件.会话过期后,会自动调用Session_End事件.

You can use Global.asax's Session_End event. Session_End event is called automatically when a Session expired.

问题是在没有当前请求的情况下调用了Session_End事件.结果,Session_End内部的 HttpContext.Current 为空.

The problem is Session_End event is called without a current request. As the result, HttpContext.Current is null inside Session_End.

如果要获取用户信息,则需要在用户成功登录后将其保存在Session中.

If you want to get user's information, you will need to save them inside Session as soon as user successfully logins.

private void Session_End(object sender, EventArgs e)
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set 
    // to StateServer or SQLServer, the event is not raised.

    var userName = Session["UserName"];
    var sessionId = Session.SessionID;
}

这篇关于在会话结束时访问成员资格用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆