前言
ASP
盡管把存在的ASP應用程序轉換到ASP
概述
Cookie是Web應用程序識別用戶對話的最常用的方法
在本例中
ASP
在ASP
public class SessionPage : System
{
public new mySession Session = null;
}
自定義的對話類使用HybridDictionary對象來相應保存內存中的對話狀態(HybridDictionary可用於處理任意數量的對話元素)
[Serializable]
public class mySession
{
private HybridDictionary dic = new HybridDictionary();
public mySession()
{
}
public string this [string name]
{
get
{
return (string)dic[name
}
set
{
dic[name
}
}
}
Page類為定制暴露了不同的事件和方法
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base
}
private void InitializeComponent()
{
cookie = this
if (cookie == null)
{
Session = new mySession();
CreateNewSessionCookie();
IsNewSession = true;
}
else
Session = sessionPersistence
Server
dsn
SessionExpiration
);
this
}
private void CreateNewSessionCookie()
{
cookie = new HttpCookie(sessionPersistence
sessionPersistence
this
}
SessionPersistence類使用微軟
public mySession LoadSession(string key
int SessionExpiration)
{
SqlConnection conn = new SqlConnection(dsn);
SqlCommand LoadCmd = new SqlCommand();
LoadCmd
LoadCmd
SqlDataReader reader = null;
mySession Session = null;
try
{
LoadCmd
conn
reader = LoadCmd
if (reader
{
DateTime LastAccessed =reader
if (LastAccessed >= DateTime
Session = Deserialize((Byte[])reader[
}
}
finally
{
if (reader != null)
reader
if (conn != null)
conn
}
return Session;
}
private mySession Deserialize(Byte[] state)
{
if (state == null) return null;
mySession Session = null;
Stream stream = null;
try
{
stream = new MemoryStream();
stream
stream
IFormatter formatter = new BinaryFormatter();
Session = (mySession)formatter
}
finally
{
if (stream != null)
stream
}
return Session;
}
在請求的末尾
private void PersistSession(Object obj
{ sessionPersistence
Server
}
public void SaveSession(string key
mySession Session
{
SqlConnection conn = new SqlConnection(dsn);
SqlCommand SaveCmd = new SqlCommand();
SaveCmd
try
{
if (IsNewSession)
SaveCmd
else
SaveCmd
SaveCmd
SaveCmd
SaveCmd
conn
SaveCmd
}
finally
{
if (conn != null)
conn
}
}
private Byte[] Serialize(mySession Session)
{
if (Session == null) return null;
Stream stream = null;
Byte[] state = null;
try
{
IFormatter formatter = new BinaryFormatter();
stream = new MemoryStream();
formatter
state = new Byte[stream
stream
stream
stream
}
finally
{
if (stream != null)
stream
}
return state;
}
SessionPage類以及與它相關的類被封裝在SessionUtility組件中
From:http://tw.wingwit.com/Article/program/net/201311/12202.html