asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證
安裝Nuget包
項(xiàng)目中添加包:dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer
添加認(rèn)證配置
Startup類中添加如下配置:
public void ConfigureServices(IServiceCollection services) { ... services.AddAuthentication(defaultScheme: JwtBearerDefaults.AuthenticationScheme); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
AddAuthentication方法會(huì)向依賴注入容器添加認(rèn)證服務(wù)和它所使用的其他服務(wù),其參數(shù)defaultScheme用于指定當(dāng)未指定具體的認(rèn)證方案時(shí)將會(huì)使用的默認(rèn)方案,上例為Bearer認(rèn)證。
AddAuthentication方法的另一重載能夠使用AuthenticationOptions類為認(rèn)證過程中的每一個(gè)動(dòng)作指明所使用的認(rèn)證方案,如DefaultAuthenticateScheme、
DefaultChallengeScheme、
DefaultSignInScheme、
DefaultSignOutScheme、
DefaultForbidScheme。
如果沒有為這些屬性設(shè)置認(rèn)證方案,則將使用DefaultScheme屬性所指定的值。
當(dāng)添加JwtBearer認(rèn)證方式時(shí),JwtBearerOptions對(duì)象能夠配置該認(rèn)證的選項(xiàng),它的TokenValidationParameters屬性用于指定驗(yàn)證Token時(shí)的規(guī)則:
var tokenSection = Configuration.GetSection("Security:Token"); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters{ ValidateAudience = true, ValidateLifetime = true, ValidateIssuer = true, ValidateIssuerSigningKey = true, ValidIssuer = tokenSection["Issuer"], ValidAudience = tokenSection["Audience"], IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(tokenSection["Key"]) ), ClockSkew = TimeSpan.Zero }; });
TokenValidationParameters類作為Token驗(yàn)證參數(shù)類,它包含了一些屬性,這些屬性如ValidateAudience、ValidateIssuer、ValidateLifetime和ValidateIssuerSigningKey,它們都是布爾類型,用于指明是否驗(yàn)證相應(yīng)的項(xiàng);而ValidIssuer和ValidAudience屬性則用于指明合法的簽發(fā)者(Issuer)與接受方(Audience)。在上例中,它們的值都從配置文件中獲取;IssuerSigningKey屬性的值用于指定進(jìn)行簽名驗(yàn)證的安全密鑰,它的值為SymmetricSecurityKey對(duì)象,即對(duì)稱加密密鑰;ClockSkew屬性的值表示驗(yàn)證時(shí)間的時(shí)間偏移值。
上述代碼會(huì)從配置文件中讀取關(guān)于Token的信息,因此還需在appsettings.json中添加如下內(nèi)容。
"Security": { "Token": { "Issuer": "demo_issuer", "Audience": "demo_audience", "Key": "<your_secret_key>" } }
為Controller添加認(rèn)證
接下來,為了使用ASP.NET Core的認(rèn)證功能來保護(hù)資源,應(yīng)為Controller或Action添加[Authorize]特性,該特性能夠?qū)崿F(xiàn)在訪問相應(yīng)的Controller或Action時(shí),要求請(qǐng)求方提供指定的認(rèn)證方式,它位于Microsoft.AspNetCore.Authorization命名空間中。需要為AuthorController和BookController添加該特性。
[Authorize] public class AuthorController : ControllerBase { } [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class BookController : ControllerBase { }
如果使用了多個(gè)認(rèn)證方式,則可以使用[Authorize]特性的AuthenticationSchemes屬性指明當(dāng)前Controller或Action要使用哪一種認(rèn)證方式(如上例中的BookController);如果不設(shè)置,則會(huì)使用所添加認(rèn)證時(shí)設(shè)置的默認(rèn)方案;如果沒有設(shè)置默認(rèn)方案,則會(huì)出現(xiàn)InvalidOperationException異常,并提示未指定默認(rèn)方案;此外,如果為AuthenticationSchemes屬性指定了不存在的方案名稱,也會(huì)出現(xiàn)InvalidOperationException異常。
此時(shí)再訪問Book和Author資源,會(huì)出現(xiàn)401 Unauthorized異常:
如果要允許某個(gè)Action可以被匿名訪問,可以在Action方法上添加屬性標(biāo)記 [AllowAnonymous]:
[AllowAnonymous] public async Task<ActionResult<IEnumerable<AuthorDto>>> GetAuthorsAsync([FromQuery] AuthorResourceParameters parameters)
添加認(rèn)證信息生成接口
JwtBearer中間件提供了對(duì)JWT的驗(yàn)證功能,然而并未提供生成Token的功能。要生成Token,可以使用JwtSecurityTokenHandler類,它位于System.IdentityModel.Tokens.Jwt命名空間,它不僅能夠生成JWT,由于它實(shí)現(xiàn)了ISecurityTokenValidator接口,因此對(duì)JWT的驗(yàn)證也是由它完成的。接下來,我們將創(chuàng)建一個(gè)Controller,它將會(huì)根據(jù)用戶的認(rèn)證信息生成JWT,并返回給客戶端。
在Controllers文件夾中創(chuàng)建一個(gè)Controller,名為AuthenticateController,內(nèi)容如下:
using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Tokens; namespace Library.Api.Controllers { [ApiController, Route("api/auth")] public class AuthenticateController : ControllerBase { public IConfiguration Configuration { get; } public AuthenticateController(IConfiguration configuration) { Configuration = configuration; } [HttpPost("token", Name = nameof(GenerateToken))] public IActionResult GenerateToken(string username, string password) { if (username != "demouser" || password != "demopassword") { return Unauthorized(); } var claims = new List<Claim>{ new Claim(JwtRegisteredClaimNames.Sub, username) }; var tokenConfigSection = Configuration.GetSection("Security:Token"); var key = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(tokenConfigSection["Key"]) ); var signCredential = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var jwtToken = new JwtSecurityToken( issuer: tokenConfigSection["Issuer"], audience: tokenConfigSection["Audience"], claims: claims, expires: DateTime.Now.AddMinutes(3), signingCredentials: signCredential ); return Ok(new { token = new JwtSecurityTokenHandler().WriteToken(jwtToken), expiration = TimeZoneInfo.ConvertTimeFromUtc(jwtToken.ValidTo, TimeZoneInfo.Local) }); } } }
在AuthenticateController中的GenerateToken方法中,通過創(chuàng)建JwtSecurityToken對(duì)象,并使用JwtSecurityTokenHandler對(duì)象的WriteToken方法最終得到生成的JWT。當(dāng)創(chuàng)建JwtSecurityToken對(duì)象時(shí),我們可以指定issuer、audience以及當(dāng)前用戶的Claim信息,此外,還可以指定該Token的有效時(shí)間。這里需要注意,由于JWT不支持銷毀以及撤回功能,因此在設(shè)置它的有效時(shí)間時(shí),應(yīng)設(shè)置一個(gè)較短的時(shí)間(如上例中的3分鐘),這樣可以有效避免Token在意外被竊取后所帶來的風(fēng)險(xiǎn)。
現(xiàn)在就可以請(qǐng)求認(rèn)證接口獲取 token:
這時(shí)重新請(qǐng)求資源接口,在請(qǐng)求頭中添加Authorization項(xiàng),值為Bearer ,就可以得到結(jié)果了:
這次示例中,使用了固定的用戶名和密碼,實(shí)際情況中,用戶名和密碼通常是存在數(shù)據(jù)庫(kù)中的,可以使用ASP.NET Core Identity來實(shí)現(xiàn)這一功能。
到此這篇關(guān)于asp.net core MVC之實(shí)現(xiàn)基于token的認(rèn)證的文章就介紹到這了,更多相關(guān)asp.net core token的認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- ASP.NET Core MVC 過濾器的使用方法介紹
- asp.net core MVC 過濾器之ActionFilter過濾器(2)
- asp.net?core?MVC?全局過濾器之ExceptionFilter過濾器(1)
- ASP.NET Core MVC 修改視圖的默認(rèn)路徑及其實(shí)現(xiàn)原理解析
- ASP.NET Core MVC解決控制器同名Action請(qǐng)求不明確的問題
- 如何在Asp.Net Core MVC中處理null值的實(shí)現(xiàn)
- ASP.NET Core MVC如何實(shí)現(xiàn)運(yùn)行時(shí)動(dòng)態(tài)定義Controller類型
- ASP.NET Core MVC/WebApi基礎(chǔ)系列2
- ASP.NET Core MVC/WebApi基礎(chǔ)系列1
- 如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
- ASP.NET Core Mvc中空返回值的處理方法詳解
- ASP.NET Core MVC 過濾器(Filter)
相關(guān)文章
asp.net的web頁(yè)面(aspx)數(shù)據(jù)量過多時(shí)提交失敗對(duì)策
asp.net的web頁(yè)面,數(shù)據(jù)量過多時(shí)提交失敗的情況想必有很多朋友都有遇到過吧,下面與大家分享下詳細(xì)的解決方法2013-05-05.net core 6.0 通過依賴注入注冊(cè)和使用上下文服務(wù)的教程
在.NET Core 6.0 中,獲取上下文的方式取決于您使用的技術(shù)棧和具體的應(yīng)用程序類型,這篇文章主要介紹了.net core 6.0 通過依賴注入注冊(cè)和使用上下文服務(wù)的教程,需要的朋友可以參考下2023-12-12ASP.NET MVC驗(yàn)證碼功能實(shí)現(xiàn)代碼
ASP.NET MVC驗(yàn)證碼功能實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-06-06ASP.NET?Core中的Caching組件簡(jiǎn)介
這篇文章介紹了ASP.NET?Core中的Caching組件,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04ASP.NET MVC下Ajax.BeginForm方式無刷新提交表單實(shí)例
下面小編就為大家分享一篇ASP.NET MVC下Ajax.BeginForm方式無刷新提交表單實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Asp.Net 網(wǎng)站優(yōu)化系列之?dāng)?shù)據(jù)庫(kù)優(yōu)化分字訣上 分庫(kù)
當(dāng)我們的數(shù)據(jù)量很小的時(shí)候,我們會(huì)把用戶表,博客表,論壇表,閃存表等等都砸在一個(gè)庫(kù)里,我們的業(yè)務(wù)增長(zhǎng)的很好,在不久之后我們盡力的優(yōu)化了查詢,但是效果依然不佳,這時(shí)候用分字訣的時(shí)機(jī)到了。2010-06-06