ASP.NET Core使用JWT認(rèn)證授權(quán)的方法
demo地址: https://github.com/william0705/JWTS
名詞解析
認(rèn)證 : 識(shí)別用戶是否合法
授權(quán): 賦予用戶權(quán)限 (能訪問(wèn)哪些資源)
鑒權(quán): 鑒定權(quán)限是否合法
Jwt優(yōu)勢(shì)與劣勢(shì)
優(yōu)勢(shì)
1、 無(wú)狀態(tài)
token 存儲(chǔ)身份驗(yàn)證所有信息 , 服務(wù)端不需要保存用戶身份驗(yàn)證信息, 減少服務(wù)端壓力 , 服務(wù)端更容易水平擴(kuò)展, 由于無(wú)狀態(tài), 又會(huì)導(dǎo)致它最大缺點(diǎn) , 很難注銷
2、 支持跨域訪問(wèn)
Cookie是不允許垮域訪問(wèn)的,token支持
3、 跨語(yǔ)言
基于標(biāo)準(zhǔn)化的 JSON Web Token (JWT) , 不依賴特定某一個(gè)語(yǔ)言 , 例如生成的Token可以對(duì)多種語(yǔ)言使用(Net , Java , PHP …)
劣勢(shì)
1、Token有效性問(wèn)題
后臺(tái)很難注銷已經(jīng)發(fā)布的Token , 通常需要借助第三方儲(chǔ)存(數(shù)據(jù)庫(kù)/緩存) 實(shí)現(xiàn)注銷, 這樣就會(huì)失去JWT最大的優(yōu)勢(shì)
2、占帶寬
Token長(zhǎng)度(取決存放內(nèi)容) 比session_id大 , 每次請(qǐng)求多消耗帶寬 , token只存必要信息 , 避免token過(guò)長(zhǎng)
3、需要實(shí)現(xiàn)續(xù)簽
cookies – session 通常是框架已經(jīng)實(shí)現(xiàn)續(xù)簽功能, 每次訪問(wèn)把過(guò)期時(shí)間更新, JWT需要自己實(shí)現(xiàn), 參考OAuth2刷新Token機(jī)制實(shí)現(xiàn)刷新Token
4、消耗更多CPU
每次請(qǐng)求需要對(duì)內(nèi)容解密和驗(yàn)證簽名這兩步操作,典型用時(shí)間換空間
只能根據(jù)自身使用場(chǎng)景決定使用哪一種身份驗(yàn)證方案 , 沒(méi)有一種方案是通用的,完美的
.NET Core集成JWT認(rèn)證授權(quán)服務(wù)
1、認(rèn)證服務(wù)API:認(rèn)證用戶,并發(fā)布Token
1、引入nuget包,System.IdentityModel.Tokens.Jwt
2、創(chuàng)建生成Token的服務(wù),建議使用面向接口和實(shí)現(xiàn)編程,方便服務(wù)注入容器ServicesCollection(涉及DI和IOC概念)
3、創(chuàng)建接口
namespace JWTS.Services { public interface IJWTService { /// <summary> /// 根據(jù)驗(yàn)證通過(guò)后的用戶以及角色生成Token,以達(dá)到角色控制的作用 /// </summary> /// <param name="userName"></param> /// <param name="role"></param> /// <returns></returns> string GetToken(string userName,string role); } }
4、在appsettings.config中添加生成token需要的信息,并映射成對(duì)象
"TokenParameter": { "Issuer": "William", //這個(gè)JWT的簽發(fā)主體(發(fā)行者) "Audience": "William", //這個(gè)JWT的接收對(duì)象 "SecurityKey": "askalsnlkndhasnaslkasmadka" } public class TokenParameter { public string Issuer { get; set; } public string Audience { get; set; } public string SecurityKey { get; set; } }
5、實(shí)現(xiàn)接口,注入Configuration,獲取TokenParameter對(duì)象
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 部分包含了一些跟這個(gè) token 有關(guān)的重要信息。 JWT 標(biāo)準(zhǔn)規(guī)定了一些字段,下面節(jié)選一些字段: JWT會(huì)被加密,但是這部分內(nèi)容任何人都可以讀取,所以不要存放機(jī)密信息 iss: The issuer of the token,token 是給誰(shuí)的 sub: The subject of the token,token 主題 exp: Expiration Time。 token 過(guò)期時(shí)間,Unix 時(shí)間戳格式 iat: Issued At。 token 創(chuàng)建時(shí)間, Unix 時(shí)間戳格式 jti: JWT ID。針對(duì)當(dāng)前 token 的唯一標(biāo)識(shí) 除了規(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)準(zhǔn)里面定好的claim有:
- iss(Issuser):代表這個(gè)JWT的簽發(fā)主體;
- sub(Subject):代表這個(gè)JWT的主體,即它的所有人;
- aud(Audience):代表這個(gè)JWT的接收對(duì)象;
- exp(Expiration time):是一個(gè)時(shí)間戳,代表這個(gè)JWT的過(guò)期時(shí)間;
- nbf(Not Before):是一個(gè)時(shí)間戳,代表這個(gè)JWT生效的開(kāi)始時(shí)間,意味著在這個(gè)時(shí)間之前驗(yàn)證JWT是會(huì)失敗的;
- iat(Issued at):是一個(gè)時(shí)間戳,代表這個(gè)JWT的簽發(fā)時(shí)間;
- jti(JWT ID):是JWT的唯一標(biāo)識(shí)。
7、在鑒權(quán)項(xiàng)目工程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> /// 實(shí)際場(chǎng)景使用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ù)庫(kù)做數(shù)據(jù)校驗(yàn),為了方便所有用戶名和密碼寫(xiě)死了 if ("william".Equals(name) && "123123".Equals(password))//應(yīng)該數(shù)據(jù)庫(kù) { var role = "Administrator";//可以從數(shù)據(jù)庫(kù)獲取角色 string token = this._iJWTService.GetToken(name, role); return new JsonResult(new { result = true, token }); } return Unauthorized("Not Register!!!"); } } }
2、資源中心API:使用從認(rèn)證服務(wù)中心獲取的Token,去訪問(wèn)資源,資源中心對(duì)用戶信息以及Token進(jìn)行鑒權(quán)操作,認(rèn)證失敗返回401
1、資源中心添加Nuget包(Microsoft.AspNetCore.Authentication.JwtBearer)
2、添加Authentication服務(wù),添加JwtBearer,通過(guò)Configuration獲取TokenParameter對(duì)象
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)//默認(rèn)授權(quán)機(jī)制 .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]屬性,以啟用認(rèn)證授權(quán)訪問(wè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認(rèn)證授權(quán)的方法的文章就介紹到這了,更多相關(guān)ASP.NET Core JWT認(rèn)證授權(quán) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
作業(yè)調(diào)度框架Quartz.net用法詳解
本文詳細(xì)講解了作業(yè)調(diào)度框架Quartz.net的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06asp.net(c#)有關(guān) Session 操作的幾個(gè)誤區(qū)
asp.net(c#)有關(guān) Session 操作的幾個(gè)誤區(qū)...2007-06-06MVC使用極驗(yàn)驗(yàn)證制作登錄驗(yàn)證碼學(xué)習(xí)筆記7
這篇文章主要介紹了MVC使用極驗(yàn)驗(yàn)證制作登錄驗(yàn)證碼學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Coolite Cool Study 2 同時(shí)更新多個(gè)Tab
前段時(shí)間有一個(gè)需求是這樣子的——錄入一個(gè)查詢條件, 出來(lái)的查詢結(jié)果是多張頁(yè)面。不知道有沒(méi)朋友遇到這個(gè)問(wèn)題。 展現(xiàn)的效果大概是這個(gè)樣子2009-05-05ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
這篇文章主要介紹了ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06.NET連接數(shù)據(jù)庫(kù)以及基本的增刪改查操作教程
這篇文章主要給大家介紹了關(guān)于.NET連接數(shù)據(jù)庫(kù)以及基本的增刪改查操作教程的相關(guān)資料,對(duì)于剛?cè)腴T(mén)的新手們來(lái)說(shuō)是個(gè)很好的入門(mén)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01Asp.net 連接MySQL的實(shí)現(xiàn)代碼[]
ASP.NET連接MySQL需要一個(gè)組件(.net本身不提供訪問(wèn)MySQL的驅(qū)動(dòng))MySQL.Data.Dll,此為官方提供(純C#開(kāi)發(fā),開(kāi)源噢),有多個(gè)版本選擇,采用的數(shù)據(jù)訪問(wèn)模式為ADO.NET,跟asp.net訪問(wèn)sqlserver很像,非常簡(jiǎn)單。2009-08-08.NET?高性能緩沖隊(duì)列實(shí)現(xiàn)?BufferQueue的操作過(guò)程
BufferQueue 是一個(gè)用 .NET 編寫(xiě)的高性能的緩沖隊(duì)列實(shí)現(xiàn),支持多線程并發(fā)操作,這篇文章主要介紹了.NET?高性能緩沖隊(duì)列實(shí)現(xiàn)?BufferQueue,需要的朋友可以參考下2024-07-07