Repository navigation
Expand file tree
/
Copy pathCustomStateDataFormat.cs
More file actions
58 lines (48 loc) · 1.93 KB
/
Copy pathCustomStateDataFormat.cs
File metadata and controls
58 lines (48 loc) · 1.93 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
// 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.IO;
using System.Runtime.Serialization;
using System.Text;
namespace Microsoft.AspNetCore.Authentication.WsFederation
{
public class CustomStateDataFormat : ISecureDataFormat<AuthenticationProperties>
{
public const string ValidStateData = "ValidStateData";
private string lastSavedAuthenticationProperties;
private DataContractSerializer serializer = new DataContractSerializer(typeof(AuthenticationProperties));
public string Protect(AuthenticationProperties data)
{
lastSavedAuthenticationProperties = Serialize(data);
return ValidStateData;
}
public string Protect(AuthenticationProperties data, string purpose)
{
return Protect(data);
}
public AuthenticationProperties Unprotect(string state)
{
return state == ValidStateData ? DeSerialize(lastSavedAuthenticationProperties) : null;
}
public AuthenticationProperties Unprotect(string protectedText, string purpose)
{
return Unprotect(protectedText);
}
private string Serialize(AuthenticationProperties data)
{
using (MemoryStream memoryStream = new MemoryStream())
{
serializer.WriteObject(memoryStream, data);
memoryStream.Position = 0;
return new StreamReader(memoryStream).ReadToEnd();
}
}
private AuthenticationProperties DeSerialize(string state)
{
var stateDataAsBytes = Encoding.UTF8.GetBytes(state);
using (var ms = new MemoryStream(stateDataAsBytes, false))
{
return (AuthenticationProperties)serializer.ReadObject(ms);
}
}
}
}