Skip to content

Commit

Permalink
Use InvariantCulture for collections as well
Browse files Browse the repository at this point in the history
#2453 was incomplete

Fixes #2409
  • Loading branch information
Keegan Caruso committed Jan 26, 2024
1 parent e34fba2 commit 9ea25a8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/System.IdentityModel.Tokens.Jwt/JwtPayload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,13 @@ private void AddListofObjects(string key, IEnumerable<object> objects, List<Clai
else if (obj is IEnumerable<object> innerObjects)
AddListofObjects(key, innerObjects, claims, issuer);
else
claims.Add(new Claim(key, obj.ToString(), GetClaimValueType(obj), issuer, issuer));
{
var claimValueType = GetClaimValueType(obj);
if (obj is IFormattable formattable)
claims.Add(new Claim(key, formattable.ToString(null, CultureInfo.InvariantCulture), claimValueType, issuer, issuer));
else
claims.Add(new Claim(key, obj.ToString(), claimValueType, issuer, issuer));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1635,20 +1635,35 @@ public void EscapedClaims()
[Fact]
public void DifferentCultureJsonWebToken()
{
string result = string.Empty;
string numericClaim = string.Empty;
List<Claim> numericList = null;

var thread = new Thread(() =>
{
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
var token = new JsonWebToken(JsonUtilities.CreateUnsignedToken("numericClaim", 10.9d));

var handler = new JsonWebTokenHandler();
var tokenStr = handler.CreateToken(new SecurityTokenDescriptor
{
Claims = new Dictionary<string, object>
{
{ "numericClaim", 10.9d },
{ "numericList", new List<object> { 12.2, 11.1 } }
}
});

var token = new JsonWebToken(tokenStr);
var claim = token.Claims.First(c => c.Type == "numericClaim");
result = claim.Value;
numericClaim = claim.Value;
numericList = token.Claims.Where(c => c.Type == "numericList").ToList();
});

thread.Start();
thread.Join();

Assert.Equal("10.9", result);
Assert.Equal("10.9", numericClaim);
Assert.Equal("12.2", numericList[0].Value);
Assert.Equal("11.1", numericList[1].Value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ public static TheoryData<JwtTheoryData> JwtSegmentTheoryData
[Fact]
public void DifferentCultureJwtSecurityToken()
{
string result = string.Empty;
string numericClaim = string.Empty;
List<Claim> numericList = null;

var thread = new Thread(() =>
{
Expand All @@ -470,19 +471,22 @@ public void DifferentCultureJwtSecurityToken()
{
Claims = new Dictionary<string, object>
{
{ "numericClaim", 10.9d }
{ "numericClaim", 10.9d },
{ "numericList", new List<object> { 12.2, 11.1 } }
}
});

var claim = token.Claims.First(c => c.Type == "numericClaim");
result = claim.Value;

numericClaim = claim.Value;
numericList = token.Claims.Where(c => c.Type == "numericList").ToList();
});

thread.Start();
thread.Join();

Assert.Equal("10.9", result);
Assert.Equal("10.9", numericClaim);
Assert.Equal("12.2", numericList[0].Value);
Assert.Equal("11.1", numericList[1].Value);
}
}
}

0 comments on commit 9ea25a8

Please sign in to comment.