Closed althunibat closed 3 years ago
Hi, there is not way to serialize a custom object for the moment. You can this achieve by using a JwtObject
and a JwtArray
:
var descriptor = new JwsDescriptor();
descriptor.AddClaim("https//hasura.io/jwt/claims", new JwtObject
{
{ "user_id", 123 },
{ "default_role", "user" },
{ "roles", new JwtArray { "user"} }
});
This is "by design" for avoiding two issues:
1/ Casing issue : how should be cased the custom property names? With the JwtObject
, you specify the casing.
2/ Type mismatch : custom objects contains custom value types and there is always debates on its representation in JSON. The JwtObject
only support native JSON types (String, Number, Boolean & Null) and the DateTime because there is convention for the mapping (epoch time).
For convenience it should be interessant to allow
{ "roles", new[] { "user"} }
for hiding the JwtArray
in case of string array.
Fixed in version 2.0.0-beta.1
If required, the JSON serialization behavior can be controled with the JsonSerializationBehavior.SerializerOptions
property, for example by adding a JsonConverter<HasuraClaim>
to JsonSerializationBehavior.SerializerOptions.Converters
i couldn't find a way to add custom claims to JwsDescriptor which accepts claim value as object, where it get automatically json serialized when jwt is written.
e.g.
descriptor.AddClaim("https://hasura.io/jwt/claims", new HasuraClaim { UserId = user.Id, DefaultRole = "user", Roles = new[] { "user" } });