IsLine Framework 的 IsLine.HttpContent.HttpContentProvider 命名空間提供了統一的 Cookie、Session 管理與加密安全支援。透過 web.config 宣告式配置,即可實現全站 Cookie 屬性一致性、加密透明化、Session 存取標準化,大幅簡化狀態管理程式碼。
<configSections>
<sectionGroup name="IsLine.HttpContent.Configuration">
<section name="Cookie" type="IsLine.Data.Configuration.SuitConfig" />
</sectionGroup>
</configSections>
<IsLine.HttpContent.Configuration>
<Cookie>
<Cryptography>False</Cryptography> <!-- 是否加密 -->
<IsHttpOnly>True</IsHttpOnly> <!-- 僅伺服器端可存取 -->
<IsSecure>False</IsSecure> <!-- 僅 HTTPS 傳輸 -->
<Domain>Default</Domain> <!-- 作用域 -->
</Cookie>
</IsLine.HttpContent.Configuration>
| 屬性 | 對應 Cookie 屬性 | 說明 |
|---|---|---|
| Cryptography | — | True 啟用 IsLine.Security.Cryptography 加密/解密,需配置 TokenKey |
| IsHttpOnly | HttpOnly | 防止 XSS,需 IE6 SP1+ 支援 |
| IsSecure | Secure | 僅 HTTPS 傳輸,不加密本地檔案(本地加密請開 Cryptography) |
| Domain | Domain | 跨子網站共享 Cookie,如 abc.com 覆蓋 www.abc.com、isline.abc.com |
<add key="IsLine.Security.Configuration.TokenKey" value="IsLine" />
public class CookieProvider : IBaseCapability {
string GetContent(string cookieName);
bool UpdateContent(string cookieName, string value);
bool UpdateContent(string cookieName, string value, DateType date, int howLong);
bool Remove(); // 移除全站 Cookie
bool Remove(string cookiePrefixName, int length); // 移除特定前綴
bool Remove(ArrayList names, RemoveType type); // 批次移除/保留
bool Remove(string cookieName);
bool Remove(int cookieIndex);
bool IsExit(string cookieName);
bool IsExit(int cookieIndex);
}
使用範例:
var cp = new CookieProvider();
cp.UpdateContent("UserName", "Admin", DateType.Days, 7); // 7 天有效
string name = cp.GetContent("UserName");
public class SessionProvider : ICapability {
object GetContent(string sessionName);
bool UpdateContent(string sessionName, object value);
bool Remove();
bool Remove(string sessionName);
bool IsExit(string sessionName);
}
關鍵差異:GetContent() 回傳 object(Session 可序列化存放任意物件),需自行轉型;Cookie 僅支援 string。
var sp = new SessionProvider();
sp.UpdateContent("Cart", new ShoppingCart());
var cart = (ShoppingCart)sp.GetContent("Cart");
| 方法 | 說明 |
|---|---|
StrengthEncrypting(str, key) | 強加密:DES + Rijndael + 字元位移 |
StrengthDecrypting(str, key) | 強解密,種子需一致 |
Encrypting(str, key) | 標準加密 |
Decrypting(str, key) | 標準解密 |
using IsLine.Security.Cryptography;
string enc = Cryptography.StrengthEncrypting("敏感資料", "isline");
string dec = Cryptography.StrengthDecrypting(enc, "isline");
IsLine HttpContentProvider 以「配置驅動、介面統一、加密透明」三大特性,將分散在各處的 Cookie/Session 管理邏輯集中化、標準化。搭配 Cryptography 命名空間,安全性與開發效率雙贏。本系列下一篇將釋出完整原始碼與配置大全,敬請期待。