Files
webdoorcreator/WebDoorCreator.Data/User/ClaimConverter.cs
T
2023-05-30 09:37:32 +02:00

45 lines
1.4 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 jo = JObject.Load(reader);
string type = (string)jo["Type"];
string value = (string)jo["Value"];
string valueType = (string)jo["ValueType"];
string issuer = (string)jo["Issuer"];
string originalIssuer = (string)jo["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)
{
throw new NotImplementedException();
}
}
}