question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing a .Net Core ClaimsIdentity with claims causes PlatformNotSupportedException

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
Misiucommented, Jan 17, 2022

in .Net 6 this is still an issue

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.PlatformNotSupportedException: This instance contains state that cannot be serialized and deserialized on this platform.
   at System.Security.Claims.ClaimsPrincipal.OnSerializingMethod(StreamingContext context)
9reactions
tb-mtgcommented, May 16, 2019

Was this resolved?

Read more comments on GitHub >

github_iconTop Results From Across the Web

PlatformNotSupportedException when serializing
PlatformNotSupportedException : This instance contains state that cannot be serialized and deserialized on this platform. at System.Security.Claims.
Read more >
Cannot Deserialize ClaimsPrincipal - serialization
Deserializing it back results in an object with empty properties. The .Claims and .Identity is lost. var identity = new ClaimsIdentity(new List< ...
Read more >
ClaimsPrincipal.GetObjectData(SerializationInfo ...
Populates the SerializationInfo with data needed to serialize the current ClaimsPrincipal object.
Read more >
最新趨勢觀測站- serialize claimsprincipal的推薦與評價
Net Framework 4.7.2 the class ClaimsPrincipal fails to serialize and deserialize when the inner ClaimsIdentity holds some claims. 於github.com. #2.
Read more >
ClaimsIdentity.GetObjectData(SerializationInfo, ...
Populates the SerializationInfo with data needed to serialize the current ClaimsIdentity object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found