Repository navigation
Expand file tree
/
Copy pathPropertiesSerializer.cs
More file actions
72 lines (65 loc) · 2.47 KB
/
Copy pathPropertiesSerializer.cs
File metadata and controls
72 lines (65 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.AspNet.Http.Authentication;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Authentication.DataHandler.Serializer
{
public class PropertiesSerializer : IDataSerializer<AuthenticationProperties>
{
private const int FormatVersion = 1;
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Dispose is idempotent")]
public byte[] Serialize(AuthenticationProperties model)
{
using (var memory = new MemoryStream())
{
using (var writer = new BinaryWriter(memory))
{
Write(writer, model);
writer.Flush();
return memory.ToArray();
}
}
}
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Dispose is idempotent")]
public AuthenticationProperties Deserialize(byte[] data)
{
using (var memory = new MemoryStream(data))
{
using (var reader = new BinaryReader(memory))
{
return Read(reader);
}
}
}
public static void Write([NotNull] BinaryWriter writer, [NotNull] AuthenticationProperties properties)
{
writer.Write(FormatVersion);
writer.Write(properties.Dictionary.Count);
foreach (var kv in properties.Dictionary)
{
writer.Write(kv.Key);
writer.Write(kv.Value);
}
}
public static AuthenticationProperties Read([NotNull] BinaryReader reader)
{
if (reader.ReadInt32() != FormatVersion)
{
return null;
}
int count = reader.ReadInt32();
var extra = new Dictionary<string, string>(count);
for (int index = 0; index != count; ++index)
{
string key = reader.ReadString();
string value = reader.ReadString();
extra.Add(key, value);
}
return new AuthenticationProperties(extra);
}
}
}