TokenValidator 1.1.2
dotnet add package TokenValidator --version 1.1.2
NuGet\Install-Package TokenValidator -Version 1.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TokenValidator" Version="1.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TokenValidator" Version="1.1.2" />
<PackageReference Include="TokenValidator" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TokenValidator --version 1.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TokenValidator, 1.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TokenValidator@1.1.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TokenValidator&version=1.1.2
#tool nuget:?package=TokenValidator&version=1.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Token校验与解析
概述
TokenValidator用于ASP .NET Core Web API项目开发,包含以下功能:
- 从Http请求头中读取、解析、校验安全令牌;
- 检测安全令牌的用户权限,以支持用户鉴权;
使用说明
引用TokenValidator.dll
初始化相关配置
在使用该dll相关功能之前,请先通过ConfigInit类初始化相关配置
ConfigInit configInit = new ConfigInit(discoveryUrl,authParamName,isBlackListEnable,isAuthValidatorEnable)
/// <summary>
/// 初始化配置参数
/// </summary>
/// <param name="discoveryUrl">OIDC认证地址</param>
/// <param name="authParamName">Http请求头中token字段名,默认为Authorization</param>
/// <param name="isBlackListEnable">是否启用token黑名单,默认不启用</param>
/// <param name="isAuthValidatorEnable">是否开启token实时校验,默认不启用</param>
public ConfigInit(string discoveryUrl, string authParamName = "Authorization", bool isBlackListEnable = false, bool isAuthValidatorEnable = false)
{
AuthParamName = authParamName;
IsBlackListEnable = isBlackListEnable;
IsAuthValidatorEnable = isAuthValidatorEnable;
DiscoveryUrl = discoveryUrl ?? throw new ArgumentNullException(nameof(discoveryUrl), "ConfigInit Error");
JwksUrl = Regex.Replace(discoveryUrl, "/$", "") + CommonCst.KEYS_SUFFIX_V1;
}
使用相关API接口
提供以下接口:
从Http请求头中解析校验token
//实现方法
/// <summary>
/// 从 HTTP 请求头中读取、验证、解析安全令牌
/// </summary>
/// <param name="httpRequest">请求</param>
/// <param name="httpResponse">响应</param>
/// <returns>token验证结果</returns>
public bool PreHandle(HttpRequestMessage httpRequest, HttpResponseMessage httpResponse)
//调用示例
HttpFilterService filterService = new HttpFilterService();
bool result = filterService.PreHandle(request, response);
校验通过后,可在该请求的处理流程中随时调用Token解析与校验的API,该dll保证并发请求中的线程安全问题
token解析与校验
//获取token中payload部分
/// <summary>
/// 解析token
/// </summary>
/// <param name="jwtString">待解析的token</param>
/// <returns>json格式的jwt中的payload</returns>
public static string ParseJwtPayload(string jwtString)
//获取token中Header部分
public static string ParseJwtHeader(string jwtString)
//获取token实例
/// <summary>
// 获取token内容
/// </summary>
/// <returns>OpenIdConnectToken实体类</returns>
public AuthTokenEntity GetTokenEntity()
//使用示例
JwtUtils.ParseJwtPayload(token);
JwtUtils.ParseJwtHeader(token);
AuthTokenService authTokenService = new AuthTokenService();
authTokenService.GetTokenEntity();
其中token实例包含的属性如下:
public class AuthTokenEntity
{
[JsonProperty("userId")]
public string UserId { get; set; }
[JsonProperty("clientId")]
public string ClientId { get; set; }
[JsonProperty("userPhone")]
public string UserPhone { get; set; }
[JsonProperty("userEmail")]
public string UserEmail { get; set; }
[JsonProperty("userName")]
public string UserName { get; set; }
[JsonProperty("userDirectory")]
public string UserDirectory { get; set; }
[JsonProperty("tenantId")]
public string TenantId { get; set; }
[JsonProperty("tenantName")]
public string TenantName { get; set; }
[JsonProperty("appId")]
public string AppId { get; set; }
[JsonProperty("nickName")]
public string NickName { get; set; }
[JsonProperty("userHead")]
public string UserHead { get; set; }
[JsonProperty("userLoginId")]
public string UserLoginId { get; set; }
[JsonProperty("roles")]
public List<string> Roles { get; set; }
[JsonProperty("permissions")]
public List<string> Permissions { get; set; }
[JsonProperty("nonce")]
public string Nonce { get; set; }
[JsonProperty("extendedField")]
public string ExtendedField { get; set; }
[JsonProperty("jwtString")]
public string JwtString { get; set; }
[JsonProperty("accountType")]
public string AccountType { get; set; }
[JsonProperty("aud")]
public string Aud { get; set; }
[JsonProperty("tp")]
public string Tp { get; set; }
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Newtonsoft.Json (>= 13.0.3)
- Portable.BouncyCastle (>= 1.9.0)
- System.Configuration.ConfigurationManager (>= 8.0.0)
- System.IdentityModel.Tokens.Jwt (>= 7.4.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on TokenValidator:
| Package | Downloads |
|---|---|
|
ApiValidateService
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.