首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

IsLine Framework 系列之十二:Session、Cookie 與安全支援完整指南

     編輯:關於.NET

IsLine Framework 的 IsLine.HttpContent.HttpContentProvider 命名空間提供了統一的 Cookie、Session 管理與加密安全支援。透過 web.config 宣告式配置,即可實現全站 Cookie 屬性一致性、加密透明化、Session 存取標準化,大幅簡化狀態管理程式碼。

1. CookieProvider:全站 Cookie 統一管理

1.1 配置區段

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

1.2 核心屬性解析

屬性對應 Cookie 屬性說明
Cryptography—True 啟用 IsLine.Security.Cryptography 加密/解密,需配置 TokenKey
IsHttpOnlyHttpOnly防止 XSS,需 IE6 SP1+ 支援
IsSecureSecure僅 HTTPS 傳輸,不加密本地檔案(本地加密請開 Cryptography)
DomainDomain跨子網站共享 Cookie,如 abc.com 覆蓋 www.abc.com、isline.abc.com

1.3 加密種子配置

<add key="IsLine.Security.Configuration.TokenKey" value="IsLine" />

1.4 介面與核心方法

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");

2. SessionProvider:標準化 Session 存取

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");

3. IsLine.Security.Cryptography:字串加解密工具

方法說明
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");

4. 結語

IsLine HttpContentProvider 以「配置驅動、介面統一、加密透明」三大特性,將分散在各處的 Cookie/Session 管理邏輯集中化、標準化。搭配 Cryptography 命名空間,安全性與開發效率雙贏。本系列下一篇將釋出完整原始碼與配置大全,敬請期待。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved