ASP.NET 狀態(tài)的傳遞和保存(2)_.Net教程
8,Session原理: 把數(shù)據(jù)Value值存儲在服務器端并在客戶端存放Value對應的ID 。(ID,Value)都存放服務器 另外把ID以Cookie的形式存放客戶端。這樣就可以從客戶端Cookie中抓取ID,然后從服務器端讀取到ID對應的Value。
10,下面示例以Session原理實現(xiàn)頁面判斷用戶是否有成功登陸:成功登陸的用戶可以對特定頁面進行訪問、如果沒有成功登陸就跳轉(zhuǎn)到登陸頁面。
A. 添加類 SessionMgr.cs 在服務器端存儲 鍵值對 ID/Value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HttpNoStatus
{
public class SessionMgr
{
//定義鍵值對,存儲登陸信息
private static Dictionary<Guid, string> KeyValue = new Dictionary<Guid, string>();
//設置鍵值對的值
public static void SetKeyValue(Guid id, string value)
{
KeyValue[id] = value;
}
/// <summary>
/// 檢查客戶端傳遞過來的鍵值對是否存在
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool IfIdExist(Guid id)
{
return KeyValue.Keys.Contains(id);
}
//返回服務器端ID對應的Value值
public static string GetValue(Guid id)
{
return KeyValue[id].ToString();
}
}
}
B. 添加 LoginSession.ashx 判斷用戶是否登陸成功,如果登陸成功把存儲對應的鍵值對的值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HttpNoStatus
{
/// <summary>
/// LoginSession 的摘要說明
/// </summary>
public class LoginSession : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string strHtml = "";
//讀取用戶名和密碼
string strUserName = context.Request.Form["txtUserName"];
string strPwd = context.Request.Form["txtPassword"];
if (strPwd == "123456")
{
//登陸成功,設置對應的鍵值對
Guid id = Guid.NewGuid(); // 產(chǎn)生唯一的ID
SessionMgr.SetKeyValue(id, strUserName);
//id 保存在客戶端cookie中
HttpCookie loginCookie = new HttpCookie("LoginCookie");
loginCookie.Value = id.ToString();
loginCookie.Expires = DateTime.Now.AddDays(7);
context.Response.Cookies.Add(loginCookie);
//跳轉(zhuǎn)到授權(quán)頁面
context.Response.Redirect("AuthorizationPage.ashx");
}
else
{
//登陸失敗 , 加載登陸頁面
strHtml = Common_Nvelocity.RenderHTML("LoginSession.html", null);
context.Response.Write(strHtml);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
C. Templates文件夾下添加LoginSession.html 登陸頁面
<!DOCTYPE html>
<html xm lns="http://www.w3.org/1999/xhtml">
<head>
<me ta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form action="LoginSession.ashx" method="post">
<table>
<tr>
<td>登陸名</td>
<td>
<in put type="text" name="txtUserName" /></td>
</tr>
<tr>
<td>密碼</td>
<td>
<in put type="password" name="txtPassword" /></td>
</tr>
<tr>
<td>
<in put type="submit" name="Login" value="登陸" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
D. 添加AuthorizationPage.ashx頁面,只有登陸后的賬戶才有權(quán)限訪問這個頁面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace HttpNoStatus.Templates
{
/// <summary>
/// AuthorizationPage 的摘要說明
/// </summary>
public class AuthorizationPage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
//抓取客戶端 Cookie的ID值
HttpCookie loginCookie = context.Request.Cookies["LoginCookie"];
if (loginCookie != null)
{
Guid id = new Guid(loginCookie.Value);
// 讀取id對應的Value
string strValue = SessionMgr.GetValue(id);
//輸出Value值,并提示該賬號是已經(jīng)登陸的賬號
context.Response.Write(strValue + ",您已經(jīng)登陸本網(wǎng)站,有權(quán)限訪問此頁面");
}
//如果Cookie不存在,則直接跳轉(zhuǎn)到登頁面
else
{
context.Response.Redirect("LoginSession.ashx");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
------------------------------------------------------------gif 動畫演示----------------------------------------------------------------

11,上面的示例是也就是Session原理。Asp.net已經(jīng)內(nèi)置了Session機制,下面我們直接用ASP.NET Session實現(xiàn) 判斷用戶是否有登陸成功:
(一般處理程序HttpHandler操作Session, 要實現(xiàn)IRequiresSessionState接口)
分別添加頁面: LoginSessionNew.ashx(登陸一般處理程序) , LoginSessionNew.html(登陸模板), AuthorizationPageNew.ashx(登陸后才有權(quán)限訪問的頁面)。
A,LoginSessionNew.ashx(登陸一般處理程序)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace HttpNoStatus
{
/// <summary>
/// LoginSessionNew 的摘要說明
/// </summary>
public class LoginSessionNew : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string strHtml = "";
//讀取用戶名和密碼
string strUserName = context.Request.Form["txtUserName"];
string strPwd = context.Request.Form["txtPassword"];
if (strPwd == "123456")
{
//登陸成功,直接保存Session值
context.Session["LoginUserName"] = strUserName;
//跳轉(zhuǎn)到授權(quán)頁面
context.Response.Redirect("AuthorizationPageNew.ashx");
}
else
{
//登陸失敗 , 加載登陸頁面
strHtml = Common_Nvelocity.RenderHTML("LoginSessionNew.html", null);
context.Response.Write(strHtml);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
B,Templates模板下新建LoginSessionNew.html(登陸模板)
<!DOCTYPE html>
<html xm lns="http://www.w3.org/1999/xhtml">
<head>
<me ta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form action="LoginSessionNew.ashx" method="post">
<table>
<tr>
<td>登陸名</td>
<td>
<in put type="text" name="txtUserName" /></td>
</tr>
<tr>
<td>密碼</td>
<td>
<in put type="password" name="txtPassword" /></td>
</tr>
<tr>
<td>
<in put type="submit" name="Login" value="登陸" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
C,AuthorizationPageNew.ashx(登陸后才有權(quán)限訪問的頁面)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace HttpNoStatus
{
/// <summary>
/// AuthorizationPageNew 的摘要說明
/// </summary>
public class AuthorizationPageNew : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//檢查Session是否存在
ob ject obj = context.Session["LoginUserName"];
if (obj != null)
{
//Session存在,讀取Session值,并提示該賬號是已經(jīng)登陸的賬號
context.Response.Write(obj.ToString() + ",您已經(jīng)登陸本網(wǎng)站,有權(quán)限訪問此頁面");
}
//如果Session不存在,則直接跳轉(zhuǎn)到登頁面
else
{
context.Response.Redirect("LoginSessionNew.ashx");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
· ASP.NET內(nèi)置Session機制同樣實現(xiàn)了對用戶是否登陸成功的判斷:LoginSessionNew.ashx頁面Headers中我們看到了Cookie中多了ASP.NET_SessionId
Session機制在客戶端存放了ASP.NET_SessionID

· 權(quán)限訪問頁面,請求頭中讀取到了客戶端Cookie中的ASP.NET_SessionID

12, ASP.NET的Session機制: Session依賴于Cookie , 借助Cookie在客戶端瀏覽器中記錄了ID, 在服務器端存儲了Value值。
13,Session的值是放到了服務器內(nèi)存中,所以Session存放小數(shù)據(jù)。
Session(會話)有自動銷毀機制,如果一段時間內(nèi)瀏覽器沒有和服務器交互,則Session會定時自動銷毀。
登陸賬號后,一段時間內(nèi)如果不操作 系統(tǒng)就會自動退出,這就是Session自動銷毀了。
- asp圖片防盜鏈的代碼
- asp (author:killer)禁止站外提交表單的代碼
- 探索c#之遞歸APS和CPS
- ASP.NET MVC中將控制器分離到類庫的實現(xiàn)
- ASP.NET實現(xiàn)推送文件到瀏覽器的方法
- ASP.NET列出數(shù)據(jù)庫活躍鏈接的方法
- ASP.NET中使用Application對象實現(xiàn)簡單在線人數(shù)統(tǒng)計功能
- .NET程序調(diào)試技巧(一):快速定位異常的一些方法
- 淺談ASP.NET中多層架構(gòu)
- ASP.NET緩存處理類實例
- ASP.NET創(chuàng)建動態(tài)縮略圖的方法
- ASP.NET中MVC 4 的JS/CSS打包壓縮功能
- 相關(guān)鏈接:
- 教程說明:
.Net教程-ASP.NET 狀態(tài)的傳遞和保存(2)
。