Write below java script function in .aspx page if u are not using master page..other wise write this function Master page .aspx page
<script language=JavaScript>
var secondsPassed = 0;
var minutesBeforeLoggedOut = 0;
var minutesToWarning = 0;
function ShowTimePassed() {
minutesBeforeLoggedOut = parseInt(minutesBeforeLoggedOut);
secondsPassed += 1;
if (minutesBeforeLoggedOut == -1) {
if (secondsPassed == 30) {
var img = new Image(1, 1);
img.src = 'KeepAlive.aspx?date=' + escape(new Date());
secondsPassed = 0;
}
}
else {
if (secondsPassed == minutesToWarning * 60) {
var answer;
var currentTime = new Date();
var expiredTime = new Date();
var minutes = expiredTime.getMinutes();
minutes += minutesBeforeLoggedOut;
expiredTime.setMinutes(minutes);
if (minutesBeforeLoggedOut == 1) {
answer = confirm("It is now " + currentTime.toLocaleTimeString() + " You have " + minutesBeforeLoggedOut + " minute left before getting logged out. Do you want to extend the session?");
}
else {
answer = confirm("It is now " + currentTime.toLocaleTimeString() + " You have " + minutesBeforeLoggedOut + " minutes left before getting logged out. Do you want to extend the session?");
} if (answer) {
var img = new Image(1, 1);
img.src = 'KeepAlive.aspx?date=' + escape(new Date());
secondsPassed = 0;
currentTime = new Date();
if (currentTime > expiredTime) {
alert("You've exceeded the time needed to extend the session. You will be logged out now");
window.location = loginUrl;
}
}
}
}
}
window.setInterval('ShowTimePassed()', 1000);
</script>
write below two function is .aspx.cs file
private void keepAlive()
{
//figure out the full url of the logout page configured in web.config + the return url which is the current page
String logoutUrl = FormsAuthentication.LoginUrl + "?ReturnUrl=" + Request.Url;
String minutesToWarning = "";
//figure out how many minutes before time out the user wants to get warned.
String minutesBeforeLoggedOut = "1";
if (minutesBeforeLoggedOut == null || minutesBeforeLoggedOut == "")
{
minutesBeforeLoggedOut = "1";
}
//if the user selected "-1", then it never gets timedout. If not, figure out, how many minutes after the page loaded, give the user the warning
if (!minutesBeforeLoggedOut.Equals("-1"))
{
int formsTimeout = GetFormsTimeout();
// int formsTimeout = Session.Timeout;
minutesToWarning = Convert.ToString(formsTimeout - Convert.ToInt32(minutesBeforeLoggedOut));
}
String script = @"
var minutesBeforeLoggedOut = " + minutesBeforeLoggedOut + @";
var loginUrl = '" + logoutUrl + @"';
if (!Page.ClientScript.IsClientScriptBlockRegistered("WarnTimeOut"))
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "WarnTimeOut", script);
}
private int GetFormsTimeout()
{
System.Xml.XmlDocument x = new System.Xml.XmlDocument();
x.Load(Request.PhysicalApplicationPath + "/web.config");
System.Xml.XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
int Timeout = int.Parse(node.Attributes["timeout"].Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
return Timeout;
}
{
//figure out the full url of the logout page configured in web.config + the return url which is the current page
String logoutUrl = FormsAuthentication.LoginUrl + "?ReturnUrl=" + Request.Url;
String minutesToWarning = "";
//figure out how many minutes before time out the user wants to get warned.
String minutesBeforeLoggedOut = "1";
if (minutesBeforeLoggedOut == null || minutesBeforeLoggedOut == "")
{
minutesBeforeLoggedOut = "1";
}
//if the user selected "-1", then it never gets timedout. If not, figure out, how many minutes after the page loaded, give the user the warning
if (!minutesBeforeLoggedOut.Equals("-1"))
{
int formsTimeout = GetFormsTimeout();
// int formsTimeout = Session.Timeout;
minutesToWarning = Convert.ToString(formsTimeout - Convert.ToInt32(minutesBeforeLoggedOut));
}
String script = @"
<script language=JavaScript>
var minutesToWarning = " + minutesToWarning + @";var minutesBeforeLoggedOut = " + minutesBeforeLoggedOut + @";
var loginUrl = '" + logoutUrl + @"';
</script>
";if (!Page.ClientScript.IsClientScriptBlockRegistered("WarnTimeOut"))
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "WarnTimeOut", script);
}
private int GetFormsTimeout()
{
System.Xml.XmlDocument x = new System.Xml.XmlDocument();
x.Load(Request.PhysicalApplicationPath + "/web.config");
System.Xml.XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
int Timeout = int.Parse(node.Attributes["timeout"].Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
return Timeout;
}
and call keepAlive(); function in Page_Load
protected void Page_Load(object sender, EventArgs e)
{
keepAlive();
}
{
keepAlive();
}
and in web.config dont forget to add these lines of code
< authentication mode="Forms">
<forms timeout="20"/>
</authentication>
If your project contains more than one folder and more than one master page..do the same procedure for every master page in every folder in your project
Thanks & Regards
Sathish Sirikonda
No comments:
Post a Comment