How can I redirect users to a login page after a timeout using C# in ASP.NET? RRS feed

  • Question

  • I’m working on an ASP.NET program with the code behind in C# and connected to a SQL Server database.  I want to create a timeout that sends them to the logout page after a certain number of minutes.  I’m not using session timeout because the timeout value will vary based on the user’s role.  The .aspx page has an UpdatePanel with a DetailsView that has a BoundField for the TimeOut in epoch seconds and another BoundField for the CurrentEpochSecond.  These fields are from the database using a select statement.  A second DetailsView contains a TemplateField that shows the TimeLeft using a SqlDataSource statement that selects the TimeOut minus the CurrentEpochSecond as TimeLeft.    This is triggered by an AJAX timer every 60 seconds and TimeLeft value is shown in the DetailsView. 

        <asp:DetailsView ID="DV_LoggedInTimeExpires" runat="server" 

        DataSourceID="SDS_LoggedInTimeExpires" Height="50px" Width="125px"

        AutoGenerateRows="False" ondatabound="DV_LoggedInTimeExpires_DataBound"



            <asp:TemplateField HeaderText="TimeLeft" SortExpression="TimeLeft">


                    <asp:Label ID="TimeLeftLabel" runat="server" Text='<%# Eval("TimeLeft") %>'></asp:Label>





        <asp:SqlDataSource ID="SDS_LoggedInTimeExpires" runat="server"

        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"

        SelectCommand="SELECT ESET - CRET AS TimeLeft FROM aspnet_Users WHERE

        (UserId = @UserId)"


            ProviderName="System.Data.SqlClient" >


            <asp:Parameter Name="UserId" Type="Object" />



    In the C# code, I’ve tried several statements.  When I put

        protected void DV_LoggedInTimeExpires_ItemUpdated(object sender,

        DetailsViewUpdatedEventArgs e)


          if (TimeLeft < 1)





    VS shows an error that says: “The name 'TimeLeft' does not exist in the current context”

    When I define TimeLeft as an int and change it to

        protected void DV_LoggedInTimeExpires_ItemUpdated(object sender,

        DetailsViewUpdatedEventArgs e)


          int TimeLeft;

          if (TimeLeft < 1)





    VS shows an error that says: “Use of unassigned local variable 'TimeLeft'”.

    If I change this and assign a number int TimeLeft = 1 or any other number then I don’t get an error but when I run the program nothing happens when the TimeLeft reaches a number less than 1.

    • Moved by CoolDadTx Wednesday, July 3, 2019 1:31 PM ASP.NET related
    Tuesday, July 2, 2019 4:52 PM

All replies

  • Hello,

    Please ask here https://forums.asp.net/25.aspx/1?Security

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Tuesday, July 2, 2019 8:39 PM