Newtonsoft.json: 使用声明序列化 .Net Core ClaimsIdentity 会导致 PlatformNotSupportedException

创建于 2018-05-21  ·  4评论  ·  资料来源: JamesNK/Newtonsoft.Json

我正在升级我的应用程序以使用 .Net Core 2 和 .NetStandard。 在几个方面,我正在序列化ClaimsIdentity以用于记录和传输目的。

每当我尝试序列化包含至少一个声明的ClaimsIdentity时,我都会得到一个PlatformNotSupportedException 。 我怀疑这是由于 .Net Core 2 (https://github.com/dotnet/corefx/issues/23415) 中引入的二进制序列化更改。 这似乎以前已经解决 (https://github.com/JamesNK/Newtonsoft.Json/issues/1404) 但是我在 11.0.2 中仍然收到此错误。

使用反编译器,在发生了错误OnSerializingMethod在方法ClaimsIdentity ,并且仅当所述身份具有附着1项或多个权利要求发生:

[OnSerializing]
private void OnSerializingMethod(StreamingContext context)
{
    if (this is ISerializable)
    {
        return;
    }

    _serializedNameType = _nameClaimType;
    _serializedRoleType = _roleClaimType;
    if (_instanceClaims != null && _instanceClaims.Count > 0)
    {
        throw new PlatformNotSupportedException(SR.PlatformNotSupported_Serialization); // BinaryFormatter would be needed
    }
}

源/目标类型

System.Security.Claims.ClaimsIdentity

预期行为

一个完全序列化的对象,所有声明都完好无损。

实际行为

PlatformNotSupportedException: This instance contains state that cannot be serialized and deserialized on this platform.

重现步骤

var identity = new ClaimsIdentity();
identity.AddClaim(new Claim("test", "test"));
var serializedIdentity = JsonConvert.SerializeObject(identity);

所有4条评论

这看起来像ClaimsIdentity的预期行为。 与 corefx 团队一起提出。

我们有解决这个问题吗?

这个问题解决了吗?

此问题尚未解决。
.net 核心 3.1

此页面是否有帮助?
0 / 5 - 0 等级