ASP.NET Core使用JWT認證授權(quán)的方法
demo地址: https://github.com/william0705/JWTS
名詞解析
認證 : 識別用戶是否合法
授權(quán): 賦予用戶權(quán)限 (能訪問哪些資源)
鑒權(quán): 鑒定權(quán)限是否合法
Jwt優(yōu)勢與劣勢
優(yōu)勢
1、 無狀態(tài)
token 存儲身份驗證所有信息 , 服務(wù)端不需要保存用戶身份驗證信息, 減少服務(wù)端壓力 , 服務(wù)端更容易水平擴展, 由于無狀態(tài), 又會導(dǎo)致它最大缺點 , 很難注銷
2、 支持跨域訪問
Cookie是不允許垮域訪問的,token支持
3、 跨語言
基于標(biāo)準化的 JSON Web Token (JWT) , 不依賴特定某一個語言 , 例如生成的Token可以對多種語言使用(Net , Java , PHP …)
劣勢
1、Token有效性問題
后臺很難注銷已經(jīng)發(fā)布的Token , 通常需要借助第三方儲存(數(shù)據(jù)庫/緩存) 實現(xiàn)注銷, 這樣就會失去JWT最大的優(yōu)勢
2、占帶寬
Token長度(取決存放內(nèi)容) 比session_id大 , 每次請求多消耗帶寬 , token只存必要信息 , 避免token過長
3、需要實現(xiàn)續(xù)簽
cookies – session 通常是框架已經(jīng)實現(xiàn)續(xù)簽功能, 每次訪問把過期時間更新, JWT需要自己實現(xiàn), 參考OAuth2刷新Token機制實現(xiàn)刷新Token
4、消耗更多CPU
每次請求需要對內(nèi)容解密和驗證簽名這兩步操作,典型用時間換空間
只能根據(jù)自身使用場景決定使用哪一種身份驗證方案 , 沒有一種方案是通用的,完美的
.NET Core集成JWT認證授權(quán)服務(wù)
1、認證服務(wù)API:認證用戶,并發(fā)布Token
1、引入nuget包,System.IdentityModel.Tokens.Jwt
2、創(chuàng)建生成Token的服務(wù),建議使用面向接口和實現(xiàn)編程,方便服務(wù)注入容器ServicesCollection(涉及DI和IOC概念)
3、創(chuàng)建接口
namespace JWTS.Services { public interface IJWTService { /// <summary> /// 根據(jù)驗證通過后的用戶以及角色生成Token,以達到角色控制的作用 /// </summary> /// <param name="userName"></param> /// <param name="role"></param> /// <returns></returns> string GetToken(string userName,string role); } }
4、在appsettings.config中添加生成token需要的信息,并映射成對象
"TokenParameter": { "Issuer": "William", //這個JWT的簽發(fā)主體(發(fā)行者) "Audience": "William", //這個JWT的接收對象 "SecurityKey": "askalsnlkndhasnaslkasmadka" } public class TokenParameter { public string Issuer { get; set; } public string Audience { get; set; } public string SecurityKey { get; set; } }
5、實現(xiàn)接口,注入Configuration,獲取TokenParameter對象
using Microsoft.Extensions.Configuration; using System; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; using Microsoft.IdentityModel.Tokens; namespace JWTS.Services { public class JWTService : IJWTService { private readonly TokenParameter _tokenParameter; public JWTService(IConfiguration configuration) { _tokenParameter = configuration.GetSection("TokenParameter").Get<TokenParameter>(); } /// <summary> /// JWT由三部分組成(Header、Payload、Signature) /// {Header}.{Payload}.{Signature} /// </summary> /// <param name="userName"></param> /// <param name="role"></param> /// <returns></returns> public string GetToken(string userName,string role) { Claim[] claims = new[] { new Claim(ClaimTypes.Name, userName), new Claim("NickName","Richard"), new Claim("Role",role)//傳遞其他信息 }; SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenParameter.SecurityKey)); SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); /** * Claims (Payload) Claims 部分包含了一些跟這個 token 有關(guān)的重要信息。 JWT 標(biāo)準規(guī)定了一些字段,下面節(jié)選一些字段: JWT會被加密,但是這部分內(nèi)容任何人都可以讀取,所以不要存放機密信息 iss: The issuer of the token,token 是給誰的 sub: The subject of the token,token 主題 exp: Expiration Time。 token 過期時間,Unix 時間戳格式 iat: Issued At。 token 創(chuàng)建時間, Unix 時間戳格式 jti: JWT ID。針對當(dāng)前 token 的唯一標(biāo)識 除了規(guī)定的字段外,可以包含其他任何 JSON 兼容的字段。 * */ var token = new JwtSecurityToken( issuer: _tokenParameter.Issuer, audience: _tokenParameter.Audience, claims: claims, expires: DateTime.Now.AddMinutes(10),//10分鐘有效期 signingCredentials: creds); string returnToken = new JwtSecurityTokenHandler().WriteToken(token); return returnToken; } } }
6、jwt中定義好的Claims
JWT標(biāo)準里面定好的claim有:
- iss(Issuser):代表這個JWT的簽發(fā)主體;
- sub(Subject):代表這個JWT的主體,即它的所有人;
- aud(Audience):代表這個JWT的接收對象;
- exp(Expiration time):是一個時間戳,代表這個JWT的過期時間;
- nbf(Not Before):是一個時間戳,代表這個JWT生效的開始時間,意味著在這個時間之前驗證JWT是會失敗的;
- iat(Issued at):是一個時間戳,代表這個JWT的簽發(fā)時間;
- jti(JWT ID):是JWT的唯一標(biāo)識。
7、在鑒權(quán)項目工程Startup.cs文件里依賴注入JWT的服務(wù)類
public void ConfigureServices(IServiceCollection services) { services.AddScoped <IJWTService, JWTService> (); services.AddControllers(); }
8、添加AuthenticationController,生成Token,后期可以添加RefreshToken
using JWTS.Services; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; namespace JWTS.Controllers { [Route("api/[controller]")] [ApiController] public class AuthenticationController : ControllerBase { #region 構(gòu)造函數(shù) private ILogger<AuthenticationController> _logger; private IJWTService _iJWTService; private readonly IConfiguration _iConfiguration; public AuthenticationController(ILogger<AuthenticationController> logger, IConfiguration configuration , IJWTService service) { _logger = logger; _iConfiguration = configuration; _iJWTService = service; } #endregion /// <summary> /// 實際場景使用Post方法 /// http://localhost:5000/api/Authentication/Login?name=william&password=123123 /// </summary> /// <param name="name"></param> /// <param name="password"></param> /// <returns></returns> [Route("Login")] [HttpGet] public IActionResult Login(string name, string password) { //這里應(yīng)該是需要去連接數(shù)據(jù)庫做數(shù)據(jù)校驗,為了方便所有用戶名和密碼寫死了 if ("william".Equals(name) && "123123".Equals(password))//應(yīng)該數(shù)據(jù)庫 { var role = "Administrator";//可以從數(shù)據(jù)庫獲取角色 string token = this._iJWTService.GetToken(name, role); return new JsonResult(new { result = true, token }); } return Unauthorized("Not Register!!!"); } } }
2、資源中心API:使用從認證服務(wù)中心獲取的Token,去訪問資源,資源中心對用戶信息以及Token進行鑒權(quán)操作,認證失敗返回401
1、資源中心添加Nuget包(Microsoft.AspNetCore.Authentication.JwtBearer)
2、添加Authentication服務(wù),添加JwtBearer,通過Configuration獲取TokenParameter對象
using System; using System.Text; using API.Core.Models; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Tokens; namespace API.Core { public class Startup { private TokenParameter _tokenParameter; public IConfiguration Configuration { get; } public Startup(IConfiguration configuration) { Configuration = configuration; _tokenParameter = configuration.GetSection("TokenParameter").Get<TokenParameter>()??throw new ArgumentNullException(nameof(_tokenParameter)); } public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)//默認授權(quán)機制 .AddJwtBearer(options => { options.TokenValidationParameters=new TokenValidationParameters() { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = _tokenParameter.Issuer, ValidAudience = _tokenParameter.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenParameter.SecurityKey)) }; }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }
3、在資源控制器上添加[Authorize]屬性,以啟用認證授權(quán)訪問API資源
[ApiController] [Route("[controller]")] [Authorize] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILogger<WeatherForecastController> _logger; public WeatherForecastController(ILogger<WeatherForecastController> logger) { _logger = logger; } [HttpGet] public IEnumerable<WeatherForecast> Get() { var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); } }
到此這篇關(guān)于ASP.NET Core使用JWT認證授權(quán)的方法的文章就介紹到這了,更多相關(guān)ASP.NET Core JWT認證授權(quán) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
作業(yè)調(diào)度框架Quartz.net用法詳解
本文詳細講解了作業(yè)調(diào)度框架Quartz.net的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06asp.net(c#)有關(guān) Session 操作的幾個誤區(qū)
asp.net(c#)有關(guān) Session 操作的幾個誤區(qū)...2007-06-06MVC使用極驗驗證制作登錄驗證碼學(xué)習(xí)筆記7
這篇文章主要介紹了MVC使用極驗驗證制作登錄驗證碼學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Coolite Cool Study 2 同時更新多個Tab
前段時間有一個需求是這樣子的——錄入一個查詢條件, 出來的查詢結(jié)果是多張頁面。不知道有沒朋友遇到這個問題。 展現(xiàn)的效果大概是這個樣子2009-05-05ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
這篇文章主要介紹了ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等),本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-06-06.NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程
這篇文章主要給大家介紹了關(guān)于.NET連接數(shù)據(jù)庫以及基本的增刪改查操作教程的相關(guān)資料,對于剛?cè)腴T的新手們來說是個很好的入門教程,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01.NET?高性能緩沖隊列實現(xiàn)?BufferQueue的操作過程
BufferQueue 是一個用 .NET 編寫的高性能的緩沖隊列實現(xiàn),支持多線程并發(fā)操作,這篇文章主要介紹了.NET?高性能緩沖隊列實現(xiàn)?BufferQueue,需要的朋友可以參考下2024-07-07