Serializing a .Net Core ClaimsIdentity with claims causes PlatformNotSupportedException
See original GitHub issueI am in the process of upgrading my application to use .Net Core 2 and .NetStandard. In several areas I am serializing a ClaimsIdentity
for logging and transmission purposes.
Whenever I try to serialize a ClaimsIdentity
that contains at least one claim, I get a PlatformNotSupportedException
. I suspect this is due to the Binary Serialization changes introduced in .Net Core 2 (https://github.com/dotnet/corefx/issues/23415). This appears to have been addressed previously (https://github.com/JamesNK/Newtonsoft.Json/issues/1404) however I am still getting this error in 11.0.2.
Using a decompiler, the error occurs in the OnSerializingMethod
method in ClaimsIdentity
, and only occurs when the identity has 1 or more claims attached:
[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
}
}
Source/destination types
System.Security.Claims.ClaimsIdentity
Expected behavior
A fully serialized object with all claims intact.
Actual behavior
PlatformNotSupportedException: This instance contains state that cannot be serialized and deserialized on this platform.
Steps to reproduce
var identity = new ClaimsIdentity();
identity.AddClaim(new Claim("test", "test"));
var serializedIdentity = JsonConvert.SerializeObject(identity);
Issue Analytics
- State:
- Created 5 years ago
- Reactions:10
- Comments:9 (1 by maintainers)
in .Net 6 this is still an issue
Was this resolved?