Files
2023-08-01 09:54:04 +02:00

47 lines
1.5 KiB
C#

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
namespace WebDoorCreator.Data.User
{
/// <summary>
/// Fix Serializzazione Claims x Newtonsoft.Json
/// https://stackoverflow.com/questions/28155169/how-to-programmatically-choose-a-constructor-during-deserialization/54645939#54645939
/// </summary>
public class ClaimConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(System.Security.Claims.Claim));
}
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jObj = JObject.Load(reader);
string type = $"{jObj["Type"]}";
string value = $"{jObj["Value"]}";
string valueType = $"{jObj["ValueType"]}";
string issuer = $"{jObj["Issuer"]}";
string originalIssuer = $"{jObj["OriginalIssuer"]}";
return new Claim(type, value, valueType, issuer, originalIssuer);
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
// lascio eccezione perché è un deserializzatore "in sola lettura"
throw new NotImplementedException();
}
}
}