Skip to content

Commit c90eb2e

Browse files
author
Adit Sheth
committed
Fixed bug 62257.
1 parent 062e663 commit c90eb2e

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/Http/Authentication.Abstractions/src/TokenExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public static void StoreTokens(this AuthenticationProperties properties, IEnumer
7171
/// </summary>
7272
/// <param name="properties">The <see cref="AuthenticationProperties"/> to update.</param>
7373
/// <param name="tokenName">The token name.</param>
74-
/// <param name="tokenValue">The token value.</param>
74+
/// <param name="tokenValue">The token value. May be <c>null</c>.</param>
7575
/// <returns><see langword="true"/> if the token was updated, otherwise <see langword="false"/>.</returns>
76-
public static bool UpdateTokenValue(this AuthenticationProperties properties, string tokenName, string tokenValue)
76+
public static bool UpdateTokenValue(this AuthenticationProperties properties, string tokenName, string? tokenValue)
7777
{
7878
ArgumentNullException.ThrowIfNull(properties);
7979
ArgumentNullException.ThrowIfNull(tokenName);

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Security.Claims;
54
using Microsoft.AspNetCore.Http;
65
using Microsoft.Extensions.DependencyInjection;
6+
using System.Security.Claims;
7+
using static Microsoft.AspNetCore.Authentication.Core.Test.AuthenticationPropertiesTests;
78

89
namespace Microsoft.AspNetCore.Authentication.Core.Test;
910

@@ -149,6 +150,64 @@ public async Task GetTokenWorksWithExplicitScheme()
149150
Assert.Equal("3", await context.GetTokenAsync("simple", "Three"));
150151
}
151152

153+
[Fact]
154+
public void StoreTokensStoresTokensWithNullValues()
155+
{
156+
var properties = new AuthenticationProperties();
157+
var tokens = new List<AuthenticationToken>
158+
{
159+
new AuthenticationToken { Name = "access_token", Value = "access_value" },
160+
new AuthenticationToken { Name = "refresh_token", Value = null },
161+
new AuthenticationToken { Name = "id_token", Value = "id_value" }
162+
};
163+
164+
properties.StoreTokens(tokens);
165+
166+
Assert.Equal("access_value", properties.GetTokenValue("access_token"));
167+
Assert.Null(properties.GetTokenValue("refresh_token"));
168+
Assert.Equal("id_value", properties.GetTokenValue("id_token"));
169+
170+
var retrievedTokens = properties.GetTokens().ToList();
171+
Assert.Equal(2, retrievedTokens.Count);
172+
Assert.DoesNotContain(retrievedTokens, t => t.Name == "refresh_token");
173+
}
174+
175+
[Fact]
176+
public void UpdateTokenValueWorksWithNullGetTokenValueResult()
177+
{
178+
var sourceProperties = new AuthenticationProperties();
179+
var targetProperties = new AuthenticationProperties();
180+
181+
var tokens = new List<AuthenticationToken>
182+
{
183+
new AuthenticationToken { Name = "refresh_token", Value = "refresh_value" }
184+
};
185+
186+
targetProperties.StoreTokens(tokens);
187+
targetProperties.UpdateTokenValue("refresh_token", sourceProperties.GetTokenValue("refresh_token"));
188+
Assert.Null(targetProperties.GetTokenValue("refresh_token"));
189+
}
190+
191+
[Fact]
192+
public void GetTokensExcludesTokensWithNullValues()
193+
{
194+
var properties = new AuthenticationProperties();
195+
var tokens = new List<AuthenticationToken>
196+
{
197+
new AuthenticationToken { Name = "access_token", Value = "access_value" },
198+
new AuthenticationToken { Name = "refresh_token", Value = null },
199+
new AuthenticationToken { Name = "id_token", Value = "id_value" }
200+
};
201+
202+
properties.StoreTokens(tokens);
203+
var retrievedTokens = properties.GetTokens().ToList();
204+
205+
Assert.Equal(2, retrievedTokens.Count);
206+
Assert.Contains(retrievedTokens, t => t.Name == "access_token" && t.Value == "access_value");
207+
Assert.Contains(retrievedTokens, t => t.Name == "id_token" && t.Value == "id_value");
208+
Assert.DoesNotContain(retrievedTokens, t => t.Name == "refresh_token");
209+
}
210+
152211
private class SimpleAuth : IAuthenticationHandler
153212
{
154213
public Task<AuthenticateResult> AuthenticateAsync()

0 commit comments

Comments
 (0)