-
Notifications
You must be signed in to change notification settings - Fork 464
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
Not properly serilized object in fluent version of Decode #461
Comments
Hi, |
Hi, all data is in test method:
|
What's your version? I release a fix to the latest version - Can you please try updating and let me know? |
Hi,
simplified test file
TestTokenParser1 => failed to restore original object |
Sorry for the delay in response, I'll take a look! |
Hi, JWT.10.0.2-beta1 does not solve mention above problem, but I looked through #456
I hope, it would help you to fix this behaviour |
I have looked more closely to your issue and it seems that you are using the wrong json attribute to decorate your payload. You're using JsonProperty on the class AuthorizationTicket, that one is for Json.Net (Newtonsoft.Json), so that's why you get it working by adding your line "WithJsonSerializer(new JsonNetSerializer())" If you want to use the default System.Text.Json serializer you should use the attribute JsonPropertyName to decorate your class. One quick way to make sure you don't mix serializers is to search for any using of Newtonsoft.Json and remove them. PS. You could make your code more reliant against typos if you read the attribute name of the property. Consider this extension method: public static class TypeExtensions
{
public static string GetJsonName(this Type type, string propertyName)
{
var attributes = type.GetProperty(propertyName)?.GetCustomAttributes(inherit: false);
if (attributes == null)
{
return propertyName;
}
foreach (var attribute in attributes)
{
if (attribute is System.Text.Json.Serialization.JsonPropertyNameAttribute stjProperty)
{
return stjProperty.Name;
}
}
return propertyName;
}
} Then you can add the claims by this way: .AddClaim(typeof(AuthorizationTicket).GetJsonName(nameof(AuthorizationTicket.IsAuthenticated), ticket.IsAuthenticated) But if you have the whole object you can just encode the jwt like this: var token = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(key)
.Encode(ticket); |
Hi @hartmark, In addition, I can say, that your "TypeExtensions" would not work with Newtonsoft.Json decorator either, obviously. In my, mentioned above. case, there is one flaw: explicit setting name of Claims:
which need additional effort in decode pipeline
because names are taken from Attributes. So case is closed. |
Net7[EDITED], JWT10.0.1 [EDITED]
Despite this information https://github.com/jwt-dotnet/jwt#parsing-decoding-and-verifying-token
Two types of encoding do not produce the same result ():
Parse1 produces the default token without origin data while Parse2 does correct object.
Test method for reproducing:
The text was updated successfully, but these errors were encountered: