網(wǎng)站首頁 編程語言 正文
早在2005年的時(shí)候,微軟隨著ASP.NET 推出了membership機(jī)制,十年磨一劍,如今的ASP.NET Identity是否足夠強(qiáng)大,一起來體會。
在VS2013下新建項(xiàng)目,選擇"ASP.NET Web應(yīng)用程序。",點(diǎn)擊"確定"。
選擇"MVC"模版。
創(chuàng)建的網(wǎng)站包括三個(gè)核心組件:
- 1、Microsoft.AspNet.Identity.EntityFramework
這是基于ASP.NET Identity的Entity Framework實(shí)現(xiàn),用來持久化ASP.NET Identity數(shù)據(jù)和架構(gòu),以及負(fù)責(zé)和SQL Server數(shù)據(jù)庫交互。
- 2、Microsoft.AspNet.Identity.Core
包含了ASP.NET Identity的核心接口,用來針對不同的持久層,比如Azure Table Storeage, NoSQL數(shù)據(jù)庫等做不同的實(shí)現(xiàn)。
- 3、Microsoft.AspNet.Identity.OWIN
OWIN是一個(gè)安全中間件,Microsoft在此基礎(chǔ)上作了再開發(fā),如記錄日志,產(chǎn)生cookie的時(shí)候用到。
各組件的依賴關(guān)系如圖:
F5運(yùn)行項(xiàng)目。
點(diǎn)擊右上角的注冊按鈕,填寫注冊信息,點(diǎn)擊"注冊"按鈕,新用戶注冊成功并呈登錄狀態(tài)。
點(diǎn)擊VS2013的"停止調(diào)試"按鈕。
存儲的數(shù)據(jù)放在了哪呢?
右鍵App_Data,點(diǎn)擊"在文件資源管理器中打開文件",原來數(shù)據(jù)庫被存放在項(xiàng)目文件夾App_Data下了。
如何查看這些數(shù)據(jù)呢?
點(diǎn)擊VS2013的左上角"服務(wù)器資源管理器",右鍵"DefaultConnection",從中可以查看所有的數(shù)據(jù)。
比如用戶數(shù)據(jù)被存放在表"AspNetUsers"中。
點(diǎn)擊"注冊"按鈕,是把請求交給了AcccountController的Register這個(gè)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);
}
}
// 如果我們進(jìn)行到這一步時(shí)某個(gè)地方出錯(cuò),則重新顯示表單
return View(model);
}
以上,
- 通過ApplicationUser來實(shí)例化一個(gè)用戶
- 通過UserManager的靜態(tài)、異步方法CreateAsync創(chuàng)建用戶
- 通過異步方法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);
}
以上
- 所有驗(yàn)證的事交給了AuthenticationManager,負(fù)責(zé)登錄登出
- 把創(chuàng)建ClaimsIdentity交給了UserManager
至于登出,請求交給了AccountController的LogOff。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
AuthenticationManager.SignOut();
return RedirectToAction("Index", "Home");
}
還沒有看到ASP.NET Idenity有多少過人之處,讓時(shí)間來告訴我們。
原文鏈接:https://www.cnblogs.com/darrenji/p/4443069.html
相關(guān)推薦
- 2023-06-13 react實(shí)現(xiàn)組件狀態(tài)緩存的示例代碼_React
- 2022-08-28 Golang正則表達(dá)式判斷手機(jī)號或身份證方法實(shí)例_Golang
- 2022-08-13 圖像處理之matlab的取整函數(shù)round、ceil、floor和fix
- 2022-11-17 Android顯式Intent與隱式Intent的使用詳解_Android
- 2022-07-16 不同存圖方式下的DFS和BFS實(shí)現(xiàn)
- 2022-10-11 Spring Boot 使用@Scheduled注解實(shí)現(xiàn)定時(shí)任務(wù)
- 2022-05-04 C#設(shè)計(jì)模式之策略模式_C#教程
- 2022-11-19 Android權(quán)限詢問的實(shí)例詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支