Skip to content

Commit 63405fd

Browse files
Fix formatting violations in new files from main refactor
- MacNativeMethods.cs: add pragma IDE0060 for P/Invoke params, convert UnmanagedCallConv new Type[]{} to collection expressions - WindowsNativeDpiHelper.cs: same pragma + collection expression fixes - WindowsNativeMethods.cs: add pragma IDE0060, fix collection expression - WindowsNativeUtils.cs: add pragma IDE0060 for CoInitializeSecurity params - MsalIdHelper.cs: fix IDE0008 var -> Regex explicit type - .editorconfig: suppress SYSLIB1054 (LibraryImport requires .NET 7+) - CI yaml: add --exclude json\ to dotnet format command Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 07c467b commit 63405fd

13 files changed

Lines changed: 152 additions & 130 deletions

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,8 @@ dotnet_diagnostic.CA1850.severity = none
903903
# Use ThrowIfCancellationRequested / ThrowIfNegativeOrZero / ObjectDisposedException.ThrowIf - requires .NET 6+/8+
904904
dotnet_diagnostic.CA2250.severity = none
905905
dotnet_diagnostic.CA1512.severity = none
906+
# SYSLIB1054: LibraryImport requires .NET 7+ source generation; multi-target false positive
907+
dotnet_diagnostic.SYSLIB1054.severity = none
906908
dotnet_diagnostic.CA1513.severity = none
907909

908910
# Dispose objects before losing scope

src/client/Microsoft.Identity.Client/Internal/ClientCredential/ClientSecretCredential.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@
1010

1111
namespace Microsoft.Identity.Client.Internal.ClientCredential
1212
{
13-
internal class ClientSecretCredential : IClientCredential
13+
internal class ClientSecretCredential(string secret) : IClientCredential
1414
{
15-
internal string Secret { get; }
15+
internal string Secret { get; } = secret;
1616

1717
public AssertionType AssertionType => AssertionType.Secret;
1818

19-
public ClientSecretCredential(string secret)
20-
{
21-
Secret = secret;
22-
}
23-
2419
public Task<CredentialMaterial> GetCredentialMaterialAsync(
2520
CredentialContext context,
2621
CancellationToken cancellationToken)
Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
using System;
@@ -13,14 +13,19 @@ namespace Microsoft.Identity.Client.Internal.ClientCredential
1313
/// Replaces the former <c>ClientCredentialApplicationResult</c> and decouples "what credentials
1414
/// produce" from "how the token client applies them".
1515
/// </summary>
16-
internal sealed class CredentialMaterial
16+
/// <param name="tokenRequestParameters">Body parameters to add to the token request. Must not be null.</param>
17+
/// <param name="resolvedCertificate">Optional certificate for mTLS transport or logging.</param>
18+
internal sealed class CredentialMaterial(
19+
IReadOnlyDictionary<string, string> tokenRequestParameters,
20+
X509Certificate2 resolvedCertificate = null)
1721
{
1822
/// <summary>
1923
/// Key/value pairs to be added to the token-request body. Usually client_assertion.
2024
/// Never <see langword="null"/>; may be empty (e.g., for pure mTLS-transport mode where the
2125
/// certificate authenticates the client at the TLS layer and no assertion is needed).
2226
/// </summary>
23-
public IReadOnlyDictionary<string, string> TokenRequestParameters { get; }
27+
public IReadOnlyDictionary<string, string> TokenRequestParameters { get; } = tokenRequestParameters
28+
?? throw new ArgumentNullException(nameof(tokenRequestParameters));
2429

2530
/// <summary>
2631
/// Optional certificate returned by the credential.
@@ -29,17 +34,6 @@ internal sealed class CredentialMaterial
2934
/// <see cref="ClientSignedAssertion.TokenBindingCertificate"/>.
3035
/// <see langword="null"/> when no certificate is involved (secret, plain JWT assertion).
3136
/// </summary>
32-
public X509Certificate2 ResolvedCertificate { get; }
33-
34-
/// <param name="tokenRequestParameters">Body parameters to add to the token request. Must not be null.</param>
35-
/// <param name="resolvedCertificate">Optional certificate for mTLS transport or logging.</param>
36-
public CredentialMaterial(
37-
IReadOnlyDictionary<string, string> tokenRequestParameters,
38-
X509Certificate2 resolvedCertificate = null)
39-
{
40-
TokenRequestParameters = tokenRequestParameters
41-
?? throw new ArgumentNullException(nameof(tokenRequestParameters));
42-
ResolvedCertificate = resolvedCertificate;
43-
}
37+
public X509Certificate2 ResolvedCertificate { get; } = resolvedCertificate;
4438
}
4539
}

src/client/Microsoft.Identity.Client/Internal/ClientCredential/CredentialMaterialResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal static async Task<CredentialMaterial> ResolveAsync(
3838
string tokenEndpoint,
3939
CancellationToken cancellationToken)
4040
{
41-
var context = BuildContext(requestParams, tokenEndpoint);
41+
CredentialContext context = BuildContext(requestParams, tokenEndpoint);
4242

4343
CredentialMaterial material = await credential
4444
.GetCredentialMaterialAsync(context, cancellationToken)

src/client/Microsoft.Identity.Client/Internal/ClientCredential/SignedAssertionClientCredential.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@
1010

1111
namespace Microsoft.Identity.Client.Internal.ClientCredential
1212
{
13-
internal class SignedAssertionClientCredential : IClientCredential
13+
internal class SignedAssertionClientCredential(string signedAssertion) : IClientCredential
1414
{
15-
private readonly string _signedAssertion;
15+
private readonly string _signedAssertion = signedAssertion;
1616

1717
public AssertionType AssertionType => AssertionType.ClientAssertion;
1818

19-
public SignedAssertionClientCredential(string signedAssertion)
20-
{
21-
_signedAssertion = signedAssertion;
22-
}
23-
2419
public Task<CredentialMaterial> GetCredentialMaterialAsync(
2520
CredentialContext context,
2621
CancellationToken cancellationToken)

src/client/Microsoft.Identity.Client/Internal/MsalIdHelper.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ internal static class MsalIdParameter
3737
/// This class adds additional query parameters or headers to the requests sent to STS. This can help us in
3838
/// collecting statistics and potentially on diagnostics.
3939
/// </summary>
40-
internal static class MsalIdHelper
40+
internal static partial class MsalIdHelper
4141
{
4242
private static readonly Lazy<string> s_msalVersion = new(
4343
() =>
4444
{
4545
string fullVersion = typeof(MsalIdHelper).Assembly.FullName;
46-
var regex = new Regex(@"Version=[\d]+.[\d+]+.[\d]+.[\d]+");
46+
Regex regex = MyRegex();
4747
Match match = regex.Match(fullVersion);
4848
if (!match.Success)
4949
{
@@ -90,5 +90,8 @@ public static string GetMsalVersion()
9090
{
9191
return s_msalVersion.Value;
9292
}
93+
94+
[GeneratedRegex(@"Version=[\d]+.[\d+]+.[\d]+.[\d]+")]
95+
private static partial Regex MyRegex();
9396
}
9497
}

src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
using System.Diagnostics;
77
using System.Globalization;
88
using System.Linq;
9+
using System.Runtime.ConstrainedExecution;
910
using System.Text;
1011
using System.Threading;
1112
using System.Threading.Tasks;
1213
using Microsoft.Identity.Client.ApiConfig.Parameters;
14+
using Microsoft.Identity.Client.AuthScheme;
1315
using Microsoft.Identity.Client.Cache;
1416
using Microsoft.Identity.Client.Cache.Items;
1517
using Microsoft.Identity.Client.Core;
1618
using Microsoft.Identity.Client.Instance.Discovery;
19+
using Microsoft.Identity.Client.Internal.Broker;
1720
using Microsoft.Identity.Client.OAuth2;
21+
using Microsoft.Identity.Client.TelemetryCore;
1822
using Microsoft.Identity.Client.TelemetryCore.Internal.Events;
23+
using Microsoft.Identity.Client.TelemetryCore.OpenTelemetry;
24+
using Microsoft.Identity.Client.TelemetryCore.TelemetryClient;
1925
using Microsoft.Identity.Client.Utils;
20-
using Microsoft.Identity.Client.TelemetryCore;
2126
using Microsoft.IdentityModel.Abstractions;
22-
using Microsoft.Identity.Client.TelemetryCore.TelemetryClient;
23-
using Microsoft.Identity.Client.TelemetryCore.OpenTelemetry;
24-
using Microsoft.Identity.Client.Internal.Broker;
25-
using System.Runtime.ConstrainedExecution;
26-
using Microsoft.Identity.Client.AuthScheme;
2727

2828
namespace Microsoft.Identity.Client.Internal.Requests
2929
{
@@ -73,7 +73,7 @@ public async Task<AuthenticationResult> RunAsync(CancellationToken cancellationT
7373
{
7474
ApiEvent apiEvent = null;
7575

76-
var measureTelemetryDurationResult = StopwatchService.MeasureCodeBlock(() =>
76+
MeasureDurationResult measureTelemetryDurationResult = StopwatchService.MeasureCodeBlock(() =>
7777
{
7878
apiEvent = InitializeApiEvent(AuthenticationRequestParameters.Account?.HomeAccountId?.Identifier);
7979
AuthenticationRequestParameters.RequestContext.ApiEvent = apiEvent;
@@ -82,7 +82,7 @@ public async Task<AuthenticationResult> RunAsync(CancellationToken cancellationT
8282
try
8383
{
8484
AuthenticationResult authenticationResult = null;
85-
var measureDurationResult = await StopwatchService.MeasureCodeBlockAsync(async () =>
85+
MeasureDurationResult measureDurationResult = await StopwatchService.MeasureCodeBlockAsync(async () =>
8686
{
8787
AuthenticationRequestParameters.LogParameters();
8888
LogRequestStarted(AuthenticationRequestParameters);
@@ -156,10 +156,10 @@ private Tuple<string, string> ParseScopesForTelemetry()
156156

157157
if (Uri.IsWellFormedUriString(firstScope, UriKind.Absolute))
158158
{
159-
Uri firstScopeAsUri = new Uri(firstScope);
159+
Uri firstScopeAsUri = new(firstScope);
160160
resource = $"{firstScopeAsUri.Scheme}://{firstScopeAsUri.Host}";
161161

162-
StringBuilder stringBuilder = new StringBuilder();
162+
StringBuilder stringBuilder = new();
163163

164164
foreach (string scope in AuthenticationRequestParameters.Scope)
165165
{
@@ -199,7 +199,7 @@ private static void LogMetricsFromAuthResult(AuthenticationResult authentication
199199
{
200200
if (logger.IsLoggingEnabled(LogLevel.Always))
201201
{
202-
var metadata = authenticationResult.AuthenticationResultMetadata;
202+
AuthenticationResultMetadata metadata = authenticationResult.AuthenticationResultMetadata;
203203
logger.Always(
204204
$"""
205205
@@ -237,7 +237,7 @@ protected virtual void EnrichTelemetryApiEvent(ApiEvent apiEvent)
237237
private ApiEvent InitializeApiEvent(string accountId)
238238
#pragma warning restore IDE0060
239239
{
240-
ApiEvent apiEvent = new ApiEvent(AuthenticationRequestParameters.RequestContext.CorrelationId)
240+
ApiEvent apiEvent = new(AuthenticationRequestParameters.RequestContext.CorrelationId)
241241
{
242242
ApiId = AuthenticationRequestParameters.ApiId,
243243
IsTokenCacheSerialized =
@@ -268,11 +268,9 @@ private ApiEvent InitializeApiEvent(string accountId)
268268

269269
private void UpdateCallerSdkDetails(ApiEvent apiEvent)
270270
{
271-
string callerSdkId;
272-
string callerSdkVer;
273271

274272
// Check if ExtraQueryParameters contains caller-sdk-id and caller-sdk-ver
275-
if (AuthenticationRequestParameters.ExtraQueryParameters.TryGetValue(Constants.CallerSdkIdKey, out callerSdkId))
273+
if (AuthenticationRequestParameters.ExtraQueryParameters.TryGetValue(Constants.CallerSdkIdKey, out string callerSdkId))
276274
{
277275
AuthenticationRequestParameters.ExtraQueryParameters.Remove(Constants.CallerSdkIdKey);
278276
}
@@ -281,7 +279,7 @@ private void UpdateCallerSdkDetails(ApiEvent apiEvent)
281279
callerSdkId = AuthenticationRequestParameters.RequestContext.ServiceBundle.Config.ClientName;
282280
}
283281

284-
if (AuthenticationRequestParameters.ExtraQueryParameters.TryGetValue(Constants.CallerSdkVersionKey, out callerSdkVer))
282+
if (AuthenticationRequestParameters.ExtraQueryParameters.TryGetValue(Constants.CallerSdkVersionKey, out string callerSdkVer))
285283
{
286284
AuthenticationRequestParameters.ExtraQueryParameters.Remove(Constants.CallerSdkVersionKey);
287285
}
@@ -331,7 +329,7 @@ protected async Task<AuthenticationResult> CacheTokenResponseAndCreateAuthentica
331329
AuthenticationRequestParameters.ApiId != ApiEvent.ApiIds.AcquireTokenForUserAssignedManagedIdentity &&
332330
AuthenticationRequestParameters.ApiId != ApiEvent.ApiIds.AcquireTokenByRefreshToken &&
333331
AuthenticationRequestParameters.AuthorityInfo.AuthorityType != AuthorityType.Adfs &&
334-
!(msalTokenResponse.ClientInfo is null))
332+
msalTokenResponse.ClientInfo is not null)
335333
{
336334
//client_info is not returned from managed identity flows because there is no user present.
337335
clientInfoFromServer = ClientInfo.CreateFromJson(msalTokenResponse.ClientInfo);
@@ -340,9 +338,9 @@ protected async Task<AuthenticationResult> CacheTokenResponseAndCreateAuthentica
340338

341339
AuthenticationRequestParameters.RequestContext.Logger.Info("Saving token response to cache..");
342340

343-
var tuple = await CacheManager.SaveTokenResponseAsync(msalTokenResponse).ConfigureAwait(false);
344-
var atItem = tuple.Item1;
345-
var idtItem = tuple.Item2;
341+
Tuple<MsalAccessTokenCacheItem, MsalIdTokenCacheItem, Account> tuple = await CacheManager.SaveTokenResponseAsync(msalTokenResponse).ConfigureAwait(false);
342+
MsalAccessTokenCacheItem atItem = tuple.Item1;
343+
MsalIdTokenCacheItem idtItem = tuple.Item2;
346344
Account account = tuple.Item3;
347345
#if !MOBILE
348346
atItem?.AddAdditionalCacheParameters(clientInfoFromServer?.AdditionalResponseParameters);
@@ -376,7 +374,7 @@ internal async Task<MsalTokenResponse> SendTokenRequestAsync(
376374
{
377375
var tokenEndpoint = await AuthenticationRequestParameters.Authority.GetTokenEndpointAsync(AuthenticationRequestParameters.RequestContext).ConfigureAwait(false);
378376

379-
var tokenResponse = await SendTokenRequestAsync(
377+
MsalTokenResponse tokenResponse = await SendTokenRequestAsync(
380378
tokenEndpoint,
381379
additionalBodyParameters,
382380
cancellationToken).ConfigureAwait(false);
@@ -393,7 +391,7 @@ protected Task<MsalTokenResponse> SendTokenRequestAsync(
393391
string scopes = GetOverriddenScopes(AuthenticationRequestParameters.Scope).AsSingleString();
394392
var tokenClient = new TokenClient(AuthenticationRequestParameters);
395393

396-
var CcsHeader = GetCcsHeader(additionalBodyParameters);
394+
KeyValuePair<string, string>? CcsHeader = GetCcsHeader(additionalBodyParameters);
397395
if (CcsHeader != null && !string.IsNullOrEmpty(CcsHeader.Value.Key))
398396
{
399397
tokenClient.AddHeaderToClient(CcsHeader.Value.Key, CcsHeader.Value.Value);
@@ -417,7 +415,7 @@ private void InjectPcaSsoPolicyHeader(TokenClient tokenClient)
417415
ServiceBundle.Config,
418416
AuthenticationRequestParameters.RequestContext.Logger);
419417

420-
var ssoPolicyHeaders = broker.GetSsoPolicyHeaders();
418+
IReadOnlyDictionary<string, string> ssoPolicyHeaders = broker.GetSsoPolicyHeaders();
421419
foreach (KeyValuePair<string, string> kvp in ssoPolicyHeaders)
422420
{
423421
tokenClient.AddHeaderToClient(kvp.Key, kvp.Value);
@@ -531,16 +529,16 @@ internal async Task<AuthenticationResult> HandleTokenRefreshErrorAsync(
531529
MsalAccessTokenCacheItem cachedAccessTokenItem,
532530
CancellationToken cancellationToken)
533531
{
534-
var logger = AuthenticationRequestParameters.RequestContext.Logger;
532+
ILoggerAdapter logger = AuthenticationRequestParameters.RequestContext.Logger;
535533

536534
logger.Warning($"Fetching a new AT failed. Is exception retry-able? {e.IsRetryable}. Is there an AT in the cache that is usable? {cachedAccessTokenItem != null}");
537535

538536
if (cachedAccessTokenItem != null && e.IsRetryable)
539537
{
540538
logger.Info("Returning existing access token. It is not expired, but should be refreshed. ");
541539

542-
var idToken = await CacheManager.GetIdTokenCacheItemAsync(cachedAccessTokenItem).ConfigureAwait(false);
543-
var account = await CacheManager.GetAccountAssociatedWithAccessTokenAsync(cachedAccessTokenItem).ConfigureAwait(false);
540+
MsalIdTokenCacheItem idToken = await CacheManager.GetIdTokenCacheItemAsync(cachedAccessTokenItem).ConfigureAwait(false);
541+
Account account = await CacheManager.GetAccountAssociatedWithAccessTokenAsync(cachedAccessTokenItem).ConfigureAwait(false);
544542

545543
return await AuthenticationResult.CreateAsync(
546544
cachedAccessTokenItem,

src/client/Microsoft.Identity.Client/OAuth2/TokenClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private async Task AddBodyParamsAndHeadersAsync(
142142
tokenEndpoint,
143143
cancellationToken).ConfigureAwait(false);
144144

145-
foreach (var kvp in material.TokenRequestParameters)
145+
foreach (KeyValuePair<string, string> kvp in material.TokenRequestParameters)
146146
{
147147
_oAuth2Client.AddBodyParameter(kvp.Key, kvp.Value);
148148
}

0 commit comments

Comments
 (0)