简体   繁体   English

基类如何从Global.asax调用Session_End方法?

[英]How does the baseclass call the Session_End method from Global.asax?

I want to redirect the user to a login page once their session has expired. 我想在用户会话期满后将其重定向到登录页面。 I created a base page which will be inherited by each aspx code behind file. 我创建了一个基础页面,该基础页面将由文件后面的每个aspx代码继承。

What i would really like to know is how can you set the basepage method to automatically perform the redirect once the Session_End() method has been called on session timeout? 我真正想知道的是,一旦会话超时调用了Session_End()方法,您如何才能将basepage方法设置为自动执行重定向?

I have searched for over an hour on Google for a solution to this but still have come up short. 我已经在Google上搜索了一个多小时,以寻求解决方案,但是仍然很短。

I have never used the global.asax page before and it is my first time creating a basepage so any advice or explanations would be great thanks 我以前从未使用过global.asax页面,这是我第一次创建基础页面,因此任何建议或解释都将非常感谢

I don't think this is possible, because when the Session_End gets fired, the user is inactive for a long time and probably the connection has been closed already. 我认为这是不可能的,因为当Session_End被触发时,用户长时间处于不活动状态,并且可能已经关闭了连接。

What you need to do is run some sort of javascript timer in all pages, which would redirect the user when it has expired. 您需要做的是在所有页面中运行某种javascript计时器,该计时器将在用户过期后将其重定向。

Kind regards, 亲切的问候,

Edwin. 埃德温。

Your Question: how can you set the basepage method to automatically perform the redirect once the Session_End() method has been called on session timeout 您的问题: how can you set the basepage method to automatically perform the redirect once the Session_End() method has been called on session timeout

You can call your Base page method from global.asax file. 您可以从global.asax文件中调用基本页面方法。 This method you will call in the Session_End() [ which is called when session expires or ends ]. 您将在Session_End()中调用此方法[在会话过期或结束时调用]。 Inside this basePage method put your custom logic for redirection. 在此basePage方法中,放入用于重定向的自定义逻辑。

However, you should agree that If you need to call a PAGE method from outside, that's surely a bad design to an extent. 但是,您应该同意,如果需要从外部调用PAGE方法,那么在一定程度上肯定是不好的设计。

Base.aspx:

public class BasePage : Page
{

    public static void MyMethod()
    {
        ...
    }

}

Global.asax Global.asax中

void Session_End(object sender, EventArgs e) 
{
  BasePage.MyMethod();

}

Refer this post for more details: http://forums.asp.net/t/1426012.aspx/1 请参阅此帖子以获取更多详细信息: http : //forums.asp.net/t/1426012.aspx/1

Well, Authentication is based on a cookie (at least form authentication, I am not sure for windows based). 好吧,身份验证基于Cookie(至少是表单身份验证,对于基于Windows的身份我不确定)。 You could simply remove this cookie when you receive an authenticated request with no associated session. 当您收到没有关联会话的经过身份验证的请求时,只需删除此cookie。 On login, you create the session. 登录后,您将创建会话。

You could delete auth cokie with 您可以使用以下方式删除auth cokie

FormsAuthentication.SignOut();

Between myself and a work colleague we have managed to resolve this question. 在我自己和一个工作同事之间,我们设法解决了这个问题。

The solution is as follows: 解决方法如下:

Within my application i used a session variable to store the user name. 在我的应用程序中,我使用了一个会话变量来存储用户名。

Once the session timed out then of course the user name session variable would be cleared from memory. 一旦会话超时,则当然会从内存中清除用户名会话变量。 This was the trigger i needed to force the application to return to the index page. 这是我迫使应用程序返回索引页面所需的触发器。

Solution

1st step: 第一步:

//in the base class enter the following code //在基类中输入以下代码

   protected override void OnLoad(EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("~/SessionTimeout.aspx");
        }
        base.OnLoad(e);
    }

2nd step: 第二步:

For each aspx code behind file ie the aspx.cs file for web form, instead of inheriting from the default (System.web.UI.Page) inherit from the base page: 对于文件后面的每个aspx代码(即Web表单的aspx.cs文件),而不是从默认页面(System.web.UI.Page)继承,都从基页继承:

   public partial class SessionTimeout : BasePage
    {
    }

final step: 最后一步:

set the sessionState attributes 设置sessionState属性

<sessionState mode="InProc" cookieless="false" timeout="30"/>

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

相关问题 从global.asax中的Session_end重定向到另一个页面 - Redirecting to another page from Session_end in global.asax ASP.NET MVC4 - Session_End - 如何在Global.asax的Session_End中获取当前登录用户的名称? - ASP.NET MVC4 - Session_End - How can I get the currently logged on user's name in Session_End of Global.asax? 会话变量值并非始终在Global.asax Session_end中可用 - Session variable value not always available in Global.asax Session_end 如何处理 global.asax 中的 session 结尾? - How to handle session end in global.asax? ASP.net MVC 5 - 如何从 Global.asax.cs 中的 session_end() 事件重定向到操作方法 - ASP.net MVC 5 - How to redirect to action method from from session_end() event in Global.asax.cs 在数据库中注销日志并在Global.asax中的Session_End()后面删除cookie - With LogOff record in database and deleting cookie behind the Session_End ( ) in Global.asax global.asax中的调用操作方法 - call action method from global.asax 如何在global.asax中调用控制器方法? - How to call controller method in global.asax? 如何从Global.asax调用页面代码背后的方法? - How to call from Global.asax a method that is in a page's codebehind? 如何处理Global.asax之外的Session.End事件 - How to handle the Session.End event outside Global.asax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM