From bf81ce39301b5ced14db71af23a4800c656c003e Mon Sep 17 00:00:00 2001 From: martincostello Date: Sun, 28 Mar 2021 13:57:25 +0100 Subject: [PATCH] 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)