發表文章

ASP.NET WebAPI2第006天_API安全防範Key的設計_API Key跟Token差異

圖片
  倘若我們設計的API任誰都能存取肯定會天下大亂 在一些公開資料平台 通常大多會設計有API金鑰的模式供他人存取,而金鑰注重在於 授權機制而非身分驗證 。 比方 Google API https://www.wfublog.com/2018/12/google-api-key-activate-quota.html 就能以此識別該專案呼叫的存取數目quota還剩多少跟費用計算 而在以前接觸過的 歐洲專利局OPS API存取 設計則還包含除了KEY 跟 Token等名詞 絕大部分人應該都知道 授權機制(Authorization) the process of validating whether ‘you have right to access’ the requested resource on the system. 確認你的身份之後提供給你許可權 例如Admin User可以修改資料,而General User只能閱讀資料。 身分驗證(Authentication) the process of validating ‘who you are’ by comparing the provided information with existing information data base of the system. 驗證使用者的身份 例如User希望以該平台會員的身份登入,那麼應用程式需要通過使用者名稱和密碼確認你真的是隸屬於此系統的會員。 API Key跟Token差異 以較廣義(不這麼嚴謹)跟通用的說法(有些其他的可能不是對應含意,先寫好避免被噴。) API Key (金鑰) 主要是 以一個專案應用、 終端(手機、平板、電腦) 為單位 ,金鑰注重在於 授權機制(Authorization) 。 白話一點可以理解為專for應用程式端所發放的 一組帳密且發放自提供API的原廠用於識別呼叫的專案 (即應用程式或網站) 可以理解生活應用中office啟用金鑰、台鐵高鐵買的票、門票 有的可能終生使用、有的則可能有有效期限。 token(使用者憑證) 或者 session key (id) 主要是 以一個user登入帳戶為單位 ,注重在於 身分驗證(Authentication) 。 使用者驗證:安全地驗證進行呼叫的使用者是否符合其宣稱的身分 使用...

ASP.NET WebAPI2第005天_API驗證

圖片
不管是面對User的WebSite或者網站程式端對端的API 都需要對傳入資料進行驗證 新增一個Customer的DTO Model  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 using System ; using System.Collections.Generic ; using System.ComponentModel.DataAnnotations ; using System.Linq ; using System.Web ; namespace MyWebAPI2_Test0.Models { public class Customer { public int Id { get ; set ; } [Required, StringLength(15)] public string Name { get ; set ; } [RegularExpression(@"^([\w\.\-] +) @ ([ \ w \ -]+)(( \ .( \ w){ 2 ,})+) $ ", ErrorMessage = " Email in not valid ")] public string Email { get ; set ; } [RegularExpression("^[0-9] + $ ", ErrorMessage = " Invalid Phone Number ")] public string Phone { get ; set ; } } } 一些正則表達式驗證補充(這邊用簡單還沒這麼嚴謹查檢) Email 格式驗證正則表達式 ^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$ ^([\w\.\-]+)@([\w\-]+)((\.(\w){2,})+)$ ^ 比對輸入列的啟始位置 $ 比對輸入列的結束位置 (pattern) 匹配pattern並獲取這一匹配 [字元...

ASP.NET WebAPI2第004天_請求內容回傳格式_客製化方法名稱_FromBody,FromUri用法

圖片
  雖然預設跑出來是json 但其實都還是建議要透過 Accept 設定好client side預期接收的格式 Accept indicates what kind of response from the server the client can accept.  可理解為發送端(client)希望能接收到的回應格式 Content-type always is about the content of the current request or response. 可理解為發送端(client or server)目前發出請求傳遞的資料格式 比方XML 發送的請求 1 2 3 GET /api/products HTTP/1.1 Host: localhost:59877 Accept: application/xml 請求回來的Response 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 GET http://localhost:59877/api/products: { "Network": { "addresses": { "local": { "address": "::1", "family": "IPv6", "port": 52394 }, "remote": { "address": "::1", "family": "IPv6", "port": 59877 } } }, "Request Headers": { "accept": "application/xml...

ASP.NET WebAPI2第003天_MVC專案及WebAPI專案比較_PostMan模擬各種HTTPVerbs呼叫操作與常見status code分類

圖片
Web API 專案建立方式 第一種.預設產生範本(會強制一併勾選MVC) 記得將解決方案與專案置於相同目錄中要取消勾選 預設產生的專案目錄與檔案結構如下 可以看到有分為MVC控制器跟API控制器 路由部分傳統大宗路由設置 也有分為MVC路由(~\App_Start\RouteConfig.cs) using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MyWebAPI2_Test1 { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } } 跟WebAPI路由(~\App_Start\WebApiConfig.cs) using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace MyWebAPI2_Test1 { public static class WebApiConfig { public st...