Skip to content

Commit 39b3f69

Browse files
authored
Route IWA calls to broker default account sign in if PCA uses broker option (#5341)
Route IWA calls to broker default account sign if .WithBroker is used
1 parent 0919b95 commit 39b3f69

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Microsoft.Identity.Client.ApiConfig.Parameters;
11+
using Microsoft.Identity.Client.Core;
12+
using Microsoft.Identity.Client.Internal.Requests.Silent;
1113
using Microsoft.Identity.Client.OAuth2;
1214
using Microsoft.Identity.Client.WsTrust;
1315

@@ -20,6 +22,7 @@ internal class IntegratedWindowsAuthRequest : RequestBase
2022
{
2123
private readonly CommonNonInteractiveHandler _commonNonInteractiveHandler;
2224
private readonly AcquireTokenByIntegratedWindowsAuthParameters _integratedWindowsAuthParameters;
25+
private readonly Lazy<ISilentAuthRequestStrategy> _brokerStrategyLazy;
2326

2427
public IntegratedWindowsAuthRequest(
2528
IServiceBundle serviceBundle,
@@ -31,10 +34,29 @@ public IntegratedWindowsAuthRequest(
3134
_commonNonInteractiveHandler = new CommonNonInteractiveHandler(
3235
authenticationRequestParameters.RequestContext,
3336
serviceBundle);
37+
38+
var silentParameters = new AcquireTokenSilentParameters();
39+
var silentRequest = new SilentRequest(ServiceBundle, authenticationRequestParameters, silentParameters);
40+
_brokerStrategyLazy = new Lazy<ISilentAuthRequestStrategy>(() => new BrokerSilentStrategy(silentRequest,
41+
serviceBundle,
42+
authenticationRequestParameters,
43+
silentParameters,
44+
serviceBundle.PlatformProxy.CreateBroker(
45+
serviceBundle.Config, null)));
3446
}
3547

3648
protected override async Task<AuthenticationResult> ExecuteAsync(CancellationToken cancellationToken)
3749
{
50+
bool isBrokerConfigured = AuthenticationRequestParameters.AppConfig.IsBrokerEnabled &&
51+
ServiceBundle.PlatformProxy.CanBrokerSupportSilentAuth();
52+
53+
if(isBrokerConfigured)
54+
{
55+
AuthenticationRequestParameters.RequestContext.Logger.Info("IWA called with broker. Routing to broker default user sign in");
56+
AuthenticationRequestParameters.Account = PublicClientApplication.OperatingSystemAccount;
57+
return await _brokerStrategyLazy.Value.ExecuteAsync(cancellationToken).ConfigureAwait(false);
58+
}
59+
3860
await ResolveAuthorityAsync().ConfigureAwait(false);
3961
await UpdateUsernameAsync().ConfigureAwait(false);
4062
var userAssertion = await FetchAssertionFromWsTrustAsync().ConfigureAwait(false);

tests/devapps/WAM/NetCoreWinFormsWam/Form1.Designer.cs

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/devapps/WAM/NetCoreWinFormsWam/Form1.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,27 @@ private async Task<AuthenticationResult> RunAtiSshBtnAsync(IPublicClientApplicat
938938

939939
return result;
940940
}
941+
942+
private async void btn_wia_Click(object sender, EventArgs e)
943+
{
944+
try
945+
{
946+
var cancellationTokenSource = new CancellationTokenSource();
947+
948+
var pca = await CreatePca(GetAuthMethod()).ConfigureAwait(false);
949+
AuthenticationResult authenticationResult = await pca
950+
.AcquireTokenByIntegratedWindowsAuth(
951+
GetScopes())
952+
.ExecuteAsync(cancellationTokenSource.Token)
953+
.ConfigureAwait(true);
954+
955+
await LogResultAndRefreshAccountsAsync(authenticationResult).ConfigureAwait(false);
956+
}
957+
catch (Exception ex)
958+
{
959+
Log("Exception: " + ex);
960+
}
961+
}
941962
}
942963

943964
public class ClientEntry

0 commit comments

Comments
 (0)