Files
2024-02-01 19:10:40 +01:00

80 lines
2.6 KiB
C#

using Microsoft.AspNetCore.Identity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Security.Claims;
namespace MagMan.Data.Admin
{
/// <summary>
/// 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
/// </summary>
public class UserData
{
#region Public Properties
[JsonProperty(ItemConverterType = typeof(ClaimConverter))]
public List<Claim> Claims { get; set; } = new List<Claim>();
public IdentityUser Identity { get; set; } = new IdentityUser();
public List<string> Roles { get; set; } = new List<string>();
#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();
}
}
/// <summary>
/// Classe x gestione dei claim come record editabile
/// </summary>
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;
}
}
}