当会话过期时,如何为登录用户将状态标志设置为false [英] How can we set status flag to false for log in users , when session Expires

查看:73
本文介绍了当会话过期时,如何为登录用户将状态标志设置为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在使用MVC4,在我的网络应用程序中,我在数据库中将登录用户的状态标志设置为true。当用户注销时,我将状态设置为false。我在Session中保存userId,基于userId我这样做。

但是当会话过期时,我的userId id消失了。现在我们怎么做呢?

我需要设置状态标志为false,当Session Expires和浏览器关闭时。

我们怎么能这样做?

Hi all,

I am using MVC4, in my web application i am setting Status flag to true in database for logged in user's. when user's Log out, i am setting status to false. I am saving userId in Session , based on that userId i am doing this.
but when session Expired , my userId id gone. now how can we do this ?
I need to set status flag false, when Session Expires and browser closes.
How can we do this ?

推荐答案

1.对于会话过期,您可以通过在 MvcApplication Session_End 事件来完成此操作来自 Global.asax.cs 的$ c>类,如下例所示:

1.For session expiration, you could do this by managing Session_End event in MvcApplication class from your Global.asax.cs, like in the next example:
protected void Session_End(object sender, EventArgs e)
       {
           SiteSession siteSession = (Session["SiteSession"] == null ? null : (SiteSession)Session["SiteSession"]);
           using (MvcBasicSiteEntities db = new MvcBasicSiteEntities())
           {
               //Update the visitor log entry!
               VisitorLog.OnUserLogoff(db, (siteSession == null ? 0 : siteSession.VisitorLogID), true);
           }
       }



2.对于浏览器关闭,您应该使用布局中的JavaScript和AJAX调用来完成( _Layout.cshtml )如下例所示:


2.For browser closing you should do it by using JavaScript and AJAX call from your layout (_Layout.cshtml) like in the next example:

<script type="text/javascript">
        
        function onWindowClosing() {
            if (window.event.clientX < 0 || window.event.clientY < 0) {


.ajax({
type: POST
url: / Account / OnWindowClosing
});
}
};

function onKeydown(evt){
if (evt! = undefined && evt.altKey&& evt.keyCode == 115 // Alt + F4
{
.ajax({ type: "POST", url: "/Account/OnWindowClosing" }); } }; function onKeydown(evt) { if (evt != undefined && evt.altKey && evt.keyCode == 115) //Alt + F4 {


.ajax( {
类型: POST
url: / Account / OnWindowClosing
});
}
};

window .onbeforeunload = onWindowClosing;
window document .onkeydown = onKeydown;

< / script>
.ajax({ type: "POST", url: "/Account/OnWindowClosing" }); } }; window.onbeforeunload = onWindowClosing; window.document.onkeydown = onKeydown; </script>



在控制器中,管理浏览器关闭的操作将从业务逻辑调用相同的方法来更新访客日志。


And in the controller the action for managing the browser closing will invoke the same method from business logic for updating the visitors log.

public void OnWindowClosing()
        {
            SiteSession siteSession = this.CurrentSiteSession;
            VisitorLog.OnUserLogoff(_db, (siteSession == null ? 0 : siteSession.VisitorLogID), false);
            //
            Session.Clear(); // This is like a user log off!
        }



3.有关所有这些的详细信息和源代码,您可以使用我的下一篇文章: MVC基本站点:步骤4 - 使用AJAX,JSON,jQuery,LINQ和序列化的MVC 4.0中的jqGrid集成[ ^ ]


这篇关于当会话过期时,如何为登录用户将状态标志设置为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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