Detect when user session

Can someone tell me how to determine when an exipred user session is in asp?

I want to redirect users to the page after expiration of the session

Thanks Sp

+4
source share
3 answers

Use the Session_End () function in the global.asax file. See here for more details.

SO - end of session

EDIT: No, maybe it won't. See comments below.

If you need to redirect to the end of the session, can this do the trick for you?

private void Page_Load(object sender, System.EventArgs e) { Response.AddHeader("Refresh",Convert.ToString((Session.Timeout * 60) + 5)); if(Session["IsUserValid"].ToString()=="") Server.Transfer("Relogin.aspx"); } 

EDIT 2: Caution, this can become tricky if you have AJAX material.

I have seen examples where people put this in the page_load of the base page and all your .aspx pages from that base page. This will not allow you to add this code for every single page that you have.

Why does the first method not work (Session_End)? This function is called internally on the server when the session ends. Thus, there is no associated request / response for redirection or transmission. Ie, this function can be called by the server 20 minutes after closing browswer.

0
source

I assume that you need to perform a session check on the page group of your site, so a good way to do this is to declare a base class for all of your restricted pages. Sort of:

 public class BasePage : System.Web.UI.Page { protected override void OnInit(EventArgs e) { base.OnInit(e); if (Session["Context"] == null) { // do redirect } } } 

suppose that when you log in, you assign an object that represents the session to the session ["Context"]

Your pages inherit this class as:

 public partial class _Default : BasePage { ... } 
+1
source

You can check the HttpContext.Current.User.Identity.IsAuthenticated property, which will let you know if there is a authenticated user or not.

so ur page load

 if (!HttpContext.Current.User.Identity.IsAuthenticated) { //FormsAuthentication.RedirectToLoginPage(); Response.Redirect("~/Login.aspx"); } 
0
source

Source: https://habr.com/ru/post/1311516/


All Articles