2016-05-19 77 views
0

如何在ASP.NET mvc中跨多个选项卡管理会话。就像我们说我已经在两个标签页中打开了相同的页面,然后我从一个标签中注销,那么我应该自动被重定向到另一个标签页。ASP.NET中的会话管理mvc

+0

看一看[此帖](https://syfuhs.net/2013/03/24/real-time-user-notification-and-session-管理与信号部分2 /)的一种方法使用SignalR。 – NightOwl888

回答

0

在基本控制器中使用会话和呼叫作为覆盖方法,以便在到期时重定向到登录页面。 下面的CustomPrincipal类文件使用using System.Security.Principal;

在一类使用会话的一个例子说SessionPersistor:

public static long UserId 
    { 
     get 
     { 


      if (HttpContext.Current == null) return 0; 
      if (HttpContext.Current.Session[userId] != null) 
       return Convert.ToInt64(GetObjectFromSession(userId)); 
      return 0; 
     } 
     set 
     { 

      SetItemInSession(value, userId); 

     } 
    } 

public static object GetObjectFromSession(string key) 
    { 
     return HttpContext.Current.Session[key]; 
    } 

在基地控制器:

protected override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     if (!string.IsNullOrEmpty(SessionPersister.UserName)) { filterContext.HttpContext.User = new CustomPrincipal(new CustomIdentity(SessionPersister.UserFullName)); } 
     else { InvalidRequest(filterContext, "101", "Access denied.", "Session expired or You dont have access to this page."); } 

     filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     filterContext.HttpContext.Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1)); 
     filterContext.HttpContext.Response.Cache.SetNoStore(); 
     filterContext.HttpContext.Response.AppendHeader("prod-tool", "no-cache"); 
     base.OnAuthorization(filterContext); 
    } 

创建CustomPrincipal类:(在使用本例的情况下)

public class CustomPrincipal : IPrincipal 
{  
public CustomPrincipal(CustomIdentity identity) 
    { 
     this.Identity = identity; 
    } 
} 
public class CustomIdentity : IIdentity 
{ 
    public CustomIdentity(string name) 
    { 
     this.Name = name; 
    } 
} 
0

使用过滤器进行授权处理 - 全局过滤器。
在配置中,您可以配置登录URL,以便用户在未被授权时应该被重定向到您的页面。
如果您的应用程序打开了第二个浏览器窗口(选项卡),则可以编写一个java脚本函数,它遍历所有子窗口并调用刷新。

如何找到子窗口:How to get the references of all already opened child windows