.NET & JWT
使用 JWT 库
JWT,a JWT(JSON Web Token) implementation for .NET
该库支持生成和解析JSON Web Token
你可以直接通过Nuget获取,也可以自己下载和编译源码.
// 不要忘了 using
using JWT;
using JWT.Algorithms;
using JWT.Builder;
// 自定义秘钥
// jwt 的生成和解析都需要使用
const string secret = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";
创建 JWT
// 使用 JwtBuilder 来生成 token
string token = new JwtBuilder()
.WithAlgorithm(new HMACSHA256Algorithm()) // 使用算法
.WithSecret(secret) // 使用秘钥
.AddClaim("exp", DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds())
.AddClaim("claim2", "claim2-value")
.Build();
Console.WriteLine(token);
生成的 token 如下:
// 注意:是通过.符号分隔成3段,分别对应的是header.payload.signature
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1Mjg3MjA2NTEsImNsYWltMiI6ImNsYWltMi12YWx1ZSJ9.56xcZALlJuwROe3qssCbe_DDjpQShk-Ik7kWAzONWFU
生成后可以分发出去, 别人拿着 token 来请求接口的时候, 我们需要解析验证
// 使用 JwtBuilder 来解析 token
try
{
string json = new JwtBuilder()
.WithSecret(secret)
.MustVerifySignature()
.Decode(token);
Console.WriteLine(json);
}
catch (TokenExpiredException)
{
Console.WriteLine("token 已过期");
}
catch (SignatureVerificationException)
{
Console.WriteLine("token 签名无效");
}
解析后得到的 json 字符串如下:
{
"exp": 1528721303,
"claim2": "claim2-value"
}
使用 Microsoft.IdentityModel.Tokens 库
- 新建一个 ASP.NET Core API 项目
- 新增一个控制器
AuthController
生成 JWT
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]/[action]")]
public class AuthController : Controller
{
private readonly IConfiguration _configuration;
public AuthController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet]
public IActionResult Token()
{
var claims = new[]
{
new Claim(type: ClaimTypes.Name, value: "username")
};
var issuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["IssuerSigningKey"]));
var creds = new SigningCredentials(issuerSigningKey, SecurityAlgorithms.HmacSha256);
//.NET Core’s JwtSecurityToken class takes on the heavy lifting and actually creates the token.
/**
* Claims (Payload)
Claims 部分包含了一些跟这个 token 有关的重要信息。 JWT 标准规定了一些字段,下面节选一些字段:
iss: The issuer of the token,token 是给谁的
sub: The subject of the token,token 主题
exp: Expiration Time。 token 过期时间,Unix 时间戳格式
iat: Issued At。 token 创建时间, Unix 时间戳格式
jti: JWT ID。针对当前 token 的唯一标识
除了规定的字段外,可以包含其他任何 JSON 兼容的字段。
* */
var token = new JwtSecurityToken(
issuer: "test.com", //
audience: "test.com",
claims: claims,
expires: DateTime.Now.AddMinutes(1),
signingCredentials: creds);
return Ok(new
{
access_token = new JwtSecurityTokenHandler().WriteToken(token: token)
});
}
}
}
生成的 JWT 可以放到 jwt.io 里验证一下.
使用 IdentityServer4 库
IdentityServerTools 是 IdentityServer4 中的一个工具类, 封装了 JWT 生成方法, 以便使用.
按照套路, 注入 IdentityServer4 服务, 这里我们仅使用工具类来生成 JWT, 所以不需要配置其他东西.
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential();
// 省略其他
}
在控制器中构造函数注入使用
using IdentityServer4;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Security.Claims;
using System.Threading.Tasks;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace WebApplication1.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class AuthController : ControllerBase
{
private readonly IdentityServerTools _identityServerTools;
public AuthController(IdentityServerTools identityServerTools)
{
_identityServerTools = identityServerTools;
}
[HttpGet]
public async Task<IActionResult> TokenAsync()
{
// 完整的场景, 应该验证用户密码
Claim[] claims = new Claim[]
{
new Claim(type: ClaimTypes.NameIdentifier, value: Guid.NewGuid().ToString("N")),
new Claim(type: ClaimTypes.Name, value: "admin"),
new Claim(type: ClaimTypes.Gender, value: "man"),
new Claim(type: ClaimTypes.Email, value: "xxx@xxx.com"),
new Claim(type: "custom", value: "value"),
};
// 可以按自己需求, 返回指定结构的数据
return Ok(value: await _identityServerTools.IssueJwtAsync(lifetime: 3600, claims: claims));
}
}
}
这个时候访问 /api/auth 应该就能看到一段老长老长的 JWT 了, 可以扔到 jwt.io 里验证下.
身份认证
上面介绍了几种不同库生成 JWT 的方法, 当别人拿着我们分发出去的 JWT 来访问我们的接口时, 需要对其进行身份认证
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
namespace WebApplication1
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false, // 是否验证 Issuer
ValidIssuer = "test.com",
ValidateAudience = false, // 是否验证 Audience
ValidAudience = "",
ValidateIssuerSigningKey = true, // 是否验证签名秘钥
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["IssuerSigningKey"])),
ValidateLifetime = true, // 是否验证失效时间
};
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseAuthentication(); // 使用身份验证
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
}
参考
- JWT.NET
- 从壹开始前后端 [vue后台] 之二 || 完美实现 JWT 滑动授权刷新
- Asp.Net Core基于JWT认证的数据接口网关Demo
- asp.net core 集成JWT
- asp.net core 集成JWT(一)
- asp.net core 集成JWT(二)token的强制失效,基于策略模式细化api权限
- ASP.NET Core Web Api之JWT(一)
- Tools - IdentityServer4
- Self-issuing an IdentityServer4 token in an IdentityServer4 service
.NET & JWT的更多相关文章
- 看图理解JWT如何用于单点登录
单点登录是我比较喜欢的一个技术解决方案,一方面他能够提高产品使用的便利性,另一方面他分离了各个应用都需要的登录服务,对性能以及工作量都有好处.自从上次研究过JWT如何应用于会话管理,加之以前的项目中也 ...
- JWT实现token-based会话管理
上文<3种web会话管理的方式>介绍了3种会话管理的方式,其中token-based的方式有必要从实现层面了解一下.本文主要介绍这方面的内容.上文提到token-based的实现目前有一个 ...
- 用JWT来保护我们的ASP.NET Core Web API
在上一篇博客中,自己动手写了一个Middleware来处理API的授权验证,现在就采用另外一种方式来处理这个授权验证的问题,毕竟现在也 有不少开源的东西可以用,今天用的是JWT. 什么是JWT呢?JW ...
- Laravel-lumen 配置JWT
具体步骤参照: [ JWT & Lumen ] 第一步 在项目根目录 执行命令 composer require tymon/jwt-auth第二步 在 bootstrap/app.php 的 ...
- .net core Jwt 添加
Jwt 已经成为跨平台身份验证通用方案,如不了解请关注:https://jwt.io/. 为了和微软其他验证模块有个比较好的衔接,项目中采用了微软开发的jwt组件: System.IdentityMo ...
- 多说评论系统API调用和本地身份说明(JWT)
多说评论系统是一个非常好用的第三方评论插件,聚合了大多数的SNS平台账号登录和分享功能,UI也很不错. 作为网站快速接入评论系统,多说是一个比较好的选择,其也提供了一些实用的API去满足定制化需求. ...
- 【JWT】JWT+HA256加密 Token验证
目录 Token验证 传统的Token验证 JWT+HA256验证 回到顶部 Token验证 最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twi ...
- 基于Token的身份验证——JWT
初次了解JWT,很基础,高手勿喷. 基于Token的身份验证用来替代传统的cookie+session身份验证方法中的session. JWT是啥? JWT就是一个字符串,经过加密处理与校验处理的字符 ...
- jwt refresh token
$app->post('auth/refresh-token', ['middleware' => 'jwt.refresh', function() { try { $old_token ...
- JWT【JSON Web Token】 简述
draft: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html http://tools.ietf.org/html/ ...
随机推荐
- Solution -「LNOI 2022」「洛谷 P8367」盒
\(\mathscr{Desription}\) Link. 有 \(n\) 个盒子排成一排,第 \(i\) 个盒子内有 \(a_i\) 个球.球可以在相邻盒子间传递,\(i\) 与 \(i+ ...
- manim边做边学--动画更新
今天介绍Manim中用于动画更新的3个类,分别是: UpdateFromFunc:根据自定义的函数来动态更新 Mobject 的属性 UpdateFromAlphaFunc:根据动画的进度来平滑地改变 ...
- GeoServer加载Arcgis切片服务
使用GeoServer中的GeoWebCache加载Arcgis切片服务 下载安装 GeoServer和GeoWebCache的下载安装都非常简单,这里选择"独立于平台的二进制版本" ...
- 数据同步工具-DataX
1.DataX 基本介绍 DataX 是阿里巴巴集团内被广泛使用的离线数据同步工具,致力于实现包括:关系型数据库(MySQL.Oracle等).HDFS.Hive.HBase.ODPS.FTP等各种异 ...
- 使用kNN算法改进约会网站配对效果(尺度归一化问题)
简单匹配:
- Object类是所有Java类的根父类
- Codeforces Round 968 (Div. 2)
题目链接:Codeforces Round 968 (Div. 2) - Codeforces 总结:C题想到了,但是写成shi了,出得有点慢. A. Turtle and Good String t ...
- dart 中在实例化 new 关键字可以省略不写
dart 中在实例化 new 关键字可以省略不写 class Person { String name; int age; String sex; Person(this.name, this.age ...
- VMware的快照原理
本文分享自天翼云开发者社区<VMware的快照原理>,作者:m****n VMware的快照是基于数据块的快照.快照也是以一个文件方式存在的,缺省位置和虚拟机在同一目录下,它是一个Delt ...
- RDK新一代模型转换可视化工具!!!
作者:SkyXZ CSDN:SkyXZ--CSDN博客 博客园:SkyXZ - 博客园 之前在使用的RDK X3的时候,吴诺老师@wunuo发布了新一代量化转换工具链使用教程,这个工具真的非常的方便, ...