From c90eb2ecfcd1abc035500b41cb0b3848909cf29c Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 26 Jun 2025 10:16:25 -0700 Subject: [PATCH 1/8] Fixed bug 62257. --- .../src/TokenExtensions.cs | 4 +- .../test/TokenExtensionTests.cs | 61 ++++++++++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/Http/Authentication.Abstractions/src/TokenExtensions.cs b/src/Http/Authentication.Abstractions/src/TokenExtensions.cs index 5e91c64d7094..68f578586fab 100644 --- a/src/Http/Authentication.Abstractions/src/TokenExtensions.cs +++ b/src/Http/Authentication.Abstractions/src/TokenExtensions.cs @@ -71,9 +71,9 @@ public static void StoreTokens(this AuthenticationProperties properties, IEnumer /// /// The to update. /// The token name. - /// The token value. + /// The token value. May be null. /// if the token was updated, otherwise . - public static bool UpdateTokenValue(this AuthenticationProperties properties, string tokenName, string tokenValue) + public static bool UpdateTokenValue(this AuthenticationProperties properties, string tokenName, string? tokenValue) { ArgumentNullException.ThrowIfNull(properties); ArgumentNullException.ThrowIfNull(tokenName); diff --git a/src/Http/Authentication.Core/test/TokenExtensionTests.cs b/src/Http/Authentication.Core/test/TokenExtensionTests.cs index 98c1558ae9e7..ff1ad4e917f7 100644 --- a/src/Http/Authentication.Core/test/TokenExtensionTests.cs +++ b/src/Http/Authentication.Core/test/TokenExtensionTests.cs @@ -1,9 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Security.Claims; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using System.Security.Claims; +using static Microsoft.AspNetCore.Authentication.Core.Test.AuthenticationPropertiesTests; namespace Microsoft.AspNetCore.Authentication.Core.Test; @@ -149,6 +150,64 @@ public async Task GetTokenWorksWithExplicitScheme() Assert.Equal("3", await context.GetTokenAsync("simple", "Three")); } + [Fact] + public void StoreTokensStoresTokensWithNullValues() + { + var properties = new AuthenticationProperties(); + var tokens = new List + { + new AuthenticationToken { Name = "access_token", Value = "access_value" }, + new AuthenticationToken { Name = "refresh_token", Value = null }, + new AuthenticationToken { Name = "id_token", Value = "id_value" } + }; + + properties.StoreTokens(tokens); + + Assert.Equal("access_value", properties.GetTokenValue("access_token")); + Assert.Null(properties.GetTokenValue("refresh_token")); + Assert.Equal("id_value", properties.GetTokenValue("id_token")); + + var retrievedTokens = properties.GetTokens().ToList(); + Assert.Equal(2, retrievedTokens.Count); + Assert.DoesNotContain(retrievedTokens, t => t.Name == "refresh_token"); + } + + [Fact] + public void UpdateTokenValueWorksWithNullGetTokenValueResult() + { + var sourceProperties = new AuthenticationProperties(); + var targetProperties = new AuthenticationProperties(); + + var tokens = new List + { + new AuthenticationToken { Name = "refresh_token", Value = "refresh_value" } + }; + + targetProperties.StoreTokens(tokens); + targetProperties.UpdateTokenValue("refresh_token", sourceProperties.GetTokenValue("refresh_token")); + Assert.Null(targetProperties.GetTokenValue("refresh_token")); + } + + [Fact] + public void GetTokensExcludesTokensWithNullValues() + { + var properties = new AuthenticationProperties(); + var tokens = new List + { + new AuthenticationToken { Name = "access_token", Value = "access_value" }, + new AuthenticationToken { Name = "refresh_token", Value = null }, + new AuthenticationToken { Name = "id_token", Value = "id_value" } + }; + + properties.StoreTokens(tokens); + var retrievedTokens = properties.GetTokens().ToList(); + + Assert.Equal(2, retrievedTokens.Count); + Assert.Contains(retrievedTokens, t => t.Name == "access_token" && t.Value == "access_value"); + Assert.Contains(retrievedTokens, t => t.Name == "id_token" && t.Value == "id_value"); + Assert.DoesNotContain(retrievedTokens, t => t.Name == "refresh_token"); + } + private class SimpleAuth : IAuthenticationHandler { public Task AuthenticateAsync() From de9106dd1b5d286ab23ef016a167efadb0e16d52 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 26 Jun 2025 11:12:11 -0700 Subject: [PATCH 2/8] Comments fix. --- .../Authentication.Abstractions/src/PublicAPI.Unshipped.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt b/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt index 7dc5c58110bf..79314df6e490 100644 --- a/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt @@ -1 +1,4 @@ #nullable enable +#nullable enable +*REMOVED*static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string! tokenValue) -> bool +static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string? tokenValue) -> bool From d0f825600ea31eb7d051607c79400829787478cd Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 26 Jun 2025 11:28:17 -0700 Subject: [PATCH 3/8] More changes. --- .../Authentication.Abstractions/src/PublicAPI.Unshipped.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt b/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt index 79314df6e490..dffc77efda1f 100644 --- a/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt @@ -1,4 +1,3 @@ #nullable enable -#nullable enable -*REMOVED*static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string! tokenValue) -> bool +static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string! tokenValue) -> bool static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string? tokenValue) -> bool From b4e5be43c02ae0432ef69300b66c2eb99d0e876d Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 26 Jun 2025 15:11:48 -0700 Subject: [PATCH 4/8] Removed duplicate. --- src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt b/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt index dffc77efda1f..68d396c3e927 100644 --- a/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt @@ -1,3 +1,2 @@ #nullable enable -static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string! tokenValue) -> bool static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string? tokenValue) -> bool From 68eb1cbff78fc52a9db71f8e256f16062605cf5a Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Thu, 26 Jun 2025 16:14:09 -0700 Subject: [PATCH 5/8] More changes. --- src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt b/src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt index 2f23ee812efc..17a27f8f1359 100644 --- a/src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt +++ b/src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt @@ -162,4 +162,3 @@ static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.GetToke static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.GetTokens(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties) -> System.Collections.Generic.IEnumerable! static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.GetTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName) -> string? static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.StoreTokens(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, System.Collections.Generic.IEnumerable! tokens) -> void -static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string! tokenValue) -> bool From 68db86de509c791f72ebd9102a42e3bcc5d4568b Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Fri, 27 Jun 2025 10:45:36 -0700 Subject: [PATCH 6/8] More fixes to resolve build errors. --- src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt | 1 + src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt b/src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt index 17a27f8f1359..2f23ee812efc 100644 --- a/src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt +++ b/src/Http/Authentication.Abstractions/src/PublicAPI.Shipped.txt @@ -162,3 +162,4 @@ static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.GetToke static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.GetTokens(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties) -> System.Collections.Generic.IEnumerable! static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.GetTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName) -> string? static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.StoreTokens(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, System.Collections.Generic.IEnumerable! tokens) -> void +static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string! tokenValue) -> bool diff --git a/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt b/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt index 68d396c3e927..5bd5a8896c0b 100644 --- a/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt +++ b/src/Http/Authentication.Abstractions/src/PublicAPI.Unshipped.txt @@ -1,2 +1,3 @@ #nullable enable +*REMOVED*static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string! tokenValue) -> bool static Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions.UpdateTokenValue(this Microsoft.AspNetCore.Authentication.AuthenticationProperties! properties, string! tokenName, string? tokenValue) -> bool From 94db68d7d95dece324bd85f44932cb26eb267713 Mon Sep 17 00:00:00 2001 From: Adit Sheth Date: Fri, 27 Jun 2025 11:08:59 -0700 Subject: [PATCH 7/8] More updates. --- .../test/TokenExtensionTests.cs | 42 ------------------- 1 file changed, 42 deletions(-) diff --git a/src/Http/Authentication.Core/test/TokenExtensionTests.cs b/src/Http/Authentication.Core/test/TokenExtensionTests.cs index ff1ad4e917f7..98541ba19303 100644 --- a/src/Http/Authentication.Core/test/TokenExtensionTests.cs +++ b/src/Http/Authentication.Core/test/TokenExtensionTests.cs @@ -150,28 +150,6 @@ public async Task GetTokenWorksWithExplicitScheme() Assert.Equal("3", await context.GetTokenAsync("simple", "Three")); } - [Fact] - public void StoreTokensStoresTokensWithNullValues() - { - var properties = new AuthenticationProperties(); - var tokens = new List - { - new AuthenticationToken { Name = "access_token", Value = "access_value" }, - new AuthenticationToken { Name = "refresh_token", Value = null }, - new AuthenticationToken { Name = "id_token", Value = "id_value" } - }; - - properties.StoreTokens(tokens); - - Assert.Equal("access_value", properties.GetTokenValue("access_token")); - Assert.Null(properties.GetTokenValue("refresh_token")); - Assert.Equal("id_value", properties.GetTokenValue("id_token")); - - var retrievedTokens = properties.GetTokens().ToList(); - Assert.Equal(2, retrievedTokens.Count); - Assert.DoesNotContain(retrievedTokens, t => t.Name == "refresh_token"); - } - [Fact] public void UpdateTokenValueWorksWithNullGetTokenValueResult() { @@ -188,26 +166,6 @@ public void UpdateTokenValueWorksWithNullGetTokenValueResult() Assert.Null(targetProperties.GetTokenValue("refresh_token")); } - [Fact] - public void GetTokensExcludesTokensWithNullValues() - { - var properties = new AuthenticationProperties(); - var tokens = new List - { - new AuthenticationToken { Name = "access_token", Value = "access_value" }, - new AuthenticationToken { Name = "refresh_token", Value = null }, - new AuthenticationToken { Name = "id_token", Value = "id_value" } - }; - - properties.StoreTokens(tokens); - var retrievedTokens = properties.GetTokens().ToList(); - - Assert.Equal(2, retrievedTokens.Count); - Assert.Contains(retrievedTokens, t => t.Name == "access_token" && t.Value == "access_value"); - Assert.Contains(retrievedTokens, t => t.Name == "id_token" && t.Value == "id_value"); - Assert.DoesNotContain(retrievedTokens, t => t.Name == "refresh_token"); - } - private class SimpleAuth : IAuthenticationHandler { public Task AuthenticateAsync() From 8aac5ed25ed049c3782d0e6ea84a9bab63d199e8 Mon Sep 17 00:00:00 2001 From: Stephen Halter Date: Mon, 30 Jun 2025 13:36:36 -0700 Subject: [PATCH 8/8] Update src/Http/Authentication.Core/test/TokenExtensionTests.cs --- src/Http/Authentication.Core/test/TokenExtensionTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Http/Authentication.Core/test/TokenExtensionTests.cs b/src/Http/Authentication.Core/test/TokenExtensionTests.cs index 98541ba19303..1a1325d27cdc 100644 --- a/src/Http/Authentication.Core/test/TokenExtensionTests.cs +++ b/src/Http/Authentication.Core/test/TokenExtensionTests.cs @@ -1,10 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Security.Claims; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; -using System.Security.Claims; -using static Microsoft.AspNetCore.Authentication.Core.Test.AuthenticationPropertiesTests; namespace Microsoft.AspNetCore.Authentication.Core.Test;