日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

ASP.NET?Identity的基本用法_實(shí)用技巧

作者:Darren?Ji ? 更新時(shí)間: 2022-11-29 編程語言

早在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

欄目分類
最近更新