@manhng

Welcome to my blog!

Save Session Timeout in Web.config as XML Document

January 3, 2020 12:06

Save Session Timeout in Web.config as XML Document (edit)

using System;
using System.Diagnostics;
using System.IO;
using System.Xml;

namespace ConsoleApp11
{
public class Program
{
public static void Main(string[] args)
{
string webConfigFilePath = @"C:\inetpub\wwwroot\WebApp\web.config";

bool isSaveSuccess = SaveSessionTimeoutToWebConfig(webConfigFilePath, "480");
}

public static bool SaveSessionTimeoutToWebConfig(string filePathWebConfig, string sessionTimeout)
{
//
// Change timeout from 30 to 1440
// FROM: <sessionState timeout="30" />
// TO: <sessionState timeout="1440" />
// ----------
// FROM:
//<authentication mode="Forms">
// <forms name="WebApp.CookieName" loginUrl="~/Login.aspx" protection="All" timeout="30" path="/" requireSSL="true" slidingExpiration="true" enableCrossAppRedirects="false" cookieless="UseDeviceProfile"></forms>
//</authentication>
// TO:
//<authentication mode="Forms">
// <forms name="WebApp.CookieName" loginUrl="~/Login.aspx" protection="All" timeout="1440" path="/" requireSSL="true" slidingExpiration="true" enableCrossAppRedirects="false" cookieless="UseDeviceProfile"></forms>
//</authentication>

try
{
if (File.Exists(filePathWebConfig))
{
XmlDocument doc = new XmlDocument();
doc.Load(filePathWebConfig);

XmlNode sessionState = doc.DocumentElement.SelectSingleNode("//system.web//sessionState");

if (sessionState != null)
{
if (sessionState.Attributes["timeout"] != null && sessionState.Attributes["timeout"].Value == sessionTimeout)
{
//return true;
}
else
{
sessionState.Attributes["timeout"].Value = sessionTimeout;
doc.Save(filePathWebConfig);
}
}

XmlNode formsAuthentication = doc.DocumentElement.SelectSingleNode("//system.web//authentication//forms");

if (formsAuthentication != null)
{
if (formsAuthentication.Attributes["timeout"] != null && formsAuthentication.Attributes["timeout"].Value == sessionTimeout)
{
return true;
}
else
{
formsAuthentication.Attributes["timeout"].Value = sessionTimeout;
doc.Save(filePathWebConfig);
}
}
}
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
return false;
}
}
}
}

Session Expire

July 2, 2019 13:37

Session Expire (edit)

Anh Bùi Hải (Wize Solutions)

Session Expires

https://cbertolasio.wordpress.com/2010/11/28/recap-on-expired-sessions-and-idle-users-in-asp-net-mvc/

https://cbertolasio.wordpress.com/2010/11/27/using-an-actionfilter-to-redirect-to-login-when-session-expires/

https://cbertolasio.wordpress.com/2010/11/28/dealing-with-idle-clients-and-expiring-sessions-in-asp-net-mvc/

https://cbertolasio.wordpress.com/2010/11/28/using-an-actionfilter-to-redirect-to-login-when-session-expires-part2/

Display Session Expires

http://www.intstrings.com/ramivemula/articles/display-session-expire-popup-in-asp-net-mvc/

Dealing with Idle Clients and Expiring Sessions in ASP.NET MVC

https://cbertolasio.wordpress.com/2010/11/28/dealing-with-idle-clients-and-expiring-sessions-in-asp-net-mvc/

Handling Session and Authentication Timeouts in ASP.NET MVC

https://markfreedman.com/handling-session-and-authentication-timeouts-in-asp-net-mvc/

Session is kept idle on client post backs, and raising session expiry warning in ASP.NET MVC Web Application (HAY)

https://forums.asp.net/t/1952999.aspx?Session+is+kept+idle+on+client+post+backs+and+raising+session+expiry+warning+in+asp+net+MVC+web+application

@section Scripts {

<script type="text/javascript">
var totalTimeOut = 0;
var sessionTimeout = @Session.Timeout;
sessionTimeout *= 60;
totalTimeOut = sessionTimeout;
function ShowTime() {
$("#timeOut").text(sessionTimeout);
sessionTimeout -= 10;
if (sessionTimeout >= 10) {
//TODO
}
else {
alert("Session will be over!");
//TODO
}
if (sessionTimeout <= 0) {
clearInterval(interneal);
}
}

//If use the ajax (e.g. Jquery ajax) to call the server anywhere, this function should be call
function ResetTimeOut() {
sessionTimeout = totalTimeOut;
}
var interneal = window.setInterval("ShowTime()", 10000);
</script>
}

Categories

Recent posts