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

  • Thread starter Thread starter ArthurMeacham
  • Start date Start date
A

ArthurMeacham

Guest
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"

onitemupdated="DV_LoggedInTimeExpires_ItemUpdated">

<Fields>

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

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

</Fields>

</asp:DetailsView>

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

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

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

(UserId = @UserId)"

onselecting="SDS_LoggedInTimeExpires_Selecting"

ProviderName="System.Data.SqlClient" >

<SelectParameters>

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

</SelectParameters>

</asp:SqlDataSource>

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

protected void DV_LoggedInTimeExpires_ItemUpdated(object sender,

DetailsViewUpdatedEventArgs e)

{

if (TimeLeft < 1)

{

Response.Redirect("~/Logout.aspx");

}

}

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)

{

Response.Redirect("~/Logout.aspx");

}

}

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.

Continue reading...
 
Back
Top