我正在尝试从 Dictionary(JSON deserialized) 中获取值并解析它。
当我快速查看变量时,我发现没有“M”作为输出参数的一部分,如下所示
但是当我点击该值时,我发现“M”被添加到给定的值中
这里的问题是当有“M”时 long.Parse 失败。如果我手动删除“M” long.Parse 工作正常。
为什么会有这种奇怪的行为?以及如何避免这种情况?
编辑:
payloadJson 中的值为
{
"http://schemas.microsoft.com/ws/2008/06/identity/claims/userdata": "admin@admin.com",
"expiry": 1551354842.0,
"issuedAt": 1551351242.0,
"notBefore": 1566989642.0,
"isRefresh": false
}
var payloadData = jsonSerializer.Deserialize<Dictionary<string, object>>(payloadJson);
object exp;
if (payloadData != null && (checkExpiration && payloadData.TryGetValue("expiry", out exp)))
{
var validTo = FromUnixTime(long.Parse(exp.ToString()));
}
控制台应用
using JWT;
using JWT.Serializers;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace TestApp
{
class Program
{
static void Main(string[] args)
{
var token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3VzZXJkYXRhIjoiYWRtaW5AYWRtaW4uY29tIiwiZXhwaXJ5IjoxNTUxMzU0ODQyLjAsImlzc3VlZEF0IjoxNTUxMzUxMjQyLjAsIm5vdEJlZm9yZSI6MTU2Njk4OTY0Mi4wLCJpc1JlZnJlc2giOmZhbHNlfQ.E-fR8VAFAy-mosEfQC3ZPlN2kZBQg02FLYuChdhqHNhzgVsbIjMXUFLHYowf0aUwQRcyoFR2mpiD_5I6drGdnQ";
var jsonSerializer = new JavaScriptSerializer();
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 payloadJson = decoder.Decode(token, "GAFaS9W4Ys2AA2VHadPdje9gaeWjY8", true);
var payloadData = jsonSerializer.Deserialize<Dictionary<string, object>>(payloadJson);
object exp;
payloadData.TryGetValue("expiry", out exp);
var tempExpiry = long.Parse(exp.ToString());
}
}
}