Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am developing a web application using an asp.net, When the user is inactive on the page through out the project. i should get a warning message in a popup window like your session is about to expire in some seconds. in that message box i should have two buttons named ''stay log in" and "log out". when i click on "stay log in button" the page should become active and popup should close. when i click on "log out" it should redirect to login page. Even not clicked any button before time count ends i should be redirected to login page.

What I have tried:

C#
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="cs.aspx.cs" Inherits="cms_video.cs" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.js"></script>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/jquery.ui/1.10.2/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.aspnetcdn.com/ajax/jquery.ui/1.10.2/themes/ui-lightness/jquery-ui.css" type="text/css" />
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .modalBackground
        {
            background-color: Black;
            filter: alpha(opacity=60);
            opacity: 0.6;
        }
        .modalPopup
        {
            background-color: #FFFFFF;
            width: 300px;
            border: 3px solid #0DA9D0;
            border-radius: 12px;
            padding: 0;
        }
        .modalPopup .header
        {
            background-color: #2FBDF1;
            height: 30px;
            color: White;
            line-height: 30px;
            text-align: center;
            font-weight: bold;
            border-top-left-radius: 6px;
            border-top-right-radius: 6px;
        }
        .modalPopup .body
        {
            padding: 10px;
            min-height: 50px;
            text-align: center;
            font-weight: bold;
        }
        .modalPopup .footer
        {
            padding: 6px;
        }
        .modalPopup .yes, .modalPopup .no
        {
            height: 23px;
            color: White;
            line-height: 23px;
            text-align: center;
            font-weight: bold;
            cursor: pointer;
            border-radius: 4px;
        }
        .modalPopup .yes
        {
            background-color: #2FBDF1;
            border: 1px solid #0DA9D0;
        }
        .modalPopup .no
        {
            background-color: #9F9F9F;
            border: 1px solid #5C5C5C;
        }
    </style>
    <script type="text/javascript">
        var timoutWarning = 40000;
       
        var warningTimer;
        
        function StartTimers() {
            warningTimer = setTimeout("SessionExpireAlert()", timoutWarning);
            
        }
        
        function SessionExpireAlert() {

            var timeout = 20000;
            
            var seconds = timer / 1000;
           
            
            document.getElementsByName("seconds").innerHTML = seconds;
            setInterval(function () {
                seconds--;
                document.getElementById("seconds").innerHTML = seconds;

            },1000);
            setTimeout(function () {
                //Show Popup before 20 seconds of timeout.
                $find("mpeTimeout").show();
            }, timeout - 19 * 1000);
            setTimeout(function () {
                
                window.location = "Default.aspx";
            }, timeout);
        };
        function ResetSession() {
            //Redirect to refresh Session.
            window.location = window.location.href;
        }
        function ResetTimers() {
            clearTimeout(seconds);
            SessionExpireAlert
                       }
        
    </script>
</head>
<body >
    <form id="form1"  runat="server" >
        <asp:ScriptManager ID="ScriptManager1" runat="server">
    <div>
        <asp:LinkButton ID="lnkFake" runat="server" />
        <ajaxToolkit:ModalPopupExtender ID="mpeTimeout" BehaviorID="mpeTimeout"  runat="server" PopupControlID="pnlPopup"
        TargetControlID="lnkFake" OkControlID="btnYes" CancelControlID="btnNo" BackgroundCssClass="modalBackground"
        >
    <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none">
        <div[>
            Session Expiring!
        
        <div class="body">
            Your Session will expire in <span id="seconds"></span> seconds.<br />
            Do you want to reset?
        </div>
        <div class="footer" align="right">
            <asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="yes" />
            <asp:Button ID="btnNo" runat="server" Text="No" CssClass="no" />
        </div>
    
    
    </div>
    </form>
</body>
</html>
Posted
Updated 29-Aug-19 6:15am
v2
Comments
vipan.net 5-Feb-16 6:59am    
check below link: hope it will help you
http://www.aspsnippets.com/Articles/Automatically-redirect-User-after-Session-Timeout-in-ASPNet.aspx
Chris Maunder 5-Feb-16 7:09am    
If this is your answer then post this as an answer, not as a question/comment
vipan.net 5-Feb-16 23:09pm    
Ok Sure @Chris .
Member 12251184 5-Feb-16 23:49pm    
i am sorry @chris. in the code which i have asked is session warning starts even the user is active. I dont want to start the timer when user is acyive
Member 12251184 5-Feb-16 23:46pm    
thanks for your reply. I used same code which you suggested. The problem is Timeout starts even when the user is active . i don't want to get warning message when the user is active.

check below link, hope it will help you.

Automatically redirect User after Session Timeout in ASP.Net
 
Share this answer
 
v2
Comments
Member 12251184 5-Feb-16 23:53pm    
I do not want to start the timer when user is active on page. it should start only when user becomes idle on page.
vipan.net 6-Feb-16 0:11am    
Its just a logic. Modify it according to your need.

check if user is idle using your best programming approach and start time in back-end(i.e don't show to user or in common language not visible to user.), show message to user.
vipan.net 6-Feb-16 0:20am    
check below thread


http://stackoverflow.com/questions/1009260/how-can-i-detect-with-javascript-jquery-if-the-user-is-currently-active-on-the-p

http://serkanyersen.github.io/ifvisible.js/

more link for you

https://github.com/thorst/jquery-idletimer

https://github.com/rigoneri/timeout-dialog.js
c# - Cookie Confusion with FormsAuthentication.SetAuthCookie() Method - Stack Overflow[^]

The parameter timeout you've found in /system.web/authentication/forms is the timeout (in minutes) of the duration of authentication ticket.

This means that after a certain amount of time of inactivity, a user is prompted to login again. If you try to check this My.Profile.Current.IsAuthenticated it will be false.

You can choose not to persist the cookie. In this situation if your ticket expires, your cookie expires too. The cookie (in case is persisted) has a purpose to remember the user if he/she comes back to your site.

You might want to persist your cookie for 10 years so the user will never have to insert username and password again, unless they've chosen to delete the cookie. The cookie is valid even if the browser is closed (when it is persisted).

Another important thing to remember is the parameter slidingExpiration:
if it's true your authentication ticket will be renewed every time there's activity on your site: refresh of the page etc.

What you can do - and what I've done - is to write your own cookie like this:

C#
FormsAuthenticationTicket authTicket = new
     FormsAuthenticationTicket(1, //version
     userName, // user name
     DateTime.Now,             //creation
     DateTime.Now.AddMinutes(30), //Expiration (you can set it to 1 month
     true,  //Persistent
     userData); // additional informations



Differences: slidingExpiration vs time Expiration vs Persistent cookie ?

If IIS App Pool recyles? Shutdown worker process if idle and recycle worker process Authentication cookie not expires, Session expires.

If modify Global.asax or Web.config, or Bin folder ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900