6
6
using System ;
7
7
using System . Linq ;
8
8
using System . Reflection ;
9
+ using System . Security . Cryptography . X509Certificates ;
9
10
10
11
namespace Aguacongas . AspNetCore . Authentication
11
12
{
13
+ /// <summary>
14
+ /// Converter For x509 Data
15
+ /// </summary>
16
+ public class X509Certificate2JsonConverter : JsonConverter
17
+ {
18
+ /// <summary>
19
+ /// Is the object fed to this a match for our converter type.
20
+ /// </summary>
21
+ /// <param name="objectType"></param>
22
+ /// <returns></returns>
23
+ public override bool CanConvert ( Type objectType )
24
+ {
25
+ return objectType == typeof ( X509Certificate2 ) ;
26
+ }
27
+
28
+ /// <summary>
29
+ /// Read method.
30
+ /// </summary>
31
+ /// <param name="reader"></param>
32
+ /// <param name="objectType"></param>
33
+ /// <param name="existingValue"></param>
34
+ /// <param name="serializer"></param>
35
+ /// <returns></returns>
36
+ public override object ReadJson ( JsonReader reader ,
37
+ Type objectType , object existingValue , JsonSerializer serializer )
38
+ {
39
+ var deserializedRaw = serializer . Deserialize < byte [ ] > ( reader ) ;
40
+ var deserialized = new X509Certificate2 ( deserializedRaw ) ;
41
+ return deserialized ;
42
+ }
43
+
44
+ /// <summary>
45
+ /// Write method.
46
+ /// </summary>
47
+ /// <param name="writer"></param>
48
+ /// <param name="value"></param>
49
+ /// <param name="serializer"></param>
50
+ public override void WriteJson ( JsonWriter writer ,
51
+ object value , JsonSerializer serializer )
52
+ {
53
+ byte [ ] certData = ( ( X509Certificate2 ) value ) . Export ( X509ContentType . Pfx ) ;
54
+ serializer . Serialize ( writer , certData ) ;
55
+ }
56
+ }
57
+
12
58
/// <summary>
13
59
/// Manage <see cref="AuthenticationSchemeOptions"/> serialization.
14
60
/// </summary>
@@ -27,7 +73,11 @@ public class AuthenticationSchemeOptionsSerializer : IAuthenticationSchemeOption
27
73
ReferenceLoopHandling = ReferenceLoopHandling . Ignore ,
28
74
Formatting = Formatting . None ,
29
75
DefaultValueHandling = DefaultValueHandling . Include ,
30
- ContractResolver = new ContractResolver ( )
76
+ ContractResolver = new ContractResolver ( ) ,
77
+ Converters =
78
+ {
79
+ new X509Certificate2JsonConverter ( )
80
+ }
31
81
} ;
32
82
33
83
/// <summary>
0 commit comments