asp.net core MVC之實現基於token的認證
安裝Nuget包
專案中新增包:dotnehttp://www.cppcns.comt add package Microsoft.AspNetCore.Authentication.JwtBearer
新增認證配置
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方法會向依賴注入容器新增認證服務和它所使用的其他服務,其引數defaultScheme用於指定當未指定具體的認證方案時將會使用的預設方案,上例為Bearer認證。
AddAuthentication方法的另一過載能夠使用AuthenticationOptions類為認證過程中的每一個動作指明所使用的認證方案,如DefaultAuthenticateScheme、
DefaultChallengeScheme、
DefaultSignInScheme、
DefaultSignOutScheme、
DefaultForbidScheme。
如果沒有為這些屬性設定認證方案,則將使用DefaultScheme屬性所指定的值。
當新增JwtBearer認證方式時,JwtBearerOptions物件能夠配置該認證的選項,它的TokenValidationParameters屬性用於指定驗證Token時的規則:
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驗證引數類,它包含了一些屬性,這些屬性如ValidateAudience、ValidateIssuer、ValidateLifetime和ValidateIssuerSigningKey,它們都是布林型別,用於指明是否驗證相應的項;而ValidIssuer和ValidAudience屬性則用於指明合法的簽發者(Issuer)與接受方(Audience)。在上例中,它們的值都從配置檔案中獲取;IssuerSigningKey屬性的值用於指定進行簽名驗證的安全金鑰,它的值為SymmetricSecurityKey物件,即對稱加密金鑰;ClockSkew屬性的值表示驗證時間的時間偏移值。
上述程式碼會從配置檔案中讀取關於Token的資訊,因此還需在appsettings.json中新增如下內容。
"Security": { "Token": { "Issuer": "demo_issuer","Audience": "demo_audience","Key": "<your_secret_key>" } }
為Controller新增認證
接下來,為了使用ASP.NET Core的認證功能來保護資源,應為Controller或Action新增[Authorize]特性,該特效能夠實現在訪問相應的Controller或Action時,要求請求方提供指定的認證方式,它位於Microsoft.AspNetCore.Authorization名稱空間中。需要為AuthorController和BookController新增該特性。
[Authorize] public class AuthorController : ControllerBase { } [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class BookController : ControllerBase { }
如果使用了多個認證方式,則可以使用[Authorize]特性的AuthenticationSchemes屬性指明當前Controller或Action要使用哪一種認證方式(如上例中的BookController);如果不設定,則會使用所新增認證時設定的預設方案;如果沒有設定預設方案,則會出現InvalidOperationException異常,並提示未指定預設方案;此外,如果為AuthenticationSchemes屬性指定了不存在的方案名稱,也會出現InvalidOperationException異常。
此時再訪問Book和Author資源,會出現401 Unauthorized異常:
如果要允許某個Action可以被匿名訪問,可以在Action方法上新增屬性標記 [AllowAnonymous]:
[AllowAnonymous] public async Task<ActionResult<IEnumerable<AuthorDto>>> GetAuthorsAsync([FromQuery] AuthorResourceParameters parameters)
新增認證資訊生成介面
JwtBearer中介軟體提供程式設計客棧了對JWT的驗證功能,然而並未提供生成Token的功能。要生成Token,可以使用JwtSecurityTokenHandler類,它位於System.IdentityModel.Tokens.Jwt名稱空間,它不僅能夠生成JWT,由於它實現了ISecurityTokenValidator介面,因此對JWT的驗證也是由它完成的。接下來,我們將建立一個Controller,它將會根據使用者的認證資訊生成JWT,並返回給客戶端。
在Controllers資料夾中建立一個Controller,名為AuthenticateController,內容如下:
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 AuthenticateControllerypcZiUfV : 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"],ypcZiUfV 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方法中,通過建立JwtSecurityToken物件,並使用JwtSecurityTokenHandler物件的WriteToken方法最終得到生成的JWT。當建立JwtSecurityToken物件時,我們可以指定issuer、audience以及當前使用者的Claim資訊,此外,還可以指定該Token的有效時間。這裡需要注意,由於JWT不支援銷燬以及撤回功能,因此在設定它的有效時間時,應設定一個較短的時間(如上例中的3分鐘),這樣可以有效避免Token在意外被竊取後所帶來的風險。
現在就可以請求認證介面獲取 token:
這時重新請求資源介面,在請求頭中新增Authorization項,值為Bearer ,就可以得到結果了:
這次示例中,使用了固定的使用者名稱和密碼,實際情況中,使用者名稱和密碼通常是存在資料庫中的,可以使用ASP.NET Core Identity來實現這一功能。
到此這篇關於asp.net core MVC之實現基於token的認證的文章就介紹到這了,更多相關asp.net core token的認證內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!