asp.net 狀態(tài)的傳遞和保存
來源:易賢網 閱讀:1172 次 日期:2015-08-12 15:06:47
溫馨提示:易賢網小編為您整理了“asp.net 狀態(tài)的傳遞和保存”,方便廣大網友查閱!

1,http協(xié)議是無狀態(tài)的。服務器不會記住上次給瀏覽器的處理結果,如果需要上次處理結果(上次狀態(tài))就需要瀏覽器把處理結果值(上次狀態(tài))再次給服務器。

2,url傳值:通過url參數或者通過form表單進行頁面件的傳值 (不能做到很自由的存取和讀取,而且不安全)

3,cookie :①cookie可以用來進行更加自由的數據的存取和讀取。

②cookie是和站點相關的,自己域名寫的只有自己的域名才可以讀取。

③客戶端向服務器發(fā)送請求的時候 處理發(fā)送form表單信息以外還會把和站點有關的所有的cookie發(fā)送給服務器,是強制的。

④服務器返回的數據處理html數據以外,還會返回修改的cookie,瀏覽器拿到修改后的cookie更新到本地的cookie

⑤服務器端使用cookie案例,記住用戶名功能:

a,設置頁面值: response.setcookie(new httpcookie("username",username))

b,讀取頁面值: username=request.cookies["username"].value

⑥瀏覽器關閉以后cookie的聲明周期到期,也就是cookie的默認生命周期是瀏覽器的生命周期??梢酝ㄟ^設置expires屬性設置cookie的過期時間:cookie.expires=datetime.now.adddays(-1)

⑦cookie在客戶端是以鍵值對存在的

4,cookie缺點:①客戶端額可以手動清楚cookie 所以cookie里面存放的信息是可有可無的信息

②瀏覽器對 cookie 的大小有限制,因此只有不超過 4096 字節(jié)才能保證被接受

③機密信息不能放到cookie里面

④cookie不能跨瀏覽器

5,cookie的寫和讀: a,新建cookietest.html頁面并添加 兩個按鈕分別用于cookie的讀和寫

代碼如下:

<!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>

<in put type="submit" name="read" value="讀取cookie" />

<in put type="submit" name="write" value="寫入cookie" />

<br />

讀取出來的cookie: $model.cookievalue

</form>

</body>

</html>

b,建立對應的cookietest.ashx頁面 實現cookie的新建寫入本地以及讀取cookie的值

using system;

using system.collections.generic;

using system.linq;

using system.web;

namespace httpnostatus

{

/// <summary>

/// httpcookie 的摘要說明

/// </summary>

public class cookietest : ihttphandler

{

public void processrequest(httpcontext context)

{

context.response.contenttype = "text/html";

//if else 判斷是點擊的那個按鈕

if (!string.isnullorempty(context.request["read"]))

{

if (context.request.cookies["age"] != null)

{

httpcookie cookie = context.request.cookies["age"];

string strvalue = cookie.value;

var data = new { cookievalue = strvalue };

//加載模板頁面并傳遞 cookie value的值

string strhtml = common_nvelocity.renderhtml("cookietest.html", data);

context.response.write(strhtml);

}

else

{

context.response.write("cookie 不存在");

}

}

else if (!string.isnullorempty(context.request["write"]))

{

//寫入新的cookie

httpcookie acookie = new httpcookie("age");

acookie.value = "25";

acookie.expires = datetime.maxvalue;

context.response.cookies.add(acookie);

//cookie不存在 直接加載模板頁面

string strhtml = common_nvelocity.renderhtml("cookietest.html", null);

context.response.write(strhtml);

}

else

{

//第一次加載頁面

string strhtml = common_nvelocity.renderhtml("cookietest.html", null);

context.response.write(strhtml);

}

}

public bool isreusable

{

get

{

return false;

}

}

}

}

6,cookie最主要的一個功能是保存用戶的登陸名,這樣用戶在下次登陸的時候系統(tǒng)就可以自動填寫登陸名稱

a,新建logincookie.html頁面,頁面中添加我們經常見到的 用戶名,用戶密碼,登陸

登陸頁面第一次加載的時候,設置默認的登陸名為空,登陸成功以及再次登陸的時候系統(tǒng)就自動補充登陸用戶名

<!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="logincookie.ashx" method="post">

<table>

<tr>

<td>登陸名</td>

<td>

<in put type="text" name="username" value="$model.loginuser" /></td>

</tr>

<tr>

<td>密碼</td>

<td>

<in put type="password" name="password" /></td>

</tr>

<tr>

<td>

<in put type="submit" name="login" value="登陸" /></td>

<td></td>

</tr>

</table>

</form>

</body>

</html>

b, 新建對應的logincookie.ashx頁面,實現把用戶名讀取出來并寫入cookie "ckloginuser"

using system;

using system.collections.generic;

using system.linq;

using system.web;

namespace httpnostatus

{

/// <summary>

/// logincookie 的摘要說明

/// </summary>

public class logincookie : ihttphandler

{

public void processrequest(httpcontext context)

{

context.response.contenttype = "text/html";

//加載頁面直接顯示 頁面

if (context.request.form["login"] == null)

{

string strhtml = "";

var data = new { loginuser = "" }; //登陸賬號默認為空

//判斷cookie是否存在,如果存在 把cookie的值傳遞到html頁面,如果不存在就是默認的空

if (context.request.cookies["ckloginuser"] != null)

{

data = new { loginuser = context.request.cookies["ckloginuser"].value.tostring() };

}

strhtml = common_nvelocity.renderhtml("logincookie.html", data);

context.response.write(strhtml);

}

else

{

//用戶登陸,保存用戶名到cookie

httpcookie loginuser = new httpcookie("ckloginuser");

loginuser.value = context.request.form["username"];

loginuser.expires = datetime.now.adddays(30);

context.response.cookies.add(loginuser);

//加載頁面直接顯示 頁面

string strhtml = common_nvelocity.renderhtml("logincookie.html", new { loginuser = context.request.form["username"] });

context.response.write(strhtml);

}

}

public bool isreusable

{

get

{

return false;

}

}

}

}

7,以上方法把登陸賬號以cookie的形式存放在客戶端,這樣每一次的請求就可以帶出用戶登陸名稱了

有一種情況: 用戶登陸成功以后就可以訪問網站的其他所有頁面,其他頁面就需要先判斷用戶是否登陸成功。

如果登陸成功為true放到cookie中,這樣的客戶端就可以進行篡改把false改為true從而可以非法訪問為授權頁面了,這樣放到cookie就不安全了。

如果登陸成功放到服務器端,那么網站的多個頁面就可以直接讀取到這個值,而且是安全的不會被客戶端篡改的了。

8,session原理: 把數據value值存儲在服務器端并在客戶端存放value對應的id 。(id,value)都存放服務器 另外把id以cookie的形式存放客戶端。這樣就可以從客戶端cookie中抓取id,然后從服務器端讀取到id對應的value。

10,下面示例以session原理實現頁面判斷用戶是否有成功登陸:成功登陸的用戶可以對特定頁面進行訪問、如果沒有成功登陸就跳轉到登陸頁面。

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(); // 產生唯一的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);

//跳轉到授權頁面

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頁面,只有登陸后的賬戶才有權限訪問這個頁面

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值,并提示該賬號是已經登陸的賬號

context.response.write(strvalue + ",您已經登陸本網站,有權限訪問此頁面");

}

//如果cookie不存在,則直接跳轉到登頁面

else

{

context.response.redirect("loginsession.ashx");

}

}

public bool isreusable

{

get

{

return false;

}

}

}

}

------------------------------------------------------------gif 動畫演示----------------------------------------------------------------

11,上面的示例是也就是session原理。asp.net已經內置了session機制,下面我們直接用asp.net session實現 判斷用戶是否有登陸成功:

(一般處理程序httphandler操作session, 要實現irequiressessionstate接口)

分別添加頁面: loginsessionnew.ashx(登陸一般處理程序) , loginsessionnew.html(登陸模板), authorizationpagenew.ashx(登陸后才有權限訪問的頁面)。

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;

//跳轉到授權頁面

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(登陸后才有權限訪問的頁面)

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值,并提示該賬號是已經登陸的賬號

context.response.write(obj.tostring() + ",您已經登陸本網站,有權限訪問此頁面");

}

//如果session不存在,則直接跳轉到登頁面

else

{

context.response.redirect("loginsessionnew.ashx");

}

}

public bool isreusable

{

get

{

return false;

}

}

}

}

· asp.net內置session機制同樣實現了對用戶是否登陸成功的判斷:loginsessionnew.ashx頁面headers中我們看到了cookie中多了asp.net_sessionid

session機制在客戶端存放了asp.net_sessionid

· 權限訪問頁面,請求頭中讀取到了客戶端cookie中的asp.net_sessionid

12, asp.net的session機制: session依賴于cookie , 借助cookie在客戶端瀏覽器中記錄了id, 在服務器端存儲了value值。

13,session的值是放到了服務器內存中,所以session存放小數據。

session(會話)有自動銷毀機制,如果一段時間內瀏覽器沒有和服務器交互,則session會定時自動銷毀。

登陸賬號后,一段時間內如果不操作 系統(tǒng)就會自動退出,這就是session自動銷毀了。

更多信息請查看IT技術專欄

更多信息請查看網絡編程
易賢網手機網站地址:asp.net 狀態(tài)的傳遞和保存
關于我們 | 聯系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點

版權所有:易賢網