1. 程式人生 > >ASP.NET Core Authentication系列(一)理解Claim, ClaimsIdentity, ClaimsPrincipal

ASP.NET Core Authentication系列(一)理解Claim, ClaimsIdentity, ClaimsPrincipal

前言 === 首先我們來看一下在ASP\.NET時代,Authentication是如何使用的。下面介紹的是`System.Web.Security.FormsAuthentication`: ``` // 登入 System.Web.Security.FormsAuthentication.SetAuthCookie("userName", false); if(User.Identity.IsAuthenticated) { // 已認證 } // 退出登入 System.Web.Security.FormsAuthentication.SignOut(); ``` 這是一個最簡單的認證用法: 1. 使用者填寫賬號密碼並提交登入; 2. 伺服器應用通過`System.Web.Security.FormsAuthentication.SetAuthCookie(string userName, bool createPersistentCookie)`來生成Auth Cookie,並返回到browser; 3. 使用者進行下一個操作時,會帶上Auth Cookie發到伺服器應用; 4. 伺服器應用解析並繫結到`Controller.User`屬性上,通過`User.Identity.IsAuthenticated`判斷使用者是否已認證; 5. 最後通過`System.Web.Security.FormsAuthentication.SignOut()`來刪除Auth Cookie; 想要設定生成的Auth Cookie屬性,可以通過修改`web.co