網站首頁 編程語言 正文
早在2005年的時候,微軟隨著ASP.NET 推出了membership機制,十年磨一劍,如今的ASP.NET Identity是否足夠強大,一起來體會。
在VS2013下新建項目,選擇"ASP.NET Web應用程序。",點擊"確定"。
選擇"MVC"模版。
創建的網站包括三個核心組件:
- 1、Microsoft.AspNet.Identity.EntityFramework
這是基于ASP.NET Identity的Entity Framework實現,用來持久化ASP.NET Identity數據和架構,以及負責和SQL Server數據庫交互。
- 2、Microsoft.AspNet.Identity.Core
包含了ASP.NET Identity的核心接口,用來針對不同的持久層,比如Azure Table Storeage, NoSQL數據庫等做不同的實現。
- 3、Microsoft.AspNet.Identity.OWIN
OWIN是一個安全中間件,Microsoft在此基礎上作了再開發,如記錄日志,產生cookie的時候用到。
各組件的依賴關系如圖:
F5運行項目。
點擊右上角的注冊按鈕,填寫注冊信息,點擊"注冊"按鈕,新用戶注冊成功并呈登錄狀態。
點擊VS2013的"停止調試"按鈕。
存儲的數據放在了哪呢?
右鍵App_Data,點擊"在文件資源管理器中打開文件",原來數據庫被存放在項目文件夾App_Data下了。
如何查看這些數據呢?
點擊VS2013的左上角"服務器資源管理器",右鍵"DefaultConnection",從中可以查看所有的數據。
比如用戶數據被存放在表"AspNetUsers"中。
點擊"注冊"按鈕,是把請求交給了AcccountController的Register這個Action。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser() { UserName = model.UserName };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// 如果我們進行到這一步時某個地方出錯,則重新顯示表單
return View(model);
}
以上,
- 通過ApplicationUser來實例化一個用戶
- 通過UserManager的靜態、異步方法CreateAsync創建用戶
- 通過異步方法SignInAsync來讓用戶登錄
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
以上
- 所有驗證的事交給了AuthenticationManager,負責登錄登出
- 把創建ClaimsIdentity交給了UserManager
至于登出,請求交給了AccountController的LogOff。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
還沒有看到ASP.NET Idenity有多少過人之處,讓時間來告訴我們。
原文鏈接:https://www.cnblogs.com/darrenji/p/4443069.html
相關推薦
- 2022-09-07 pytest配置文件pytest.ini的具體使用_python
- 2022-05-13 iOS msgSend消息發送流程
- 2022-08-03 使用Apache?Camel表達REST服務的方法_Linux
- 2022-11-16 從Context到go設計理念輕松上手教程_Golang
- 2022-06-07 SQL?Server內存機制詳解_MsSql
- 2022-05-26 C語言算法練習之佩奇借書_C 語言
- 2022-07-18 win10無法訪問ubuntu共享文件夾(smbd出錯排查)
- 2022-06-21 使用Apache?Hudi?加速傳統的批處理模式的方法_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支