Pergunta

Im new developer in asp.net. I want to make a Session management that if 10 minutes passes without any action, the system will end the session and logout the user.

I searched about it and I found this code:

In Web.config file:

<sessionState
   mode="InProc"
   cookieless="true"
   timeout="10" />

And in the page we want to end the session:

public int Timeout { get; set; }

But when I tried it, it didn't work! I don't not know should I try it in a server rather than localhost or this code does not satisfy the purpose that I need ?

Foi útil?

Solução

try

public class PageBase : System.Web.UI.Page

{

protected override void OnPreRender(EventArgs e)

{

base.OnPreRender(e);

AutoRedirect();

}

public void AutoRedirect()

{

int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000);

string str_Script = @"

<script type='text/javascript'>

intervalset = window.setInterval('Redirect()'," + int_MilliSecondsTimeOut.ToString() + @");

function Redirect()

{

alert('Your session has been expired and system redirects to login page now.!\n\n');

window.location.href='/login.aspx';

}

</script>";

ClientScript.RegisterClientScriptBlock(this.GetType(), "Redirect", str_Script);

}

}

Read more How to do auto logout and redirect to login page when session expires using asp.net?

Auto redirect to login page when Session is expired

Outras dicas

Try this in page load

HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Clear();
Authentication.SignOut();
Response.Redirect("~/Login.aspx");
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top