欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET Identity的基本用法

 更新時(shí)間:2022年10月22日 16:16:11   作者:Darren Ji  
這篇文章介紹了ASP.NET Identity的基本用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

早在2005年的時(shí)候,微軟隨著ASP.NET 推出了membership機(jī)制,十年磨一劍,如今的ASP.NET Identity是否足夠強(qiáng)大,一起來(lái)體會(huì)。

在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),用來(lái)持久化ASP.NET Identity數(shù)據(jù)和架構(gòu),以及負(fù)責(zé)和SQL Server數(shù)據(jù)庫(kù)交互。

  • 2、Microsoft.AspNet.Identity.Core

包含了ASP.NET Identity的核心接口,用來(lái)針對(duì)不同的持久層,比如Azure Table Storeage, NoSQL數(shù)據(jù)庫(kù)等做不同的實(shí)現(xiàn)。

  • 3、Microsoft.AspNet.Identity.OWIN

OWIN是一個(gè)安全中間件,Microsoft在此基礎(chǔ)上作了再開(kāi)發(fā),如記錄日志,產(chǎn)生cookie的時(shí)候用到。

各組件的依賴關(guān)系如圖:

F5運(yùn)行項(xiàng)目。

點(diǎn)擊右上角的注冊(cè)按鈕,填寫注冊(cè)信息,點(diǎn)擊"注冊(cè)"按鈕,新用戶注冊(cè)成功并呈登錄狀態(tài)。

點(diǎn)擊VS2013的"停止調(diào)試"按鈕。

存儲(chǔ)的數(shù)據(jù)放在了哪呢?

右鍵App_Data,點(diǎn)擊"在文件資源管理器中打開(kāi)文件",原來(lái)數(shù)據(jù)庫(kù)被存放在項(xiàng)目文件夾App_Data下了。

如何查看這些數(shù)據(jù)呢?

點(diǎn)擊VS2013的左上角"服務(wù)器資源管理器",右鍵"DefaultConnection",從中可以查看所有的數(shù)據(jù)。

比如用戶數(shù)據(jù)被存放在表"AspNetUsers"中。

點(diǎn)擊"注冊(cè)"按鈕,是把請(qǐng)求交給了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);
        }

以上,

  • 通過(guò)ApplicationUser來(lái)實(shí)例化一個(gè)用戶
  • 通過(guò)UserManager的靜態(tài)、異步方法CreateAsync創(chuàng)建用戶
  • 通過(guò)異步方法SignInAsync來(lái)讓用戶登錄
        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

至于登出,請(qǐng)求交給了AccountController的LogOff。

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            AuthenticationManager.SignOut();
            return RedirectToAction("Index", "Home");
        }

還沒(méi)有看到ASP.NET Idenity有多少過(guò)人之處,讓時(shí)間來(lái)告訴我們。

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論