ServerZoo虛擬主機使用教學文件區 Home      會員列表      Register Now!
舊 2011-10-31, 04:16 PM   #1
Tech-SZ 的頭像
 
Tech-SZ
最高權限

預設 [心得]Windows 主機 如何保持 sessions alive 超過20分鐘

ServerZoo IIS虛擬主機(Windows主機)的預設sessions time out 時間都是20分鐘,這是為了要符合PCI規範,但是因為Sessions 是server Wide的東西,有時候例如IIS軟重開時可能會遺失,在分享式虛擬主機下,都建議客戶"不要"使用主機Sessions改用Cookie 控制,可以參考以下各方法。另外把 sessions 存在資料庫,一般也是最建議的方式喔請參考微軟說明和教學

用cookie 維持sessions live
ASP.NET
http://support.microsoft.com/kb/899918
ASP
http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

我們ASP.NET的 StateServer 也有開啟,您也可以參考(或此討論串最下方回覆)
http://msdn.microsoft.com/en-us/libr...vs.100%29.aspx
http://king971119.blogspot.com/2009/...te-server.html

相關問題也可以參考:

http://www.blueshop.com.tw/board/sho...00804134503GQM

http://www.blueshop.com.tw/board/sho...r=cdt&odrtyp=1

http://www.blueshop.com.tw/board/FUM...4537534/2.html

或參考微軟教學
SQL Server 以儲存 ASP.NET 工作階段狀態
http://support.microsoft.com/kb/317604
http://blog.miniasp.com/post/2008/01...in-ASPNET.aspx

引用:
<sessionState mode="SQLServer"
 sqlConnectionString="data source=SQL主機路徑;user id=使用者<username>;password=DB使用者密碼<strongpassword>"
 cookieless="false" timeout="20" />


此篇文章於 2014-03-16 05:45 PM 被 Tech-SZ 編輯。.
Show SignatureDisplay Signature 回覆時引用此篇文章
舊 2011-10-31, 04:16 PM   #2
Tech-SZ 的頭像
 
Tech-SZ
最高權限

預設

Keep ASP.NET session alive using ASP.NET Ajax

To keep ASP.NET session alive, one more option is to use ASP.NET Ajax. Timer control is useful in this scenario, since it can send requests in regular time intervals. To see how it works, add to web form one UpdatePanel control and one Timer control. Here is an example markup code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax-Refresh.aspx.cs" Inherits="Ajax_Refresh" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10000" ontick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

</div>
</form>
</body>
</html>
Interval property of Timer1 is set to 10000, which is about 10 seconds. You can change this interval according to your needs.
On server side, we'll use Timer_Tick to keep session alive:
[ C# ]
using System;

public partial class Ajax_Refresh : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Set session timeout to small value, in this case
// 2 minutes, to see quickly if Timer will keep session alive
Session.Timeout = 2;
// Set some value in session
Session["Testing"] = "session is alive";
}

// Timer will make request to server in regular time intervals
protected void Timer1_Tick(object sender, EventArgs e)
{
// Write current session value into label
Label1.Text = (string)Session["Testing"];
Label1.Text += "<br /> Last request at " + DateTime.Now.ToString();
}
}
[ VB.NET ]
Partial Class Ajax_Refresh
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Set session timeout to small value, in this case
' 2 minutes, to see quickly if Timer will keep session alive
Session.Timeout = 2
' Set some value in session
Session("Testing") = "session is alive"
End Sub

' Timer will make request to server in regular time intervals
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' Write current session value into label
Label1.Text = Session("Testing")
Label1.Text &= "<br /> Last postback at " & DateTime.Now.ToString()
End Sub
End Class
Comparation of JavaScript, jQuery, Meta Refresh and ASP.NET Ajax methods

Notice that any of suggested methods (JavaScript, jQuery, Meta Refresh, ASP.NET Ajax) works only if user keeps web browser opened. If browser window is closed, session will normally expire after 20 minutes. If Session timeout is increased, ASP.NET will keep all sessions including useless sessions of visitors who leaved website. If website has high traffic, keeping of thousands of sessions could easily spend server's resources.
Refresh methods like these are more scalable than increasing of session timeout. They will keep only wanted sessions, where visitor is not closed a browser and discard others. Of course, you are not obligated to keep sessions on all website pages. Keeping sessions alive could be security problem, because there is an option that user is just forgot to close browser. In this case, it is better if session normally expire than if someone else comes to computer and gets access. You can limit this approach on just few pages where visitor needs more time to complete task.
Show SignatureDisplay Signature 回覆時引用此篇文章
舊 2013-06-26, 09:12 PM   #3
Tech-SZ 的頭像
 
Tech-SZ
最高權限

預設


source:
http://www.beansoftware.com/ASP.NET-...ion-Alive.aspx

How to keep ASP.NET session alive using JavaScript

ASP.NET just remembers time of last request and it doesn't know if visitor is closed browser window or is just doing something else and will return soon. It is certainly worthless to keep session values of user who leaved website. It would be better if we could keep live sessions of visitors who still have page opened.
Solution for this is to use JavaScript that will make periodic calls to some .aspx page on website, restart session timeout and keep session alive in that way. Implementation code will use JavaScript setInterval function. It could look like this:
<%--
In this example, image will be used to keep session alive,
By changing image's src parameter, we'll make periodical requests
to web server.
--%>
<img id="imgSessionAlive" width="1" height="1" />


<script type="text/javascript" >
// Helper variable used to prevent caching on some browsers
var counter;
counter = 0;

function KeepSessionAlive() {
// Increase counter value, so we'll always get unique URL
counter++;

// Gets reference of image
var img = document.getElementById("imgSessionAlive");

// Set new src value, which will cause request to server, so
// session will stay alive
img.src = "http://YourWebSiteUrl.com/RefreshSessionState.aspx?c=" + counter;

// Schedule new call of KeepSessionAlive function after 60 seconds
setTimeout(KeepSessionAlive, 60000);
}

// Run function for a first time
KeepSessionAlive();
</script>
In this example, RefreshSessionState.aspx page will be called every minute. This is far less than default session timeout which is 20 minutes. If you just want to keep session alive, you can set this time for 19 minutes (19 * 60 * 1000 = 1140000).
But, with smaller intervals you could know almost instantly when visitor is closed a browser. If scalability is a problem, you can delete session variables almost immediately after user closed web browser. There is no need to wait 20 minutes for session to expire. You can even decrease session timeout to low value, like 2 minutes. JavaScript from previous example will make requests every minute, and keep sessions alive for active users (users that have opened web browser), but sessions where browser is closed will expire.
Since RefreshSessionState.aspx page is called every minute, you can use ASP.NET server side code for tasks like visitor tracking, how many visitors are currently online, which page each visitor is currently browsing etc.
This option will work fine, although it has its own small drawbacks. Some users could have JavaScript disabled or have a browser that doesn't support JavaScript (like some mobile web browsers). If JavaScript is not enabled, this code would not work and session will expire. Also, rarely but theoretically possible, especially on mobile browsers is, if user's Internet connection is temporally broken JavaScript will miss few requests while user is reconnecting.
This example manipulates image's src element to make request to web server. There is a second option to make web requests in JavaScript using Http Request, but this option requires browser specific code because Internet Explorer and Firefox use different objects. IE uses ActiveX object Msxml2.XMLHTTP or Microsoft.XMLHTTP, while Firefox uses XMLHttpRequest. So, final browser compatible code becomes large. In the other hand, using image's src property to make request requires only one line of code.
Keep ASP.NET session alive using jQuery

As very similar alternative we can use jQuery for same task. In this example, I will use jQuery post function to make a request to web server. Pages requested with POST are not cached, so we don't need unique URL like in previous JavaScript example.
Code for keeping ASP.NET session alive using jQuery is very short:
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript" type="text/javascript">

function KeepSessionAlive() {
// 1. Make request to server
$.post("http://YourWebSiteUrl.com/RefreshSessionState.aspx");

// 2. Schedule new request after 60000 miliseconds (1 minute)
setInterval(KeepSessionAlive, 60000);
}

// Initial call of function
KeepSessionAlive(); Â
</script>
Keep ASP.NET session alive using Meta Refresh

One more way to keep ASP.NET session alive is by using Meta Refresh and postback. Of course, we can't refresh complete page because that will annoy visitor, especially if he or she is completing a large form. Instead of that, place small IFRAME tag somewhere on page, and set its src parameter to helper .aspx page. Let's call that page RefreshSessionState.aspx.
HTML code on main page will be:
<iframe height="0" width="0" src="RefreshSessionState.aspx" frameborder="0" />
Code for RefreshSessionState.aspx doesn't require complicated server side code except you want some visitor tracking. Just add meta refresh tag in head section. There are few different methods, I used Response.Write:
<%@ Page Language="C#" %>
<html>
<head>
<%
Response.Write(@"<meta http-equiv=""refresh"" content=""900;url=RefreshSessionState.aspx?x=" +
Server.UrlEncode(DateTime.Now.ToString()) + @""" />");
%>
</head>
<body>

</body>
</html>
I added additional query string "x", to avoid using of cache in some browsers. Query string value will be current time, so URL will always be unique to provide complete postback.
Now, first value in content parameter represents after how much seconds will page refresh. In this example, page will refresh after 900 seconds (15 minutes). It could be any value less than session timeout. Since default session timeout is 20 minutes, this IFRAME will keep session alive while user is working something else.
Show SignatureDisplay Signature 回覆時引用此篇文章
回覆

書籤

主題工具
顯示模式

發文規則
不可以發表新主題
不可以發表回覆
不可以上傳附件
不可以編輯自己的文章

啟用 BB 代碼
論壇啟用 表情符號
論壇啟用 [IMG] 代碼
論壇禁用 HTML 代碼

論壇跳轉


所有時間均為台北時間。現在的時間是 02:32 AM


Powered by vBulletin® 版本 3.8.0
版權所有 ©2000 - 2018,Jelsoft Enterprises Ltd.