MVC4网站:在超时时返回登录界面的正确方法 [英] MVC4 Web site: Proper way to return to Login screen on timeout

查看:425
本文介绍了MVC4网站:在超时时返回登录界面的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这条路上行驶,但仍然没有令人满意的结果。所以,让我从基础知识开始,这样我至少可以获得总体方向。



我们拥有什么:

- 使用VS2012,MVC4的网站,C#

- 支持桌面和移动视图(如果重要)

- 登录页面指向主页,链接打开新窗口中相同窗口或详细信息类型页面中的新页面

- 某些页面在Kendo UI窗格内实现部分视图



我正在寻找的东西:

当发生超时时(SessionState或表格 - 以最好者为准 - 我都试过)我想在它自己的窗口和其他窗口打开登录界面窗户应该关闭。无论用户在超时发生时哪个窗口都处于活动状态,都会发生这种情况。



现在发生了什么:

假设我在第二个窗口的某个详细页面上,并允许超时发生。我单击树视图中的一个链接,该链接应将部分视图加载到Kendo UI窗格中。会发生的事情是登录屏幕被加载到窗格中,我仍然打开了2个窗口。



此时我会接受任何帮助,无论如何多么一般或只是指向我错过的某个线程的指针。

I've been going down many roads with this, and still no satisfactory result. So let me start with just basics so I can at least get the general direction.

What we have:
- Web site using VS2012, MVC4, C#
- Supports desktop and mobile views (in case that matters)
- Login page directs to home page with links to open new pages in the same window or detail type pages in a new window
- some pages implement partial views inside Kendo UI panes

What I'm looking for:
When a timeout occurs (SessionState or forms - whichever is best - I tried both) I want to open the login screen in it's own window and other windows should be closed. This should happen no matter which window the user is active on when the timeout happens.

What happens now:
Let's say I was on one of the detail pages in a 2nd window and allowed a timeout to occur. I click on a link in the treeview that should load a partial view into a Kendo UI pane. What happens is that the login screen gets loaded into the pane and I still have 2 windows open.

At this point I'll take any help at all, no matter how general or just a pointer to some thread that I missed.

推荐答案

它不多,但是:

对于Forms身份验证超时我只需设置Web.Config并让它做到这一点:



It's not much, but:
For the Forms Authentication timeout I just set the Web.Config and let it do it's thing:

<authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="1"/>
    </authentication>





对于sessionState超时,我修改了我在其中一个帖子中找到的建议:



Web.Config:



For the sessionState timeout I modified a suggestion I found in one of these threads:

Web.Config:

<sessionstate timeout="1" />





在Global:asax.cs:

在RegisterGlobalFilters()中添加(从Application_Start()调用)





In the Global:asax.cs:
Added in RegisterGlobalFilters() (called from Application_Start())

filters.Add(new CheckSessionOutAttribute());





已添加这个类:



Added this class:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class CheckSessionOutAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower();
        if (!controllerName.Contains("account"))
        {
            HttpSessionStateBase session = filterContext.HttpContext.Session;
            var user = session["UserName"]; //Key 2 should be User or UserName
            if (((user == null) && (!session.IsNewSession)) || (session.IsNewSession))
            {
                //send them off to the login page
                var url = new UrlHelper(filterContext.RequestContext);
                var loginUrl = url.Content("~/Account/Login");
                filterContext.HttpContext.Response.Redirect(loginUrl, true);
            }
        }
    }
}





注意:对于我每次修改的测试Web.Config超时值,以便我知道我得到了哪一个。



我知道应用程序正在执行它所做的事情 - 点击KendoUI树视图指向详细信息窗格,这是登录屏幕加载的位置。我试图弄清楚如何拦截更高级别的所有内容,只是进入完整的登录屏幕,并关闭额外的页面。也许我在每个视图中都需要代码?



以下是树视图选择显示重定向的代码,但我需要采取全面的操作,无论页面是什么/触摸控制。





Note: for testing each I modify the Web.Config timeout values so that I know which one I'm getting.

I know the application is just doing what it's told to do - the click in the KendoUI treeview is directing to the detail-pane, which is where the Login screen in getting loaded. I'm trying to figure out how to intercept all that at a higher level and just go to a full login screen, and close extra pages. Perhaps I need code in every view?

Here is the code for the treeview select to show the redirect, but I need to take a comprehensive action no matter what page/control is touched.

function Selected(e) {
    var id =


( '#ItemId')VAL();
var treeItem = treeview.dataItem(e.node);
('#ItemId').val(); var treeItem = treeview.dataItem(e.node);


.post('@ Url.Action(_ Detail)',{
id:treeItem .id,actid:id
},function(data){
.post('@Url.Action("_Detail")', { id: treeItem.id, actid: id }, function (data) {


这篇关于MVC4网站:在超时时返回登录界面的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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