Skip to content

Commit 0984c6d

Browse files
Do not serialize Parameters property on AuthenticationProperties (#31414)
* Do not serialize Parameters Explicitly prevent the Parameters dictionary from being included in the deserialized payload. See #31330 (comment). * Ignore props backed by Items Also ignore the properties backed by the Items dictionary to minimise the size of the serialized JSON payload. * Add ignoreLineEndingDifferences Stop tests from failing on non-Windows OSs due to different line endings.
1 parent fe57908 commit 0984c6d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/Http/Authentication.Abstractions/src/AuthenticationProperties.cs

+6
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ public AuthenticationProperties Clone()
6565
/// Collection of parameters that are passed to the authentication handler. These are not intended for
6666
/// serialization or persistence, only for flowing data between call sites.
6767
/// </summary>
68+
[JsonIgnore]
6869
public IDictionary<string, object?> Parameters { get; }
6970

7071
/// <summary>
7172
/// Gets or sets whether the authentication session is persisted across multiple requests.
7273
/// </summary>
74+
[JsonIgnore]
7375
public bool IsPersistent
7476
{
7577
get => GetString(IsPersistentKey) != null;
@@ -79,6 +81,7 @@ public bool IsPersistent
7981
/// <summary>
8082
/// Gets or sets the full path or absolute URI to be used as an http redirect response value.
8183
/// </summary>
84+
[JsonIgnore]
8285
public string? RedirectUri
8386
{
8487
get => GetString(RedirectUriKey);
@@ -88,6 +91,7 @@ public string? RedirectUri
8891
/// <summary>
8992
/// Gets or sets the time at which the authentication ticket was issued.
9093
/// </summary>
94+
[JsonIgnore]
9195
public DateTimeOffset? IssuedUtc
9296
{
9397
get => GetDateTimeOffset(IssuedUtcKey);
@@ -97,6 +101,7 @@ public DateTimeOffset? IssuedUtc
97101
/// <summary>
98102
/// Gets or sets the time at which the authentication ticket expires.
99103
/// </summary>
104+
[JsonIgnore]
100105
public DateTimeOffset? ExpiresUtc
101106
{
102107
get => GetDateTimeOffset(ExpiresUtcKey);
@@ -106,6 +111,7 @@ public DateTimeOffset? ExpiresUtc
106111
/// <summary>
107112
/// Gets or sets if refreshing the authentication session should be allowed.
108113
/// </summary>
114+
[JsonIgnore]
109115
public bool? AllowRefresh
110116
{
111117
get => GetBool(RefreshKey);

src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs

+50
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ public void Roundtrip_Serializes_With_SystemTextJson()
320320
props.Parameters.Add("baz", "quux");
321321

322322
var json = JsonSerializer.Serialize(props);
323+
324+
// Verify that Parameters was not serialized
325+
Assert.NotNull(json);
326+
Assert.DoesNotContain("baz", json);
327+
Assert.DoesNotContain("quux", json);
328+
323329
var deserialized = JsonSerializer.Deserialize<AuthenticationProperties>(json);
324330

325331
Assert.NotNull(deserialized);
@@ -339,6 +345,50 @@ public void Roundtrip_Serializes_With_SystemTextJson()
339345
Assert.Equal(0, deserialized.Parameters.Count);
340346
}
341347

348+
[Fact]
349+
public void Parameters_Is_Not_Deserialized_With_SystemTextJson()
350+
{
351+
var json = @"{""Parameters"":{""baz"":""quux""}}";
352+
353+
var deserialized = JsonSerializer.Deserialize<AuthenticationProperties>(json);
354+
355+
Assert.NotNull(deserialized);
356+
357+
// Ensure that parameters is not deserialized from a raw payload
358+
Assert.NotNull(deserialized!.Parameters);
359+
Assert.Equal(0, deserialized.Parameters.Count);
360+
}
361+
362+
[Fact]
363+
public void Serialization_Is_Minimised_With_SystemTextJson()
364+
{
365+
var props = new AuthenticationProperties()
366+
{
367+
AllowRefresh = true,
368+
ExpiresUtc = new DateTimeOffset(2021, 03, 28, 13, 47, 00, TimeSpan.Zero),
369+
IssuedUtc = new DateTimeOffset(2021, 03, 28, 12, 47, 00, TimeSpan.Zero),
370+
IsPersistent = true,
371+
RedirectUri = "/foo/bar"
372+
};
373+
374+
props.Items.Add("foo", "bar");
375+
376+
var options = new JsonSerializerOptions() { WriteIndented = true }; // Indented for readability if test fails
377+
var json = JsonSerializer.Serialize(props, options);
378+
379+
// Verify that the payload doesn't duplicate the properties backed by Items
380+
Assert.Equal(@"{
381+
""Items"": {
382+
"".refresh"": ""True"",
383+
"".expires"": ""Sun, 28 Mar 2021 13:47:00 GMT"",
384+
"".issued"": ""Sun, 28 Mar 2021 12:47:00 GMT"",
385+
"".persistent"": """",
386+
"".redirect"": ""/foo/bar"",
387+
""foo"": ""bar""
388+
}
389+
}", json, ignoreLineEndingDifferences: true);
390+
}
391+
342392
public class MyAuthenticationProperties : AuthenticationProperties
343393
{
344394
public new DateTimeOffset? GetDateTimeOffset(string key)

0 commit comments

Comments
 (0)