Skip to content

Commit 5549234

Browse files
isra-felYeming Liu
andauthored
Can clear token cache when wam is enabled (#28289)
Co-authored-by: Yeming Liu <[email protected]>
1 parent 7005c71 commit 5549234

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Fixed an issue where `Clear-AzContext` does not clear the token cache when broker is enabled.
2223
* Added new parameter `-ClaimsChallenge` to `Connect-AzAccount` to support claims challenge authentication for MFA.
2324
* Refined the error message when a cmdlet fails because of policy violations about Multi-Factor Authentication (MFA) to provide more actionable guidance.
2425

src/Accounts/Accounts/Context/ClearAzureRmContext.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ void ClearContext(AzureRmProfile profile, RMProfileClient client)
6565
bool result = false;
6666
if (profile != null)
6767
{
68+
string authorityHost = profile.DefaultContext?.Environment?.ActiveDirectoryAuthority;
69+
string tenantId = profile.DefaultContext?.Tenant?.Id;
70+
6871
var contexts = profile.Contexts.Values;
6972
foreach (var context in contexts)
7073
{
@@ -78,11 +81,19 @@ void ClearContext(AzureRmProfile profile, RMProfileClient client)
7881
}
7982
else
8083
{
81-
tokenCacheProvider.ClearCache();
84+
string authority = null;
85+
if (authorityHost != null)
86+
{
87+
authority = $"{authorityHost}/{tenantId}";
88+
}
89+
WriteDebug($"Clearing token cache for authority: {authority}");
90+
tokenCacheProvider.ClearCache(authority);
8291
var defaultContext = new AzureContext();
8392
profile.TrySetDefaultContext(defaultContext);
8493
result = true;
8594
}
95+
96+
8697
if (AzureSession.Instance.TryGetComponent(AzKeyStore.Name, out AzKeyStore keyStore))
8798
{
8899
keyStore?.Clear();

src/Accounts/Authentication/Authentication/TokenCache/PowerShellTokenCacheProvider.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Extensions;
2525
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces;
2626
using Microsoft.Azure.Commands.Common.Authentication.Utilities;
27-
using Microsoft.Azure.Commands.Shared.Config;
2827
using Microsoft.Azure.Internal.Subscriptions;
2928
using Microsoft.Azure.Internal.Subscriptions.Models;
30-
using Microsoft.Azure.PowerShell.Common.Config;
3129
using Microsoft.Identity.Client;
3230
using Microsoft.Identity.Client.Broker;
3331

@@ -59,9 +57,14 @@ public virtual void ClearCache()
5957
{
6058
}
6159

60+
public virtual void ClearCache(string authority)
61+
{
62+
ClearCache();
63+
}
64+
6265
public bool TryRemoveAccount(string accountId)
6366
{
64-
TracingAdapter.Information(string.Format("[AuthenticationClientFactory] Calling GetAccountsAsync"));
67+
TracingAdapter.Information(string.Format("[AuthenticationClientFactory] Calling TryRemoveAccount"));
6568
var client = CreatePublicClient();
6669
var account = client.GetAccountsAsync()
6770
.ConfigureAwait(false).GetAwaiter().GetResult()
@@ -73,7 +76,7 @@ public bool TryRemoveAccount(string accountId)
7376

7477
try
7578
{
76-
TracingAdapter.Information(string.Format("[AuthenticationClientFactory] Calling RemoveAsync - Account: '{0}'", account.Username));
79+
TracingAdapter.Information(string.Format("[AuthenticationClientFactory] Calling TryRemoveAccount - Account: '{0}'", account.Username));
7780
client.RemoveAsync(account)
7881
.ConfigureAwait(false).GetAwaiter().GetResult();
7982
}

src/Accounts/Authentication/Authentication/TokenCache/SharedTokenCacheProvider.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
16-
1715
using Azure.Identity;
18-
16+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1917
using Microsoft.Identity.Client;
2018
using Microsoft.Identity.Client.Extensions.Msal;
19+
using System;
2120

2221
namespace Microsoft.Azure.Commands.Common.Authentication
2322
{
@@ -105,7 +104,17 @@ protected override void RegisterCache(IPublicClientApplication client)
105104

106105
public override void ClearCache()
107106
{
108-
var client = CreatePublicClient();
107+
ClearCacheInternal(null);
108+
}
109+
110+
public override void ClearCache(string authority)
111+
{
112+
ClearCacheInternal(authority);
113+
}
114+
115+
private void ClearCacheInternal(string authority)
116+
{
117+
var client = CreatePublicClient(authority);
109118
var accounts = client.GetAccountsAsync().GetAwaiter().GetResult();
110119
foreach (var account in accounts)
111120
{

src/Accounts/Authentication/Utilities/AzConfigReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static public bool IsWamEnabled(string authority)
6363
{
6464
authority = authority + "/";
6565
}
66-
return Instance.GetConfigValue<bool>(ConfigKeys.EnableLoginByWam) && 0 == string.Compare(authority, AzureAuthorityHosts.AzurePublicCloud.OriginalString, System.StringComparison.OrdinalIgnoreCase);
66+
return Instance.GetConfigValue<bool>(ConfigKeys.EnableLoginByWam) && authority.StartsWith(AzureAuthorityHosts.AzurePublicCloud.OriginalString, System.StringComparison.OrdinalIgnoreCase);
6767
}
6868
catch
6969
{

0 commit comments

Comments
 (0)