今天來介紹IsLine.HttpContent.HttpContentProvider命名空間中關於Session與Cookie的支援。
這是IsLine FrameWork中一個常用的組件,使用這個組件可以為整站程式提供統一的Cookie配置屬性,例如Cookie加密方式、是否HttpOnly、是否開啟https安全選項、以及預設範圍。CookieProvider是該命名空間下的類,在該類初始化時,會自動載入web.config檔案中的Cookie節點配置,讀取成功後會載入於整個web網站,這些屬性不需要出現在程式中。
web.comfig節點配置:
在</configSections>與<appSettings>之間加入以下配置:
代碼
<IsLine.HttpContent.Configuration>
<Cookie>
<Cryptography>False</Cryptography>
<IsHttpOnly>True</IsHttpOnly>
<IsSecure>False</IsSecure>
<Domain>Default</Domain>
</Cookie>
</IsLine.HttpContent.Configuration>
在<configSections>與</configSections>之間加入以下節點:
<sectionGroup name="IsLine.HttpContent.Configuration">
<section name="Cookie" type="IsLine.Data.Configuration.SuitConfig"/>
</sectionGroup>
然後就可以在程式中使用這個Provider了。在<IsLine.HttpContent.Configuration>節點中,Cryptography表示是否啟用Cookie的加密,它有True和Fasle兩個值,如果值為True,在對Cookie進行建立或更新時,Provider將會自動啟用IsLine.Security.Cryptography命名空間下的Encrypting方法進行加密,在讀取Cookie時,Provider將會自動啟用IsLine.Security.Cryptography命名空間下的Decrypting方法進行解密,關於IsLine.Security.Cryptography資訊後面會有介紹。在加密與解密時,會用到種子字串,種子在web.config中添加節點進行配置:
<add key=" IsLine.Security.Configuration.TokenKey " value="IsLine"/>
配置好後,Cryptography節點的值就可以使用True了,這時查看硬碟上的Cookie資訊,就是已經加密過的了。
IsHttpOnly對應Cookie的HttpOnly屬性,該屬性指示是否只能在伺服器端擷取到使用者Cookie,如果該值為True,那麼Javascript、Flash等用戶端方法,將無法擷取到Cookie。這需要IE6的SP1以上版本的瀏覽器支援。
IsSecure對應Cookie的 Secure屬性,如果該屬性為True,那麼cookie就只能通過https或其他安全性通訊協定才能傳輸,在http中是無效的。置了secure屬性不代表其他人不能看到你機器本地儲存的cookie,只保證cookie與WEB伺服器之間的資料轉送過程加密,而儲存在本地的cookie檔案並不加密。如果想加密本機存放區的Cookie,請設定Cryptography節點為True。
Domain 對應Cookie的Domain屬性,該屬性指明了Cookie的範圍。比如有一個網站www.abc.com,後來又新增了類似isline.abc.com的網站,為了實現web網站的cookie共用,就需要對cookie的domain重新設定了,這時Domain值可以設定為abc.com,這樣基於abc.com的網站都可以共用Cookie了。
這就是CookieProvider的方便之處,只需要簡單的配置,就可以對整站Cookie進行統一的管理!
CookieProvider同樣實現了Capability介面,但它與Cache類不同的是,它只實現了IBaseCapability介面:
public class CookieProvider : IBaseCapability
{
public CookieProvider()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
}
IBaseCapability介面內容:
public interface IBaseCapability
{
bool Remove(string Name);
bool IsExit(string Name);
}
使用該Provider對Cookie的操作,必須制定一個Cookie名稱,不存在預設Cookie,這一點是與Cache不同的。使用Cookie可以通過類似以下代碼實現:
CookieProvider cp = new CookieProvider();
cp.GetContent(“Name”);
CookieProvider支援的方法如下:
方法 |
說明 |
string GetContent(string cookieName) |
獲得cookie內容 |
bool UpdateContent(string cookieName, string value) |
建立或更新一個cookie,如果cookie不存在,Provider會自動建立 |
bool UpdateContent(string cookieName, string value, DateType date, int howLong) |
同上。DateType指明到期時間的類型,可以是小時、分鐘等;howLong指明了到期的具體數字,與DateType搭配起來可以決定具體到期時間 |
bool Remove() |
移除網站下所有Cookie |
bool Remove(string cookiePrefixName, int cookiePrefixNameLength) |
刪除帶有某首碼的cookie,cookiePrefixName為首碼,cookiePrefixNameLength為前置長度(個數,從1計數) |
bool Remove(ArrayList cookieName, RemoveType rct) |
ArrayList傳入一組Cookie的名稱; RemoveType有兩個枚舉值:RemoveType.RemoveInput表示刪除傳入的ArrayList數組中的Cookie,RemoveType.RemoveAllExcludeInput表示保留傳入的ArrayList數組中的Cookie,網站中的其餘Cookie將被刪除。 |
bool Remove(string cookieName) |
移除指定Cookie,傳入Cookie名稱 |
bool Remove(int cookieNum) |
移除指定Cookie,傳入Cookie編號 |
bool IsExit(string cookieName) |
判斷指定Cookie是否存在,傳入Cookie名稱 |
bool IsExit(int cookieNum) |
判斷指定Cookie是否存在,傳入Cookie編號 |
下面來介紹一下IsLine.HttpContent.HttpContentProvider命名空間中的Session支援。
使用這個組件可以為整站程式提供統一的Session配置屬性,使用方法比較簡單,SessionProvider類同樣實現了ICapability,使用這個Provider的好處是為編程人員提供了與訪問Cache與Cookie同樣的訪問方式。
public class SessionProvider : ICapability
{
public SessionProvider()
{
//
// TODO: 在此處添加建構函式邏輯
//
}
}
使用方法:
SessionProvider sp = new SessionProvider();
sp.GetContent(sessionName);
方法列表:
方法 |
說明 |
object GetContent(string sessionName) |
獲得指定的Session內容 |
bool UpdateContent(string sessionName, object value) |
建立或更新一個Sesison,如果該Session不存在,Provider會建立 |
bool Remove() |
移除網站下所有Session |
bool Remove(string sessionName) |
移除指定Session |
bool IsExit(string sessionName) |
判斷指定Session是否存在 |
注意SessionProvider和CookieProvider的GetContent()方法,該方法在SessionProvider中傳回型別是object,在CookieProvider中返回string。因為Cookie是不可序列化的,Cookie中只能儲存string類型的變數;而Session是可序列化的,Session可以儲存object,所以才有以上的區別。所以使用SessionProvider的GetContent()取得Session後,還需要使用強制轉換的方法獲得所需的類型。
IsLine.HttpContent.HttpContentProvider 主要完成對Cache、Session、Cookie的支援,為編程人員提供統一的方法名與整站統一的屬性,如果你希望更改這些屬性只需修改設定檔,而不需修改程式。
下面來介紹最後一個命名空間,IsLine.Security.Cryptography命名空間。
該命名空間完成了string類型的加密與解密過程,它是由類Cryptography 完成的,該類方法比較多,使用者常用方法可以使用如下幾種:
方法 |
描述 |
static string StrengthEncrypting (string str,string key) |
在Encrypting方法的基礎上進行二次加密,會使用DES與Rijndael演算法,並在此基礎上按規則移動字串位置,實現加密,該方法為強加密方法。參數Key為種子 |
static string StrengthDecrypting (string str,string key) |
與強加密方法配合使用解密。參數Key為種子,種子需與強加密方法的種子一致。 |
static string Encrypting (string Source, string Key) |
加密一個傳入的字串,參數Key為種子。 |
static string Decrypting (string Source, string Key) |
配合Encrypting使用,為解密方法,Key為種子,種子需與Encrypting方法的種子一致。 |
使用者調用樣本:
引入命名空間:
Using IsLine.Security.Cryptography;
代碼:
代碼
string a = Cryptography. StrengthEncrypting(“加密內容”,”isline”) //強加密
Cryptography.StrengthDecrypting(a,”isline”); //配合強加密的解密
string b = Cryptography.Encrypting(“加密內容”,”isline”) //普通加密
Cryptography.Decrypting(b,”isline”); //配合普通加密的解密
這個命名空間使用比較簡單,就不在繼續闡述了。
至此,IsLine FrameWork的全部命名空間已經介紹完畢,下面一篇IsLine FrameWork系列文章將會公布原始碼以及配置資訊大全,敬請期待。
我是李鳴(Aicken) 歡迎您關注我的下一篇文章。
IsLine FrameWork是一個開源的企業級系統開發架構,目前已有以下系列:
IsLine FrameWork系列之一——第一次的親密接觸
IsLine FrameWork系列之二——命名空間與契約
IsLine FrameWork系列之三--七種武器
IsLine FrameWork系列之四--DataProvider 資料訪問(上)
IsLine FrameWork系列之五--DataProvider 資料訪問(中)
IsLine FrameWork系列之六--DataProvider 資料訪問(下)
IsLine FrameWork系列之七--AppLogProvider日誌架構(上)
IsLine FrameWork系列之八--AppLogProvider日誌架構(下)
IsLine FrameWork系列之九--ExceptionProcessProvider異常架構(上)
IsLine FrameWork系列之十--ExceptionProcessProvider異常架構(下)
IsLine FrameWork系列之十一--HttpContentProvider 訪問緩衝
IsLine FrameWork系列之十二--使用Session、Cookie與安全支援