Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializing a .Net Core ClaimsIdentity with claims causes PlatformNotSupportedException #1713

Closed
nd-srogers opened this issue May 21, 2018 · 10 comments

Comments

@nd-srogers
Copy link

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 (#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);
@JamesNK
Copy link
Owner

JamesNK commented May 27, 2018

That looks like the expected behavior of ClaimsIdentity. Raise it with the corefx team.

@JamesNK JamesNK closed this as completed May 27, 2018
@vikpai
Copy link

vikpai commented Mar 25, 2019

did we get a resolution for this?

@tb-mtg
Copy link

tb-mtg commented May 16, 2019

Was this resolved?

@EngRajabi
Copy link

EngRajabi commented Jun 6, 2020

This issue has not been resolved.
.net core 3.1

@ronaldpschutte
Copy link

in .Net 5 this still is a problem

@alkanyunus
Copy link

Still a problem

@Misiu
Copy link

Misiu commented 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)

@Sergey-PrudentDev
Copy link

Sergey-PrudentDev commented Jun 2, 2023

Annoyingly only solution seems to be to create DTOs

@senglory
Copy link

Experiencing the same issue (net 7.0). What's the fix?

@andreyshiryaev
Copy link

andreyshiryaev commented Nov 19, 2023

@senglory @Misiu

I had the same problem.
And I found that sometimes google does not return family_name and when i removed the code userClaims.FindFirstValue(ClaimTypes.Surname).?Value my application starts works fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests