Redirección automática para iniciar sesión después del tiempo de espera de la sesión

I am trying to redirect automatically to my login page after session times out. I tried to add this code in my Main.Master page (all the other pages are connected to this master page):

protected void Page_Load(object sender, EventArgs e)
{
            //Redirects to Login Page 3 seconds before session timeout
            Response.AppendHeader("Redirect", Convert.ToString((Session.Timeout * 60) - 3) + "; URL=~/Login.aspx");
}

I configured the session timeout to 1 minute in my web config:

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

pero nada pasa

Can anyone help me find the problem with this code, or has other ideas how to make it work?

Editar: Authentication node from web.config

<authentication mode="Forms">
    <forms name=".CAuthenticated" loginUrl="Login.aspx" protection="All" 
    timeout="20"/>
</authentication>

preguntado el 31 de julio de 12 a las 15:07

can you show more from your web.config? That is where it should be handled, and you can specify a login page. -

Are you running this is IIS, or using Visual Studio's built in host? -

@ChrisKnight Visual Studio's built in host -

Response.AppendHeader("Redirect", 60) Can somebody point me to some reference about this. (If it's actually valid and works, that is) -

4 Respuestas

protected void Page_Init(object sender, EventArgs e)
{
    if (Session["Username"] == null)
    {
        Response.Redirect(ResolveClientUrl("~/login.aspx") + "?returnURL=" + HttpContext.Current.Request.Url.AbsolutePath);
    }
    else
    {
        lblUsername.Text = Session["Username"].ToString();
    }
}

Respondido el 11 de Septiembre de 12 a las 11:09

AppendHeader is documented as causing an exception if "header is appended after the HTTP headers have been sent" You need to make sure AppendHeader is called before the HTTP headers have been sent. Depending on your master page, the Load event might be too late. You could try the Init evento en su lugar.

Respondido 31 Jul 12, 16:07

when the redirection works, I get Requested URL: /website/Pages/~/Login.aspx Do you know why? and it seems like it takes the content page location and not the app root. - Inbal

~/Login.aspx is just text, if you want to translate that into a relative url use something like VirtualPathUtility.MakeRelative - pedro ritchie

Muchísimas gracias :) - Inbal

Creo que necesitas usar Refresh instead of `Redirect' in your header:

Response.AppendHeader("Refresh",
    Convert.ToString((Session.Timeout * 60) - 3) +
    ";URL=~/Login.aspx");

Respondido 31 Jul 12, 15:07

Here is an example I have that works for me:

    <authentication mode="Forms">
      <forms loginUrl="~/Login/Index" defaultUrl="~/Admin" timeout="20">
      </forms>
    </authentication>

If you have this, there is no need for you to check the timeout cookie yourself. This is assuming you are using Forms Authentication.

Respondido 31 Jul 12, 15:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.