<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識(shí)百科 - 正文

        淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證

        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:36:14
        文檔

        淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證

        淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證:ASP.NET終于可以跨平臺(tái)了,但是不是我們常用的ASP.NET, 而是叫一個(gè)ASP.NET Core的新平臺(tái),他可以跨Windows, Linux, OS X等平臺(tái)來部署你的web應(yīng)用程序,你可以理解為,這個(gè)框架就是ASP.NET的下一個(gè)版本,相對于傳統(tǒng)ASP.NET程序,它還是有一些不同的地方
        推薦度:
        導(dǎo)讀淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證:ASP.NET終于可以跨平臺(tái)了,但是不是我們常用的ASP.NET, 而是叫一個(gè)ASP.NET Core的新平臺(tái),他可以跨Windows, Linux, OS X等平臺(tái)來部署你的web應(yīng)用程序,你可以理解為,這個(gè)框架就是ASP.NET的下一個(gè)版本,相對于傳統(tǒng)ASP.NET程序,它還是有一些不同的地方

        ASP.NET終于可以跨平臺(tái)了,但是不是我們常用的ASP.NET, 而是叫一個(gè)ASP.NET Core的新平臺(tái),他可以跨Windows, Linux, OS X等平臺(tái)來部署你的web應(yīng)用程序,你可以理解為,這個(gè)框架就是ASP.NET的下一個(gè)版本,相對于傳統(tǒng)ASP.NET程序,它還是有一些不同的地方的,比如很多類庫在這兩個(gè)平臺(tái)之間是不通用的。

        今天首先我們在ASP.NET Core中來實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證,既登陸功能。

        前期準(zhǔn)備:

        1.推薦使用 VS 2015 Update3 作為你的IDE,下載地址://www.gxlcms.com/softjc/446184.html

        2.你需要安裝.NET Core的運(yùn)行環(huán)境以及開發(fā)工具,這里提供VS版://www.gxlcms.com/softs/472362.html

        創(chuàng)建項(xiàng)目:

        在VS中新建項(xiàng)目,項(xiàng)目類型選擇ASP.NET Core Web Application (.NET Core), 輸入項(xiàng)目名稱為TestBasicAuthor。

        接下來選擇 Web Application, 右側(cè)身份認(rèn)證選擇:No Authentication

        打開Startup.cs

        在ConfigureServices方法中加入如下代碼:

        services.AddAuthorization(); 

        在Configure方法中加入如下代碼:

        app.UseCookieAuthentication(new CookieAuthenticationOptions 
        { 
         AuthenticationScheme = "Cookie", 
         LoginPath = new PathString("/Account/Login"), 
         AccessDeniedPath = new PathString("/Account/Forbidden"), 
         AutomaticAuthenticate = true, 
         AutomaticChallenge = true 
        });
        

        完整的代碼應(yīng)該是這樣:

        public void ConfigureServices(IServiceCollection services) 
        { 
         services.AddMvc(); 
         
         services.AddAuthorization(); 
        } 
         
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
        { 
         app.UseCookieAuthentication(new CookieAuthenticationOptions 
         { 
         AuthenticationScheme = "Cookie", 
         LoginPath = new PathString("/Account/Login"), 
         AccessDeniedPath = new PathString("/Account/Forbidden"), 
         AutomaticAuthenticate = true, 
         AutomaticChallenge = true 
         }); 
         
         app.UseMvc(routes => 
         { 
         routes.MapRoute( 
         name: "default", 
         template: "{controller=Home}/{action=Index}/{id?}"); 
         }); 
        }
        

        你或許會(huì)發(fā)現(xiàn)貼進(jìn)去的代碼是報(bào)錯(cuò)的,這是因?yàn)檫€沒有引入對應(yīng)的包,進(jìn)入報(bào)錯(cuò)的這一行,點(diǎn)擊燈泡,加載對應(yīng)的包就可以了。

        在項(xiàng)目下創(chuàng)建一個(gè)文件夾命名為Model,并向里面添加一個(gè)類User.cs

        代碼應(yīng)該是這樣

        public class User
        {
         public string UserName { get; set; }
         public string Password { get; set; }
        }

        創(chuàng)建一個(gè)控制器,取名為:AccountController.cs

        在類中貼入如下代碼:

        [HttpGet] 
        public IActionResult Login() 
        { 
         return View(); 
        } 
         
        [HttpPost] 
        public async Task<IActionResult> Login(User userFromFore) 
        { 
         var userFromStorage = TestUserStorage.UserList 
         .FirstOrDefault(m => m.UserName == userFromFore.UserName && m.Password == userFromFore.Password); 
         
         if (userFromStorage != null) 
         { 
         //you can add all of ClaimTypes in this collection 
         var claims = new List<Claim>() 
         { 
         new Claim(ClaimTypes.Name,userFromStorage.UserName) 
         //,new Claim(ClaimTypes.Email,"emailaccount@microsoft.com") 
         }; 
         
         //init the identity instances 
         var userPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, "SuperSecureLogin")); 
         
         //signin 
         await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties 
         { 
         ExpiresUtc = DateTime.UtcNow.AddMinutes(20), 
         IsPersistent = false, 
         AllowRefresh = false 
         }); 
         
         return RedirectToAction("Index", "Home"); 
         } 
         else 
         { 
         ViewBag.ErrMsg = "UserName or Password is invalid"; 
         
         return View(); 
         } 
        } 
         
        public async Task<IActionResult> Logout() 
        { 
         await HttpContext.Authentication.SignOutAsync("Cookie"); 
         
         return RedirectToAction("Index", "Home"); 
        }
        

        相同的文件里讓我們來添加一個(gè)模擬用戶存儲(chǔ)的類

        //for simple, I'm not using the database to store the user data, just using a static class to replace it.
        public static class TestUserStorage
        {
         public static List<User> UserList { get; set; } = new List<User>() {
         new User { UserName = "User1",Password = "112233"}
         };
        }
        

        接下來修復(fù)好各種引用錯(cuò)誤。

        完整的代碼應(yīng)該是這樣

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Threading.Tasks;
        using Microsoft.AspNetCore.Mvc;
        using TestBasicAuthor.Model;
        using System.Security.Claims;
        using Microsoft.AspNetCore.Http.Authentication;
        
        // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
        
        namespace TestBasicAuthor.Controllers
        {
         public class AccountController : Controller
         {
         [HttpGet]
         public IActionResult Login()
         {
         return View();
         }
        
         [HttpPost]
         public async Task<IActionResult> Login(User userFromFore)
         {
         var userFromStorage = TestUserStorage.UserList
         .FirstOrDefault(m => m.UserName == userFromFore.UserName && m.Password == userFromFore.Password);
        
         if (userFromStorage != null)
         {
         //you can add all of ClaimTypes in this collection 
         var claims = new List<Claim>()
         {
         new Claim(ClaimTypes.Name,userFromStorage.UserName) 
         //,new Claim(ClaimTypes.Email,"emailaccount@microsoft.com") 
         };
        
         //init the identity instances 
         var userPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, "SuperSecureLogin"));
        
         //signin 
         await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties
         {
         ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
         IsPersistent = false,
         AllowRefresh = false
         });
        
         return RedirectToAction("Index", "Home");
         }
         else
         {
         ViewBag.ErrMsg = "UserName or Password is invalid";
        
         return View();
         }
         }
        
         public async Task<IActionResult> Logout()
         {
         await HttpContext.Authentication.SignOutAsync("Cookie");
        
         return RedirectToAction("Index", "Home");
         }
         }
        
         //for simple, I'm not using the database to store the user data, just using a static class to replace it.
         public static class TestUserStorage
         {
         public static List<User> UserList { get; set; } = new List<User>() {
         new User { UserName = "User1",Password = "112233"}
         };
         }
        }
        
        

        在Views文件夾中創(chuàng)建一個(gè)Account文件夾,在Account文件夾中創(chuàng)建一個(gè)名位index.cshtml的View文件。

        貼入如下代碼:

        @model TestBasicAuthor.Model.User
        
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
         <title></title>
        </head>
        <body>
         @using (Html.BeginForm())
         {
         <table>
         <tr>
         <td></td>
         <td>@ViewBag.ErrMsg</td>
         </tr>
         <tr>
         <td>UserName</td>
         <td>@Html.TextBoxFor(m => m.UserName)</td>
         </tr>
         <tr>
         <td>Password</td>
         <td>@Html.PasswordFor(m => m.Password)</td>
         </tr>
         <tr>
         <td></td>
         <td><button>Login</button></td>
         </tr>
         </table>
         }
        </body>
        </html>
        
        

        打開HomeController.cs

        添加一個(gè)Action, AuthPage.

        [Authorize]
        [HttpGet]
        public IActionResult AuthPage()
        {
         return View();
        }
        

        在Views/Home下添加一個(gè)視圖,名為AuthPage.cshtml

        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
         <title></title>
        </head>
        <body>
         <h1>Auth page</h1>
        
         <p>if you are not authorized, you can't visit this page.</p>
        </body>
        </html>
        

        到此,一個(gè)基礎(chǔ)的身份認(rèn)證就完成了,核心登陸方法如下:

        await HttpContext.Authentication.SignInAsync("Cookie", userPrincipal, new AuthenticationProperties
        {
         ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
         IsPersistent = false,
         AllowRefresh = false
        });
        

        啟用驗(yàn)證如下:

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
         app.UseCookieAuthentication(new CookieAuthenticationOptions
         {
         AuthenticationScheme = "Cookie",
         LoginPath = new PathString("/Account/Login"),
         AccessDeniedPath = new PathString("/Account/Forbidden"),
         AutomaticAuthenticate = true,
         AutomaticChallenge = true
         });
        }
        

        在某個(gè)Controller或Action添加[Author],即可配置位需要登陸驗(yàn)證的頁面。

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證

        淺談如何在ASP.NET Core中實(shí)現(xiàn)一個(gè)基礎(chǔ)的身份認(rèn)證:ASP.NET終于可以跨平臺(tái)了,但是不是我們常用的ASP.NET, 而是叫一個(gè)ASP.NET Core的新平臺(tái),他可以跨Windows, Linux, OS X等平臺(tái)來部署你的web應(yīng)用程序,你可以理解為,這個(gè)框架就是ASP.NET的下一個(gè)版本,相對于傳統(tǒng)ASP.NET程序,它還是有一些不同的地方
        推薦度:
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲AV日韩AV永久无码下载| 亚洲动漫精品无码av天堂| 亚洲免费视频播放| 久草视频在线免费看| 亚洲国产精品无码久久一区二区 | 一区二区三区视频免费观看| 免费高清在线影片一区| 久久久久亚洲国产| 国产精品视_精品国产免费| 欧美日韩亚洲精品| 亚洲人成无码www久久久| 国产日韩在线视频免费播放| 亚洲女同成av人片在线观看| 97在线视频免费播放| ww亚洲ww在线观看国产| 免费视频中文字幕| 一级有奶水毛片免费看| 久久久无码精品亚洲日韩蜜桃 | 亚洲福利一区二区三区| 一二三四免费观看在线视频中文版 | 国产成人精品免费午夜app| 国产成人亚洲合集青青草原精品 | 免费看美女被靠到爽| 激情吃奶吻胸免费视频xxxx| 亚洲午夜视频在线观看| 日本免费人成在线网站| 亚洲第一街区偷拍街拍| 狠狠亚洲婷婷综合色香五月排名| 免费av一区二区三区| 亚洲宅男精品一区在线观看| 啊v在线免费观看| 无人在线观看免费高清| 亚洲成a人片在线观看天堂无码| 亚洲片国产一区一级在线观看 | 国产精品黄页免费高清在线观看| 亚洲AV成人片色在线观看高潮| 国产卡二卡三卡四卡免费网址| 免费视频成人国产精品网站| 青青草原精品国产亚洲av| 国产成人无码a区在线观看视频免费| 国产美女视频免费观看的网站|