|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Globalization;
|
4 | 4 | using System.Linq;
|
| 5 | +using System.Text.Json; |
5 | 6 | using Xunit;
|
6 | 7 |
|
7 | 8 | namespace Microsoft.AspNetCore.Authentication.Core.Test
|
@@ -302,6 +303,42 @@ public void GetBool()
|
302 | 303 | Assert.Equal("BAR", props.Items["foo"]);
|
303 | 304 | }
|
304 | 305 |
|
| 306 | + [Fact] |
| 307 | + public void Roundtrip_Serializes_With_SystemTextJson() |
| 308 | + { |
| 309 | + var props = new AuthenticationProperties() |
| 310 | + { |
| 311 | + AllowRefresh = true, |
| 312 | + ExpiresUtc = new DateTimeOffset(2021, 03, 28, 13, 47, 00, TimeSpan.Zero), |
| 313 | + IssuedUtc = new DateTimeOffset(2021, 03, 28, 12, 47, 00, TimeSpan.Zero), |
| 314 | + IsPersistent = true, |
| 315 | + RedirectUri = "/foo/bar" |
| 316 | + }; |
| 317 | + |
| 318 | + props.Items.Add("foo", "bar"); |
| 319 | + |
| 320 | + props.Parameters.Add("baz", "quux"); |
| 321 | + |
| 322 | + var json = JsonSerializer.Serialize(props); |
| 323 | + var deserialized = JsonSerializer.Deserialize<AuthenticationProperties>(json); |
| 324 | + |
| 325 | + Assert.NotNull(deserialized); |
| 326 | + |
| 327 | + Assert.Equal(props.AllowRefresh, deserialized!.AllowRefresh); |
| 328 | + Assert.Equal(props.ExpiresUtc, deserialized.ExpiresUtc); |
| 329 | + Assert.Equal(props.IssuedUtc, deserialized.IssuedUtc); |
| 330 | + Assert.Equal(props.IsPersistent, deserialized.IsPersistent); |
| 331 | + Assert.Equal(props.RedirectUri, deserialized.RedirectUri); |
| 332 | + |
| 333 | + Assert.NotNull(deserialized.Items); |
| 334 | + Assert.True(deserialized.Items.ContainsKey("foo")); |
| 335 | + Assert.Equal(props.Items["foo"], deserialized.Items["foo"]); |
| 336 | + |
| 337 | + // Ensure that parameters are not round-tripped |
| 338 | + Assert.NotNull(deserialized.Parameters); |
| 339 | + Assert.Equal(0, deserialized.Parameters.Count); |
| 340 | + } |
| 341 | + |
305 | 342 | public class MyAuthenticationProperties : AuthenticationProperties
|
306 | 343 | {
|
307 | 344 | public new DateTimeOffset? GetDateTimeOffset(string key)
|
|
0 commit comments