ValidPlatform Attribute

ValidPlatformAttribute 是一個繼承自 AuthorizeAttribute 的擴充類別,它簡化了 Server to Server 端的 API 呼叫驗證,讓你可以毋須實作驗證的程式,直接使用提供的標籤進行驗證檢查

Super simple to use

該功能位於 using ZapLib.Security

Web API Controller 驗證

提供 Web Api Controller 新的標籤 [ValidPlatform] 用於驗證是否為信任的請求,使用方式如下:

// 在某一個 Controller 的 Action 中
[ValidPlatform]
[Route("test")]
public HttpResponseMessage test_file()
{
    // 如果驗證通過才會進入這個 Action    
}

Fetch 附加驗證

如果使用 Fetch 呼叫具有 [ValidPlatform] 驗證的 API,可以啟用 validPlatform 改為 true 便會自動附加驗證的資訊

Fetch2 f = new Fetch2("https://httpbin.org/post");
// 附加平台驗證資訊
f.validPlatform = true;
object res = f.post<object>(new { data = "123", result = true, number = 123 });

系統金鑰與上帝鑰匙

WebApiConfig.cs

ZapLib.Security.Const.Key = "新的金鑰";

為了開發人員方便,驗證留有一個後門 上帝鑰匙 可以直接通過驗證,預設為 nvOcQMfERrASHCIuE797,也可以在系統進入點修改它

WebApiConfig.cs

ZapLib.Security.Const.GodKey = "上帝鑰匙";

驗證原理與規格

平台驗證實作了 DES 方法,但是稍作改良,請求端會再 HTTP Header 附加 3 個欄位資料

規格:

key description
Channel-Signature 將傳送的資料以 MD5 加密後的字串,作為請求的簽章
Channel-Iv 一組每次都會亂數產生的加密種子
Channel-Authorization Channel-Signature + Channel-Iv + 系統金鑰 使用 DES 雜湊後的結果

驗證原理說明:

如果在 Controller 附加了 [ValidPlatform] 則會在進入 Action 之前,先將 Channel-Signature + Channel-Iv + 系統金鑰 在進行一次 DES 雜湊,比對 Channel-Authorization 數值是否相同,如果相同就會驗證通過。
驗證不通過時,會直接回傳 401 Unauthorized 未授權回應。

results matching ""

    No results matching ""