using Microsoft.AspNetCore.Identity; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Security.Claims; namespace MagMan.Data.Admin { /// /// Classe generalizzaizone identity (user + roles + claims) x gestione semplificata in editing /// /// per deserializzare i claims vedere questi link: /// - https://stackoverflow.com/questions/63634652/c-sharp-newtonsoft-deserialize-custom-object-with-claims /// - https://stackoverflow.com/questions/28155169/how-to-programmatically-choose-a-constructor-during-deserialization/28155770#28155770 /// public class UserData { #region Public Properties [JsonProperty(ItemConverterType = typeof(ClaimConverter))] public List Claims { get; set; } = new List(); public IdentityUser Identity { get; set; } = new IdentityUser(); public List Roles { get; set; } = new List(); #endregion Public Properties } 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(); } } /// /// Classe x gestione dei claim come record editabile /// public class ClaimRec { public int Id { get; set; } = 0; public string Type { get; set; } = ""; public string Value { get; set; } = ""; public ClaimRec(Claim currItem, int nId) { this.Id = nId; this.Type = currItem.Type; this.Value = currItem.Value; } public ClaimRec(string newType, string newValue, int nId) { this.Id = nId; this.Type = newType; this.Value = newValue; } } }