简体   繁体   English

如何检测用户何时使用会话超时注销?

[英]How to detect when users are logout with Session timeout?

I am implementing web-push notifications, it works fine except one problem with the Session timeout. 我正在实现网络推送通知,除了会话超时的一个问题外,它工作正常。 On our web, users will be log out after the Session is expired. 在我们的网站上,会话过期后用户将被注销。 However, at that time user may have closed the tab/ the browser. 但是,那时用户可能已经关闭了选项卡/浏览器。 How can I detect the time the Session expired so that I can stop sending notifications to this user. 如何检测会话过期的时间,以便停止向该用户发送通知。

Update: I have the Session which will be expired after 30 mins (sessionState timeout = "30"). 更新:我的会话将在30分钟后过期(sessionState超时=“ 30”)。 A user logins, and then closes the tab without logout; 用户登录,然后关闭选项卡而不注销; after 30 mins he will be logout. 30分钟后,他将退出。 But he still receives notifications. 但是他仍然收到通知。

I´m not complettly sure if this is the anwser but maybe it help you. 我不确定这是否是答案,但也许可以为您提供帮助。

First you can get a event when ever a user session is end. 首先,当用户会话结束时,您可以获取一个事件。 So in this event you can end the push notification. 因此,在这种情况下,您可以结束推送通知。

To achive this just add the following line of code to your Global.asax 为此,只需将以下代码行添加到您的Global.asax中

    protected void Session_End(object sender, EventArgs e)
    {
       //End push notification
    }

To achive that you are notice that a user close the browser or tap you need some javascript. 为使您注意到用户关闭浏览器或点击,您需要一些JavaScript。 In my example i do it with a HTTP-Request but you can also do it with some Sockets or everything else. 在我的示例中,我使用HTTP请求来执行此操作,但是您也可以使用某些套接字或其他操作来执行此操作。

    function SendPostRequest(eMail) {
    try {
        $.post(
            "/apiacc/sessionend",
            { Value: eMail },
            function (data, status, xhr) {
            }
        );
    } catch (error) {
        //Error catching
    }
}

    function SetInterval(eMail) {
        SendPostRequest(eMail);
        window.setInterval(() => { SendPostRequest(eMail); }, 300000);
    }

And i just start the function when ever a site is loaded. 当站点加载时,我就启动该功能。 So the JavaScript send every 5 Minute a Request. 因此,JavaScript每5分钟发送一次请求。 Which I save in the database (I use for example the E-Mailadress to authorization). 我将其保存在数据库中(例如,我使用E-Mailadress进行授权)。 On your Application start-up you create a background Task. 在应用程序启动时,您将创建一个后台任务。 Which will check every 5 minut if some of the session, dont get a call within the last 5 minutes. 这将每隔5分钟检查一次会话中的某些会话是否在最近5分钟内没有接到电话。 So you can end the session and the event in your global.asax will be called. 因此,您可以结束会话并在global.asax中调用该事件。

I hope this help? 希望对您有所帮助?

My suggestion is, 我的建议是

  1. save the username or any identification to session variable , Finally when Logout is clicked kill the session variable. 将用户名或任何标识保存到会话变量,最后单击“注销”,终止会话变量。

  2. Another Option is TempData in ASP.NET , it stores the value for next page. 另一个选项是ASP.NET中的TempData,它存储下一页的值。 check that variable first and then Give access to the Component. 首先检查该变量,然后再授予对组件的访问权限。

http://www.tutorialsteacher.com/mvc/tempdata-in-asp.net-mvc http://www.tutorialsteacher.com/mvc/tempdata-in-asp.net-mvc

when the page is closed , tempdata will automatically killed. 当页面关闭时,tempdata将自动终止。

Hope may this helpful. 希望对您有所帮助。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM