.net core webapi jwt 更為清爽的認證詳解
我的方式非主流,控制卻可以更加靈活,喜歡的朋友,不妨花一點時間學習一下
jwt認證分為兩部分,第一部分是加密解密,第二部分是靈活的應用于中間件,我的處理方式是將獲取token放到api的一個具體的controller中,將發(fā)放token與驗證分離,token的失效時間,發(fā)證者,使用者等信息存放到config中。
1.配置:
在appsettings.json中增加配置
"Jwt": {
"Issuer": "issuer",//隨意定義
"Audience": "Audience",//隨意定義
"SecretKey": "abc",//隨意定義
"Lifetime": 20, //單位分鐘
"ValidateLifetime": true,//驗證過期時間
"HeadField": "useless", //頭字段
"Prefix": "prefix", //前綴
"IgnoreUrls": [ "/Auth/GetToken" ]//忽略驗證的url
}
2:定義配置類:
internal class JwtConfig
{
public string Issuer { get; set; }
public string Audience { get; set; }
/// <summary>
/// 加密key
/// </summary>
public string SecretKey { get; set; }
/// <summary>
/// 生命周期
/// </summary>
public int Lifetime { get; set; }
/// <summary>
/// 是否驗證生命周期
/// </summary>
public bool ValidateLifetime { get; set; }
/// <summary>
/// 驗證頭字段
/// </summary>
public string HeadField { get; set; }
/// <summary>
/// jwt驗證前綴
/// </summary>
public string Prefix { get; set; }
/// <summary>
/// 忽略驗證的url
/// </summary>
public List<string> IgnoreUrls { get; set; }
}
3.加密解密接口:
public interface IJwt
{
string GetToken(Dictionary<string, string> Clims);
bool ValidateToken(string Token,out Dictionary<string ,string> Clims);
}
4.加密解密的實現(xiàn)類:
install -package System.IdentityModel.Tokens.Jwt
public class Jwt : IJwt
{
private IConfiguration _configuration;
private string _base64Secret;
private JwtConfig _jwtConfig = new JwtConfig();
public Jwt(IConfiguration configration)
{
this._configuration = configration;
configration.GetSection("Jwt").Bind(_jwtConfig);
GetSecret();
}
/// <summary>
/// 獲取到加密串
/// </summary>
private void GetSecret()
{
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes("salt");
byte[] messageBytes = encoding.GetBytes(this._jwtConfig.SecretKey);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
this._base64Secret= Convert.ToBase64String(hashmessage);
}
}
/// <summary>
/// 生成Token
/// </summary>
/// <param name="Claims"></param>
/// <returns></returns>
public string GetToken(Dictionary<string, string> Claims)
{
List<Claim> claimsAll = new List<Claim>();
foreach (var item in Claims)
{
claimsAll.Add(new Claim(item.Key, item.Value));
}
var symmetricKey = Convert.FromBase64String(this._base64Secret);
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Issuer = _jwtConfig.Issuer,
Audience = _jwtConfig.Audience,
Subject = new ClaimsIdentity(claimsAll),
NotBefore = DateTime.Now,
Expires = DateTime.Now.AddMinutes(this._jwtConfig.Lifetime),
SigningCredentials =new SigningCredentials(new SymmetricSecurityKey(symmetricKey),
SecurityAlgorithms.HmacSha256Signature)
};
var securityToken = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(securityToken);
}
public bool ValidateToken(string Token, out Dictionary<string, string> Clims)
{
Clims = new Dictionary<string, string>();
ClaimsPrincipal principal = null;
if (string.IsNullOrWhiteSpace(Token))
{
return false;
}
var handler = new JwtSecurityTokenHandler();
try
{
var jwt = handler.ReadJwtToken(Token);
if (jwt == null)
{
return false;
}
var secretBytes = Convert.FromBase64String(this._base64Secret);
var validationParameters = new TokenValidationParameters
{
RequireExpirationTime = true,
IssuerSigningKey = new SymmetricSecurityKey(secretBytes),
ClockSkew = TimeSpan.Zero,
ValidateIssuer = true,//是否驗證Issuer
ValidateAudience = true,//是否驗證Audience
ValidateLifetime = this._jwtConfig.ValidateLifetime,//是否驗證失效時間
ValidateIssuerSigningKey = true,//是否驗證SecurityKey
ValidAudience = this._jwtConfig.Audience,
ValidIssuer = this._jwtConfig.Issuer
};
SecurityToken securityToken;
principal = handler.ValidateToken(Token, validationParameters, out securityToken);
foreach (var item in principal.Claims)
{
Clims.Add(item.Type, item.Value);
}
return true;
}
catch (Exception ex)
{
return false;
}
}
}
5.定義獲取Token的Controller:
在Startup.ConfigureServices中注入 IJwt
services.AddTransient<IJwt, Jwt>(); // Jwt注入
[Route("[controller]/[action]")]
[ApiController]
public class AuthController : ControllerBase
{
private IJwt _jwt;
public AuthController(IJwt jwt)
{
this._jwt = jwt;
}
/// <summary>
/// getToken
/// </summary>
/// <returns></returns>
[HttpPost]
public IActionResult GetToken()
{
if (true)
{
Dictionary<string, string> clims = new Dictionary<string, string>();
clims.Add("userName", userName);
return new JsonResult(this._jwt.GetToken(clims));
}
}
}
6.創(chuàng)建中間件:
public class UseJwtMiddleware
{
private readonly RequestDelegate _next;
private JwtConfig _jwtConfig =new JwtConfig();
private IJwt _jwt;
public UseJwtMiddleware(RequestDelegate next, IConfiguration configration,IJwt jwt)
{
_next = next;
this._jwt = jwt;
configration.GetSection("Jwt").Bind(_jwtConfig);
}
public Task InvokeAsync(HttpContext context)
{
if (_jwtConfig.IgnoreUrls.Contains(context.Request.Path))
{
return this._next(context);
}
else
{
if (context.Request.Headers.TryGetValue(this._jwtConfig.HeadField, out Microsoft.Extensions.Primitives.StringValues authValue))
{
var authstr = authValue.ToString();
if (this._jwtConfig.Prefix.Length > 0)
{
authstr = authValue.ToString().Substring(this._jwtConfig.Prefix.Length+1, authValue.ToString().Length -(this._jwtConfig.Prefix.Length+1));
}
if (this._jwt.ValidateToken(authstr, out Dictionary<string, string> Clims))
{
foreach (var item in Clims)
{
context.Items.Add(item.Key, item.Value);
}
return this._next(context);
}
else
{
context.Response.StatusCode = 401;
context.Response.ContentType = "application/json";
return context.Response.WriteAsync("{\"status\":401,\"statusMsg\":\"auth vaild fail\"}");
}
}
else
{
context.Response.StatusCode = 401;
context.Response.ContentType = "application/json";
return context.Response.WriteAsync("{\"status\":401,\"statusMsg\":\"auth vaild fail\"}");
}
}
}
}
7.中間件暴露出去
public static class UseUseJwtMiddlewareExtensions
{
/// <summary>
/// 權限檢查
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IApplicationBuilder UseJwt(this IApplicationBuilder builder)
{
return builder.UseMiddleware<UseJwtMiddleware>();
}
}
8.在Startup.Configure中使用中間件:
app.UseJwt();
以1的配置為例:
除了請求 /auth/getToken 不需要加頭信息外,其他的請求一律要求頭信息中必須帶著
userless:prefix (從Auth/GetToken中獲取到的token)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- .NET?Core支持Cookie和JWT混合認證、授權的方法
- .net?core?api接口JWT方式認證Token
- ASP.NET?Core應用JWT進行用戶認證及Token的刷新方案
- asp.net core3.1cookie和jwt混合認證授權實現(xiàn)多種身份驗證方案
- AntDesign Pro + .NET Core 實現(xiàn)基于JWT的登錄認證功能
- .Net Core官方JWT授權驗證的全過程
- 詳解ASP.NET Core Web Api之JWT刷新Token
- ASP.NET Core使用JWT認證授權的方法
- ASP.NET Core學習之使用JWT認證授權詳解
- 淺談ASP.NET Core 中jwt授權認證的流程原理
- ASP.Net Core3.0中使用JWT認證的實現(xiàn)
- .NET core 3.0如何使用Jwt保護api詳解
- asp.net core集成JWT的步驟記錄
- Asp.Net Core基于JWT認證的數(shù)據(jù)接口網(wǎng)關實例代碼
- .Net Core實現(xiàn)JWT授權認證
相關文章
ASP.NET Core3.1 Ocelot負載均衡的實現(xiàn)
這篇文章主要介紹了ASP.NET Core3.1 Ocelot負載均衡的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
.Net插件框架Managed Extensibility Framework簡介
這篇文章介紹了.Net插件框架Managed Extensibility Framework,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
asp.net GridView模板列中實現(xiàn)選擇行功能
近來在項目中用到了GridView控件,用它實現(xiàn)添加、修改、刪除、選擇、顯示復雜表頭等功能2010-07-07

