From bf81ce39301b5ced14db71af23a4800c656c003e Mon Sep 17 00:00:00 2001 From: martincostello Date: Sun, 28 Mar 2021 13:57:25 +0100 Subject: [PATCH 1/3] Support roundtrip serialization with S.T.Json Add attribute to support round-trip serialization of AuthenticationProperties with System.Text.Json. Resolves #20722. --- .../src/AuthenticationProperties.cs | 2 ++ .../test/AuthenticationPropertiesTests.cs | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Http/Authentication.Abstractions/src/AuthenticationProperties.cs b/src/Http/Authentication.Abstractions/src/AuthenticationProperties.cs index 89768d6d53c5..3a72df17bda8 100644 --- a/src/Http/Authentication.Abstractions/src/AuthenticationProperties.cs +++ b/src/Http/Authentication.Abstractions/src/AuthenticationProperties.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Text.Json.Serialization; namespace Microsoft.AspNetCore.Authentication { @@ -30,6 +31,7 @@ public AuthenticationProperties() /// Initializes a new instance of the class. /// /// State values dictionary to use. + [JsonConstructor] public AuthenticationProperties(IDictionary items) : this(items, parameters: null) { } diff --git a/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs b/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs index c511208a3c53..96d06b9adcee 100644 --- a/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs +++ b/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Text.Json; using Xunit; namespace Microsoft.AspNetCore.Authentication.Core.Test @@ -302,6 +303,41 @@ public void GetBool() Assert.Equal("BAR", props.Items["foo"]); } + [Fact] + public void Roundtrip_Serializes_With_SystemTextJson() + { + var props = new AuthenticationProperties() + { + AllowRefresh = true, + ExpiresUtc = new DateTimeOffset(2021, 03, 28, 13, 47, 00, TimeSpan.Zero), + IssuedUtc = new DateTimeOffset(2021, 03, 28, 12, 47, 00, TimeSpan.Zero), + IsPersistent = true, + RedirectUri = "/foo/bar" + }; + + props.Items.Add("foo", "bar"); + + props.Parameters.Add("baz", "quux"); + + var json = JsonSerializer.Serialize(props); + props = JsonSerializer.Deserialize(json); + + Assert.NotNull(props); + + Assert.True(props!.AllowRefresh); + Assert.Equal(new DateTimeOffset(2021, 03, 28, 13, 47, 00, TimeSpan.Zero), props.ExpiresUtc); + Assert.Equal(new DateTimeOffset(2021, 03, 28, 12, 47, 00, TimeSpan.Zero), props.IssuedUtc); + Assert.True(props.IsPersistent); + Assert.Equal("/foo/bar", props.RedirectUri); + + Assert.NotNull(props.Items); + Assert.True(props.Items.ContainsKey("foo")); + Assert.Equal("bar", props.Items["foo"]); + + Assert.NotNull(props.Parameters); + Assert.Equal(0, props.Parameters.Count); + } + public class MyAuthenticationProperties : AuthenticationProperties { public new DateTimeOffset? GetDateTimeOffset(string key) From 89e21ba3b3ad85a832a2e0698440b12c3680634c Mon Sep 17 00:00:00 2001 From: martincostello Date: Sun, 28 Mar 2021 20:20:46 +0100 Subject: [PATCH 2/3] Refactor test Refactor test to not overwrite the input. --- .../test/AuthenticationPropertiesTests.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs b/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs index 96d06b9adcee..e9af6efe9e6f 100644 --- a/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs +++ b/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs @@ -320,22 +320,22 @@ public void Roundtrip_Serializes_With_SystemTextJson() props.Parameters.Add("baz", "quux"); var json = JsonSerializer.Serialize(props); - props = JsonSerializer.Deserialize(json); + var deserialized = JsonSerializer.Deserialize(json); - Assert.NotNull(props); + Assert.NotNull(deserialized); - Assert.True(props!.AllowRefresh); - Assert.Equal(new DateTimeOffset(2021, 03, 28, 13, 47, 00, TimeSpan.Zero), props.ExpiresUtc); - Assert.Equal(new DateTimeOffset(2021, 03, 28, 12, 47, 00, TimeSpan.Zero), props.IssuedUtc); - Assert.True(props.IsPersistent); - Assert.Equal("/foo/bar", props.RedirectUri); + Assert.Equal(props.AllowRefresh, deserialized!.AllowRefresh); + Assert.Equal(props.ExpiresUtc, deserialized.ExpiresUtc); + Assert.Equal(props.IssuedUtc, deserialized.IssuedUtc); + Assert.Equal(props.IsPersistent, deserialized.IsPersistent); + Assert.Equal(props.RedirectUri, deserialized.RedirectUri); - Assert.NotNull(props.Items); - Assert.True(props.Items.ContainsKey("foo")); - Assert.Equal("bar", props.Items["foo"]); + Assert.NotNull(deserialized.Items); + Assert.True(deserialized.Items.ContainsKey("foo")); + Assert.Equal(props.Items["foo"], deserialized.Items["foo"]); - Assert.NotNull(props.Parameters); - Assert.Equal(0, props.Parameters.Count); + Assert.NotNull(deserialized.Parameters); + Assert.Equal(0, deserialized.Parameters.Count); } public class MyAuthenticationProperties : AuthenticationProperties From 51a70859a8dfe56bf0919b9d626fba9503dcb10a Mon Sep 17 00:00:00 2001 From: martincostello Date: Mon, 29 Mar 2021 19:04:37 +0100 Subject: [PATCH 3/3] Add comment for clarity Add comment explaining that parameters are expected not to be round-tripped as suggested by code review. --- .../Authentication.Core/test/AuthenticationPropertiesTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs b/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs index e9af6efe9e6f..e486ce3255a7 100644 --- a/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs +++ b/src/Http/Authentication.Core/test/AuthenticationPropertiesTests.cs @@ -334,6 +334,7 @@ public void Roundtrip_Serializes_With_SystemTextJson() Assert.True(deserialized.Items.ContainsKey("foo")); Assert.Equal(props.Items["foo"], deserialized.Items["foo"]); + // Ensure that parameters are not round-tripped Assert.NotNull(deserialized.Parameters); Assert.Equal(0, deserialized.Parameters.Count); }