Repository navigation
Expand file tree
/
Copy pathSecureDataFormat.cs
More file actions
65 lines (56 loc) · 2.16 KB
/
Copy pathSecureDataFormat.cs
File metadata and controls
65 lines (56 loc) · 2.16 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNet.DataProtection;
using Microsoft.AspNet.Authentication.DataHandler.Encoder;
using Microsoft.AspNet.Authentication.DataHandler.Serializer;
namespace Microsoft.AspNet.Authentication.DataHandler
{
public class SecureDataFormat<TData> : ISecureDataFormat<TData>
{
private readonly IDataSerializer<TData> _serializer;
private readonly IDataProtector _protector;
private readonly ITextEncoder _encoder;
public SecureDataFormat(IDataSerializer<TData> serializer, IDataProtector protector, ITextEncoder encoder)
{
_serializer = serializer;
_protector = protector;
_encoder = encoder;
}
public string Protect(TData data)
{
byte[] userData = _serializer.Serialize(data);
byte[] protectedData = _protector.Protect(userData);
string protectedText = _encoder.Encode(protectedData);
return protectedText;
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Exception will be traced")]
public TData Unprotect(string protectedText)
{
try
{
if (protectedText == null)
{
return default(TData);
}
byte[] protectedData = _encoder.Decode(protectedText);
if (protectedData == null)
{
return default(TData);
}
byte[] userData = _protector.Unprotect(protectedData);
if (userData == null)
{
return default(TData);
}
TData model = _serializer.Deserialize(userData);
return model;
}
catch
{
// TODO trace exception, but do not leak other information
return default(TData);
}
}
}
}