Skip to content

Commit 05455a5

Browse files
authored
Fix for #4860 - ignore tenant id for authorities that do not support … (#5027)
Fix for #4860 - ignore tenant id for authorities that do not support tenants
1 parent 2ad7d8d commit 05455a5

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

src/client/Microsoft.Identity.Client/ApiConfig/AbstractAcquireTokenParameterBuilder.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,13 @@ public T WithTenantIdFromAuthority(Uri authorityUri)
296296
}
297297

298298
var authorityInfo = AuthorityInfo.FromAuthorityUri(authorityUri.ToString(), false);
299-
var authority = Authority.CreateAuthority(authorityInfo);
300-
return WithTenantId(authority.TenantId);
299+
if (authorityInfo.CanBeTenanted)
300+
{
301+
var authority = Authority.CreateAuthority(authorityInfo);
302+
return WithTenantId(authority.TenantId);
303+
}
304+
305+
return this as T;
301306
}
302307

303308
/// <summary>

tests/Microsoft.Identity.Test.Integration.netcore/HeadlessTests/UsernamePasswordIntegrationTests.NetFwk.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,14 @@ public async Task AcquireTokenFromAdfsUsernamePasswordAsync()
110110
LabResponse labResponse = await LabUserHelper.GetAdfsUserAsync(FederationProvider.ADFSv2019, true).ConfigureAwait(false);
111111

112112
var user = labResponse.User;
113-
113+
Uri authorityUri = new Uri(Adfs2019LabConstants.Authority);
114+
114115
var msalPublicClient = PublicClientApplicationBuilder
115116
.Create(Adfs2019LabConstants.PublicClientId)
116-
.WithAdfsAuthority(Adfs2019LabConstants.Authority)
117+
.WithAuthority(authorityUri)
117118
.WithTestLogging()
118119
.Build();
120+
119121
AuthenticationResult authResult = await msalPublicClient
120122
.AcquireTokenByUsernamePassword(s_scopes, user.Upn, user.GetOrFetchPassword())
121123
.ExecuteAsync()

tests/Microsoft.Identity.Test.Unit/PublicApiTests/AdfsAcceptanceTests.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.Net.Http;
56
using System.Threading.Tasks;
67
using Microsoft.Identity.Client;
@@ -12,6 +13,8 @@ namespace Microsoft.Identity.Test.Unit.PublicApiTests
1213
[TestClass]
1314
public class AdfsAcceptanceTests : TestBase
1415
{
16+
private const string AdfsScope = "https://arm.asz/.default";
17+
1518
// Possible authorities copied from: https://msazure.visualstudio.com/One/_search?action=contents&text=CanAcquireToken_UsingRefreshToken&type=code&lp=code-Project&filters=ProjectFilters%7BOne%7DRepositoryFilters%7BAzureStack-Services-Graph%7D&pageSize=25&result=DefaultCollection/One/AzureStack-Services-Graph/GBmain//src/Identity.Web.Tests/MsalTests.cs
1619
[DataTestMethod]
1720
[DataRow("https://localhost:3001/adfs")]
@@ -31,6 +34,37 @@ public async Task AdfsAuthorityVariants_WithAuthority_Async(string authority)
3134
await RunAuthCodeFlowAsync(authority, useWithAdfsAuthority: false).ConfigureAwait(false);
3235
}
3336

37+
[TestMethod]
38+
public async Task AcquireTokenByUsernamePasswordAsync()
39+
{
40+
Uri authorityUri = new Uri("https://localhost:3001/adfs");
41+
using (var httpManager = new MockHttpManager())
42+
{
43+
var builder = PublicClientApplicationBuilder
44+
.Create(TestConstants.ClientId)
45+
.WithAuthority(authorityUri)
46+
.WithHttpManager(httpManager)
47+
.WithInstanceDiscovery(false)
48+
.WithRedirectUri(TestConstants.RedirectUri);
49+
50+
var app = builder.Build();
51+
52+
AddAdfsWithTenantIdMockHandler(httpManager);
53+
54+
var result = await app.AcquireTokenByUsernamePassword(
55+
TestConstants.s_scope,
56+
TestConstants.Username,
57+
TestConstants.DefaultPassword)
58+
.WithTenantIdFromAuthority(authorityUri)
59+
.ExecuteAsync()
60+
.ConfigureAwait(false);
61+
62+
var account = await app.GetAccountAsync(result.Account.HomeAccountId.Identifier).ConfigureAwait(false);
63+
64+
AssertAdfsResult(result, account);
65+
}
66+
}
67+
3468
private static async Task RunAuthCodeFlowAsync(string authority, bool useWithAdfsAuthority)
3569
{
3670
using (var httpManager = new MockHttpManager())
@@ -52,15 +86,15 @@ private static async Task RunAuthCodeFlowAsync(string authority, bool useWithAdf
5286

5387
AddAdfsWithTenantIdMockHandler(httpManager);
5488

55-
var result = await app.AcquireTokenByAuthorizationCode(new[] { "https://arm.asz/.default" }, "authcode")
89+
var result = await app.AcquireTokenByAuthorizationCode(new[] { AdfsScope }, "authcode")
5690
.ExecuteAsync()
5791
.ConfigureAwait(false);
5892

5993
var account = await app.GetAccountAsync(result.Account.HomeAccountId.Identifier).ConfigureAwait(false);
6094

6195
AssertAdfsResult(result, account);
6296

63-
var result2 = await app.AcquireTokenSilent(new[] { "https://arm.asz/.default" }, account)
97+
var result2 = await app.AcquireTokenSilent(new[] { AdfsScope }, account)
6498
.ExecuteAsync()
6599
.ConfigureAwait(false);
66100

0 commit comments

Comments
 (0)