eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiTXJCdWciLCJleHAiOjE1MTI5NTkzMDMuMCwianRpIjoibHVvemhpcGVuZyJ9.9iwGMHmS0mophyFgliLK15hs_eE770IchaZ-bWcX5c0
{
"alg": "HS256",
"typ": "JWT"
}
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
{
"name": "MrBug",
"exp": 1512959303,
"jti": "luozhipeng"
}
eyJuYW1lIjoiTXJCdWciLCJleHAiOjE1MTI5NTkzMDMuMCwianRpIjoibHVvemhpcGVuZyJ9
<span style="color: #008000">//</span><span style="color: #008000"> javascript</span> <span style="color: #0000ff">var</span> encodedString = base64UrlEncode(header) + '.' +<span style="color: #000000"> base64UrlEncode(payload); </span><span style="color: #0000ff">var</span> signature = HMACSHA256(encodedString, 'secret'); <span style="color: #008000">//</span><span style="color: #008000"> 9iwGMHmS0mophyFgliLK15hs_eE770IchaZ-bWcX5c0</span>
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiTXJCdWciLCJleHAiOjE1MTI5NTkzMDMuMCwianRpIjoibHVvemhpcGVuZyJ9.9iwGMHmS0mophyFgliLK15hs_eE770IchaZ-bWcX5c0
fetch('api/user/1', {
headers: {
'Authorization': 'Bearer ' + token
}
})
Install-Package JWT -Version 3.0.0
IDateTimeProvider provider = new UtcDateTimeProvider();
var now = provider.GetNow();
var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); // or use JwtValidator.UnixEpoch
var secondsSinceEpoch = Math.Round((now - unixEpoch).TotalSeconds);
var payload = new Dictionary<string, object>
{
{ "name", "MrBug" },
{"exp",secondsSinceEpoch+100 },
{"jti","luozhipeng" }
};
Console.WriteLine(secondsSinceEpoch);
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
IJsonSerializer serializer = new JsonNetSerializer();
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
var token = encoder.Encode(payload, secret);
Console.WriteLine(token);
try
{
IJsonSerializer serializer = new JsonNetSerializer();
IDateTimeProvider provider = new UtcDateTimeProvider();
IJwtValidator validator = new JwtValidator(serializer, provider);
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder);
var json = decoder.Decode(token, secret, verify: true);//token为之前生成的字符串
Console.WriteLine(json);
}
catch (TokenExpiredException)
{
Console.WriteLine("Token has expired");
}
catch (SignatureVerificationException)
{
Console.WriteLine("Token has invalid signature");
}
public class CustomJsonSerializer : IJsonSerializer
{
public string Serialize(object obj)
{
// Implement using favorite JSON Serializer
}
public T Deserialize<T>(string json)
{
// Implement using favorite JSON Serializer
}
}
IJwtAlgorithm algorithm = new HMACSHA256Algorithm(); IJsonSerializer serializer = new CustomJsonSerializer(); IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder(); IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
JsonSerializer customJsonSerializer = new JsonSerializer
{
// All json keys start with lowercase characters instead of the exact casing of the model/property. e.g. fullName
ContractResolver = new CamelCasePropertyNamesContractResolver(),
// Nice and easy to read, but you can also do Formatting.None to reduce the payload size (by hardly anything...)
Formatting = Formatting.Indented,
// The best date/time format/standard.
DateFormatHandling = DateFormatHandling.IsoDateFormat,
// Don't add key/values when the value is null.
NullValueHandling = NullValueHandling.Ignore,
// Use the enum string-value, not the implicit int value, e.g. "oolor" : "red"
Converters.Add(new StringEnumConverter())
};
IJsonSerializer serializer = new JsonNetSerializer(customJsonSerializer);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有