Skip to content

Commit 588dd67

Browse files
authored
Fix tenantId selection in MSAL (Azure#52613)
1 parent ef1510a commit 588dd67

File tree

4 files changed

+12
-35
lines changed

4 files changed

+12
-35
lines changed

sdk/identity/Azure.Identity/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- TenantId is now configured via MSAL's `WithTenantId` instead of `WithTenantIdFromAuthority` to prevent malformed Uris to the authority.
12+
1113
### Other Changes
1214

1315
## 1.16.0 (2025-09-09)

sdk/identity/Azure.Identity/src/MsalClientBase.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,5 @@ await _clientWithCaeAsyncLock.GetLockOrValueAsync(true, default).ConfigureAwait(
108108

109109
return asyncLock.HasValue ? asyncLock.Value.Cache : null;
110110
}
111-
112-
public UriBuilder BuildTenantIdWithAuthorityHost(string tenantId)
113-
{
114-
UriBuilder uriBuilder = new(AuthorityHost);
115-
if (uriBuilder.Path.EndsWith("/"))
116-
{
117-
uriBuilder.Path += tenantId;
118-
}
119-
else
120-
{
121-
uriBuilder.Path = uriBuilder.Path + "/" + tenantId;
122-
}
123-
124-
return uriBuilder;
125-
}
126111
}
127112
}

sdk/identity/Azure.Identity/src/MsalConfidentialClient.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ public virtual async ValueTask<AuthenticationResult> AcquireTokenForClientCoreAs
169169

170170
if (!string.IsNullOrEmpty(tenantId))
171171
{
172-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(tenantId);
173-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
172+
builder.WithTenantId(tenantId);
174173
}
175174
if (!string.IsNullOrEmpty(claims))
176175
{
@@ -211,8 +210,7 @@ public virtual async ValueTask<AuthenticationResult> AcquireTokenSilentCoreAsync
211210
var builder = client.AcquireTokenSilent(scopes, account);
212211
if (!string.IsNullOrEmpty(tenantId))
213212
{
214-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(tenantId);
215-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
213+
builder.WithTenantId(tenantId);
216214
}
217215
if (!string.IsNullOrEmpty(claims))
218216
{
@@ -254,8 +252,7 @@ public virtual async ValueTask<AuthenticationResult> AcquireTokenByAuthorization
254252

255253
if (!string.IsNullOrEmpty(tenantId))
256254
{
257-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(tenantId);
258-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
255+
builder.WithTenantId(tenantId);
259256
}
260257
if (!string.IsNullOrEmpty(claims))
261258
{
@@ -297,8 +294,7 @@ public virtual async ValueTask<AuthenticationResult> AcquireTokenOnBehalfOfCoreA
297294

298295
if (!string.IsNullOrEmpty(tenantId))
299296
{
300-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(tenantId);
301-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
297+
builder.WithTenantId(tenantId);
302298
}
303299
if (!string.IsNullOrEmpty(claims))
304300
{

sdk/identity/Azure.Identity/src/MsalPublicClient.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenSilentCoreAs
126126
}
127127
if (tenantId != null)
128128
{
129-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(tenantId);
130-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
129+
builder.WithTenantId(tenantId);
131130
}
132131

133132
if (context.IsProofOfPossessionEnabled)
@@ -182,8 +181,7 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenSilentCoreAs
182181

183182
if (tenantId != null || record.TenantId != null)
184183
{
185-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(tenantId ?? record.TenantId);
186-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
184+
builder.WithTenantId(tenantId ?? record.TenantId);
187185
}
188186

189187
if (!string.IsNullOrEmpty(claims))
@@ -284,8 +282,7 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenInteractiveC
284282
}
285283
if (tenantId != null)
286284
{
287-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(tenantId);
288-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
285+
builder.WithTenantId(tenantId);
289286
}
290287
if (browserOptions != null)
291288
{
@@ -328,8 +325,7 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenByUsernamePa
328325
}
329326
if (!string.IsNullOrEmpty(tenantId))
330327
{
331-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(tenantId);
332-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
328+
builder.WithTenantId(tenantId);
333329
}
334330
return await builder.ExecuteAsync(async, cancellationToken)
335331
.ConfigureAwait(false);
@@ -353,8 +349,7 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenWithDeviceCo
353349
}
354350
if (!string.IsNullOrEmpty(TenantId))
355351
{
356-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(TenantId);
357-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
352+
builder.WithTenantId(TenantId);
358353
}
359354

360355
return await builder.ExecuteAsync(async, cancellationToken)
@@ -380,8 +375,7 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenByRefreshTok
380375

381376
if (!string.IsNullOrEmpty(TenantId))
382377
{
383-
UriBuilder uriBuilder = BuildTenantIdWithAuthorityHost(TenantId);
384-
builder.WithTenantIdFromAuthority(uriBuilder.Uri);
378+
builder.WithTenantId(TenantId);
385379
}
386380

387381
return await builder.ExecuteAsync(async, cancellationToken)

0 commit comments

Comments
 (0)