Пример #1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var identity = (ClaimsIdentity)value;

            writer.WriteStartObject();

            writer.WritePropertyName("AuthenticationType");
            writer.WriteValue(identity.AuthenticationType);

            writer.WritePropertyName("NameClaimType");
            writer.WriteValue(identity.NameClaimType);

            writer.WritePropertyName("RoleClaimType");
            writer.WriteValue(identity.RoleClaimType);

            writer.WritePropertyName("Claims");

            writer.WriteStartArray();

            foreach (var c in identity.Claims)
            {
                writer.WriteStartObject();

                ClaimConverter.WriteClaim(writer, c);

                writer.WriteEndObject();
            }

            writer.WriteEndArray();

            writer.WriteEndObject();
        }
Пример #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var jo = JObject.Load(reader);

            var authType = (string)jo["AuthenticationType"];
            var nameType = (string)jo["NameClaimType"];
            var roleType = (string)jo["RoleClaimType"];

            var claimsArray = (JArray)jo["Claims"];

            var claims = new List <Claim>();

            foreach (var joClaim in claimsArray)
            {
                claims.Add(ClaimConverter.ReadClaim((JObject)joClaim));
            }

            return(new ClaimsIdentity(claims, authType, nameType, roleType));
        }