Skip to content

Commit ac90bf4

Browse files
committed
Handle null scopes in MsalAuth.cs more robustly
Ensure that the `scopes` parameter is checked for null before joining its elements into a string. This prevents potential null reference exceptions. Changes are applied when constructing the `cacheKey` and setting the "scope" parameter in the `parameters` dictionary.
1 parent a734eb5 commit ac90bf4

File tree

1 file changed

+13
-3
lines changed
  • src/libraries/Authentication/Authentication.Msal

1 file changed

+13
-3
lines changed

src/libraries/Authentication/Authentication.Msal/MsalAuth.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ public async Task<string> GetAgenticUserTokenAsync(string agentAppInstanceId, st
210210
AssertionHelpers.ThrowIfNullOrWhiteSpace(agentAppInstanceId, nameof(agentAppInstanceId));
211211
AssertionHelpers.ThrowIfNullOrWhiteSpace(upn, nameof(upn));
212212

213-
var cacheKey = $"{agentAppInstanceId}/{upn}/{string.Join(";", scopes)}";
213+
string k1 = "";
214+
if ( scopes != null )
215+
{
216+
k1 = string.Join(";", scopes);
217+
}
218+
var cacheKey = $"{agentAppInstanceId}/{upn}/{k1}";
214219
var value = _agenticTokenCache.Get(cacheKey);
215220
if (value != null)
216221
{
@@ -250,11 +255,16 @@ public async Task<string> GetAgenticUserTokenAsync(string agentAppInstanceId, st
250255
var tokenEndpoint = _connectionSettings.Authority != null
251256
? $"{_connectionSettings.Authority}/oauth2/v2.0/token"
252257
: $"https://login.microsoftonline.com/{_connectionSettings.TenantId}/oauth2/v2.0/token";
253-
258+
259+
string scp = "";
260+
if (scopes != null)
261+
{
262+
scp = string.Join(" ", scopes);
263+
}
254264
var parameters = new Dictionary<string, string>
255265
{
256266
{ "client_id", agentAppInstanceId },
257-
{ "scope", string.Join(" ", scopes) },
267+
{ "scope", scp },
258268
{ "client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" },
259269
{ "client_assertion", agentToken },
260270
{ "username", upn },

0 commit comments

Comments
 (0)