Skip to content

Commit 7ca3fda

Browse files
authored
Reenable CA1305 warnings (#49695)
1 parent bf52af4 commit 7ca3fda

File tree

10 files changed

+33
-28
lines changed

10 files changed

+33
-28
lines changed

.editorconfig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ dotnet_diagnostic.CA1018.severity = warning
8787
dotnet_diagnostic.CA1047.severity = warning
8888

8989
# CA1305: Specify IFormatProvider
90-
# TODO: Renable as warning after https://github.com/dotnet/roslyn-analyzers/issues/6746 is resolved
91-
dotnet_diagnostic.CA1305.severity = suggestion
90+
dotnet_diagnostic.CA1305.severity = warning
9291

9392
# CA1507: Use nameof to express symbol names
9493
dotnet_diagnostic.CA1507.severity = warning

src/Antiforgery/samples/MinimalFormSample/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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.Globalization;
45
using Microsoft.AspNetCore.Antiforgery;
56
using Microsoft.AspNetCore.Mvc;
67

@@ -54,7 +55,7 @@
5455
{
5556
var form = await context.Request.ReadFormAsync();
5657
var name = form["name"].ToString();
57-
var dueDate = DateTime.Parse(form["dueDate"].ToString());
58+
var dueDate = DateTime.Parse(form["dueDate"].ToString(), CultureInfo.InvariantCulture);
5859
var isCompleted = bool.Parse(form["isCompleted"].ToString());
5960
var result = Results.Ok(new Todo(name, isCompleted, dueDate));
6061
await result.ExecuteAsync(context);

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.EventDispatch.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Http;
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Hosting;
9+
using System.Globalization;
910
using System.Linq;
1011
using System.Text;
1112

@@ -58,7 +59,7 @@ private string CreateMessageForAmbiguousNamedSubmitEvent(string scopeQualifiedNa
5859

5960
foreach (var location in locations)
6061
{
61-
sb.Append($"\n - {GenerateComponentPath(location.ComponentId)}");
62+
sb.Append(CultureInfo.InvariantCulture, $"\n - {GenerateComponentPath(location.ComponentId)}");
6263
}
6364

6465
return sb.ToString();

src/Components/test/E2ETest/Tests/ComponentRenderingTestBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Configuration.Assemblies;
5+
using System.Globalization;
56
using System.Numerics;
67
using BasicTestApp;
78
using BasicTestApp.HierarchicalImportsTest.Subdir;
@@ -401,7 +402,7 @@ public void CanUseFocusExtensionToFocusElement()
401402
string getFocusedElementId() => Browser.SwitchTo().ActiveElement().GetAttribute("id");
402403

403404
// A local helper that gets window.PageYOffset
404-
int getPageYOffset() => Convert.ToInt32(((IJavaScriptExecutor)Browser).ExecuteScript("return window.pageYOffset"));
405+
int getPageYOffset() => Convert.ToInt32(((IJavaScriptExecutor)Browser).ExecuteScript("return window.pageYOffset"), CultureInfo.InvariantCulture);
405406
}
406407

407408
[Fact]
@@ -456,7 +457,7 @@ public void CanUseFocusExtensionToFocusElementPreventScroll()
456457
string getFocusedElementId() => Browser.SwitchTo().ActiveElement().GetAttribute("id");
457458

458459
// A local helper that gets window.PageYOffset
459-
int getPageYOffset() => Convert.ToInt32(((IJavaScriptExecutor)Browser).ExecuteScript("return window.pageYOffset"));
460+
int getPageYOffset() => Convert.ToInt32(((IJavaScriptExecutor)Browser).ExecuteScript("return window.pageYOffset"), CultureInfo.InvariantCulture);
460461
}
461462

462463
[Theory]

src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/FrameworkParametersCompletionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public override async Task ProvideCompletionsAsync(CompletionContext context)
263263

264264
if (change.NewPosition != null)
265265
{
266-
properties.Add(NewPositionKey, change.NewPosition.ToString());
266+
properties.Add(NewPositionKey, change.NewPosition.Value.ToString(CultureInfo.InvariantCulture));
267267
}
268268

269269
// Keep everything sorted in the order we just produced the items in.

src/Framework/AspNetCoreAnalyzers/src/Analyzers/RouteEmbeddedLanguage/RoutePatternCompletionProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ not CompletionTriggerKind.InvokeAndCommitIfUnique and
142142

143143
if (change.NewPosition != null)
144144
{
145-
properties.Add(NewPositionKey, change.NewPosition.ToString());
145+
properties.Add(NewPositionKey, change.NewPosition.Value.ToString(CultureInfo.InvariantCulture));
146146
}
147147

148148
// Keep everything sorted in the order we just produced the items in.

src/Http/Routing/test/FunctionalTests/AntiforgeryTests.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
3+
using System.Globalization;
34
using System.Net;
45
using System.Net.Http;
56
using System.Text.Json;
@@ -56,7 +57,7 @@ public async Task MapPost_WithForm_ValidToken_Works()
5657
new KeyValuePair<string,string>("__RequestVerificationToken", tokens.RequestToken),
5758
new KeyValuePair<string,string>("name", "Test task"),
5859
new KeyValuePair<string,string>("isComplete", "false"),
59-
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString()),
60+
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString(CultureInfo.InvariantCulture)),
6061
};
6162
request.Content = new FormUrlEncodedContent(nameValueCollection);
6263

@@ -88,7 +89,7 @@ public async Task MapRequestDelegate_WithForm_RequiresValidation_ValidToken_Work
8889
{
8990
Name = form["name"],
9091
IsCompleted = bool.Parse(form["isComplete"]),
91-
DueDate = DateTime.Parse(form["dueDate"])
92+
DueDate = DateTime.Parse(form["dueDate"], CultureInfo.InvariantCulture)
9293
};
9394
await context.Response.WriteAsJsonAsync(todo);
9495
}).WithMetadata(AntiforgeryMetadata.ValidationRequired));
@@ -116,7 +117,7 @@ public async Task MapRequestDelegate_WithForm_RequiresValidation_ValidToken_Work
116117
new KeyValuePair<string,string>("__RequestVerificationToken", tokens.RequestToken),
117118
new KeyValuePair<string,string>("name", "Test task"),
118119
new KeyValuePair<string,string>("isComplete", "false"),
119-
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString()),
120+
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString(CultureInfo.InvariantCulture)),
120121
};
121122
request.Content = new FormUrlEncodedContent(nameValueCollection);
122123

@@ -161,7 +162,7 @@ public async Task MapPost_WithForm_InvalidToken_Fails()
161162
{
162163
new KeyValuePair<string,string>("name", "Test task"),
163164
new KeyValuePair<string,string>("isComplete", "false"),
164-
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString()),
165+
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString(CultureInfo.InvariantCulture)),
165166
};
166167
request.Content = new FormUrlEncodedContent(nameValueCollection);
167168

@@ -200,7 +201,7 @@ public async Task MapPost_WithForm_WithoutMiddleware_ThrowsException()
200201
{
201202
new KeyValuePair<string,string>("name", "Test task"),
202203
new KeyValuePair<string,string>("isComplete", "false"),
203-
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString()),
204+
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString(CultureInfo.InvariantCulture)),
204205
};
205206
request.Content = new FormUrlEncodedContent(nameValueCollection);
206207

@@ -277,7 +278,7 @@ public async Task MapPost_WithForm_WithoutAntiforgery_WithoutMiddleware_Works()
277278
{
278279
new KeyValuePair<string,string>("name", "Test task"),
279280
new KeyValuePair<string,string>("isComplete", "false"),
280-
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString()),
281+
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString(CultureInfo.InvariantCulture)),
281282
};
282283
request.Content = new FormUrlEncodedContent(nameValueCollection);
283284

@@ -303,7 +304,7 @@ public static IEnumerable<object[]> RequestDelegateData
303304
{
304305
Name = form["name"],
305306
IsCompleted = bool.Parse(form["isComplete"]),
306-
DueDate = DateTime.Parse(form["dueDate"])
307+
DueDate = DateTime.Parse(form["dueDate"], CultureInfo.InvariantCulture)
307308
};
308309
await context.Response.WriteAsJsonAsync(todo);
309310
}),
@@ -317,7 +318,7 @@ public static IEnumerable<object[]> RequestDelegateData
317318
{
318319
Name = form["name"],
319320
IsCompleted = bool.Parse(form["isComplete"]),
320-
DueDate = DateTime.Parse(form["dueDate"])
321+
DueDate = DateTime.Parse(form["dueDate"], CultureInfo.InvariantCulture)
321322
};
322323
await context.Response.WriteAsJsonAsync(todo);
323324
}),
@@ -331,7 +332,7 @@ public static IEnumerable<object[]> RequestDelegateData
331332
{
332333
Name = form["name"],
333334
IsCompleted = bool.Parse(form["isComplete"]),
334-
DueDate = DateTime.Parse(form["dueDate"])
335+
DueDate = DateTime.Parse(form["dueDate"], CultureInfo.InvariantCulture)
335336
};
336337
await context.Response.WriteAsJsonAsync(todo);
337338
}),
@@ -373,7 +374,7 @@ public async Task MapRequestDelegate_WithForm_RequiresValidation_InvalidToken_Fa
373374
{
374375
new KeyValuePair<string,string>("name", "Test task"),
375376
new KeyValuePair<string,string>("isComplete", "false"),
376-
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString()),
377+
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString(CultureInfo.InvariantCulture)),
377378
};
378379
request.Content = new FormUrlEncodedContent(nameValueCollection);
379380

@@ -430,7 +431,7 @@ public async Task MapPost_WithForm_ValidToken_RequestSizeLimit_Works(bool hasLim
430431
new KeyValuePair<string,string>("__RequestVerificationToken", tokens.RequestToken),
431432
new KeyValuePair<string,string>("name", "Test task"),
432433
new KeyValuePair<string,string>("isComplete", "false"),
433-
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString()),
434+
new KeyValuePair<string,string>("dueDate", DateTime.Today.AddDays(1).ToString(CultureInfo.InvariantCulture)),
434435
};
435436
request.Content = new FormUrlEncodedContent(nameValueCollection);
436437

src/Identity/test/Identity.FunctionalTests/MapIdentityApiTests.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#nullable enable
55

6+
using System.Globalization;
67
using System.Net;
78
using System.Net.Http;
89
using System.Net.Http.Json;
@@ -612,7 +613,7 @@ public async Task CanEnableAndLoginWithTwoFactor(string addIdentityMode)
612613
var keyBytes = Base32.FromBase32(sharedKey);
613614
var unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
614615
var timestep = Convert.ToInt64(unixTimestamp / 30);
615-
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString();
616+
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString(CultureInfo.InvariantCulture);
616617

617618
var enable2faResponse = await client.PostAsJsonAsync("/identity/account/2fa", new { twoFactorCode, Enable = true });
618619
var enable2faContent = await enable2faResponse.Content.ReadFromJsonAsync<JsonElement>();
@@ -652,7 +653,7 @@ public async Task CanLoginWithRecoveryCodeAndDisableTwoFactor()
652653
var keyBytes = Base32.FromBase32(sharedKey);
653654
var unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
654655
var timestep = Convert.ToInt64(unixTimestamp / 30);
655-
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString();
656+
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString(CultureInfo.InvariantCulture);
656657

657658
var enable2faResponse = await client.PostAsJsonAsync("/identity/account/2fa", new { twoFactorCode, Enable = true });
658659
var enable2faContent = await enable2faResponse.Content.ReadFromJsonAsync<JsonElement>();
@@ -702,7 +703,7 @@ public async Task CanResetSharedKey()
702703
var keyBytes = Base32.FromBase32(sharedKey);
703704
var unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
704705
var timestep = Convert.ToInt64(unixTimestamp / 30);
705-
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString();
706+
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString(CultureInfo.InvariantCulture);
706707

707708
await AssertValidationProblemAsync(await client.PostAsJsonAsync("/identity/account/2fa", new { twoFactorCode, Enable = true, ResetSharedKey = true }),
708709
"CannotResetSharedKeyAndEnable");
@@ -718,7 +719,7 @@ public async Task CanResetSharedKey()
718719
var resetSharedKey = resetKeyContent.GetProperty("sharedKey").GetString();
719720

720721
var resetKeyBytes = Base32.FromBase32(sharedKey);
721-
var resetTwoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString();
722+
var resetTwoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString(CultureInfo.InvariantCulture);
722723

723724
// The old 2fa code no longer works
724725
await AssertValidationProblemAsync(await client.PostAsJsonAsync("/identity/account/2fa", new { twoFactorCode, Enable = true }),
@@ -748,7 +749,7 @@ public async Task CanResetRecoveryCodes()
748749
var keyBytes = Base32.FromBase32(sharedKey);
749750
var unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
750751
var timestep = Convert.ToInt64(unixTimestamp / 30);
751-
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString();
752+
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString(CultureInfo.InvariantCulture);
752753

753754
var enable2faResponse = await client.PostAsJsonAsync("/identity/account/2fa", new { twoFactorCode, Enable = true });
754755
var enable2faContent = await enable2faResponse.Content.ReadFromJsonAsync<JsonElement>();
@@ -815,7 +816,7 @@ public async Task CanUsePersistentTwoFactorCookies()
815816
var keyBytes = Base32.FromBase32(sharedKey);
816817
var unixTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
817818
var timestep = Convert.ToInt64(unixTimestamp / 30);
818-
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString();
819+
var twoFactorCode = Rfc6238AuthenticationService.ComputeTotp(keyBytes, (ulong)timestep, modifierBytes: null).ToString(CultureInfo.InvariantCulture);
819820

820821
var enable2faResponse = await client.PostAsJsonAsync("/identity/account/2fa", new { twoFactorCode, Enable = true });
821822
var enable2faContent = await enable2faResponse.Content.ReadFromJsonAsync<JsonElement>();

src/Middleware/Session/test/SessionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ public async Task RefreshesSession_WhenSessionData_IsNotModified()
463463
else if (context.Request.Path == new PathString("/AccessSessionData"))
464464
{
465465
var value = context.Session.GetInt32("Key");
466-
responseData = (value == null) ? "No value found in session." : value.ToString();
466+
responseData = (value == null) ? "No value found in session." : value.Value.ToString(CultureInfo.InvariantCulture);
467467
}
468468
else if (context.Request.Path == new PathString("/DoNotAccessSessionData"))
469469
{

src/Security/Authentication/test/JwtBearerTests_Handler.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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.Globalization;
45
using System.IdentityModel.Tokens.Jwt;
56
using System.Net;
67
using System.Net.Http;
@@ -951,11 +952,11 @@ public async Task ExpirationAndIssuedWhenMinOrMaxValue()
951952
var expiresElement = dom.RootElement.GetProperty("expires");
952953
Assert.Equal(JsonValueKind.String, expiresElement.ValueKind);
953954

954-
var elementValue = DateTime.Parse(expiresElement.GetString());
955+
var elementValue = DateTime.Parse(expiresElement.GetString(), CultureInfo.InvariantCulture);
955956
var elementValueUtc = elementValue.ToUniversalTime();
956957
// roundtrip DateTime.MaxValue through parsing because it is lossy and we
957958
// need equivalent values to compare against.
958-
var max = DateTime.Parse(DateTime.MaxValue.ToString());
959+
var max = DateTime.Parse(DateTime.MaxValue.ToString(CultureInfo.InvariantCulture), CultureInfo.InvariantCulture);
959960

960961
Assert.Equal(max, elementValueUtc);
961962
}

0 commit comments

Comments
 (0)