Skip to content

Commit 17b6b9d

Browse files
author
Lei Jin
committedMay 15, 2025
Fix null reference
1 parent 299d1d6 commit 17b6b9d

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed
 

‎src/Accounts/Accounts/Context/ClearAzureRmContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void ClearContext(AzureRmProfile profile, RMProfileClient client)
7474
var contexts = profile.Contexts.Values;
7575
foreach (var context in contexts)
7676
{
77-
tokenCacheProvider?.ClearCache(context.Environment.ActiveDirectoryAuthority);
77+
tokenCacheProvider?.ClearCache(context?.Environment?.ActiveDirectoryAuthority);
7878
client.TryRemoveContext(context);
7979
}
8080

‎src/Accounts/Accounts/Context/GetAzureRMContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ public override void ExecuteCmdlet()
8484
var defaultProfile = DefaultProfile as AzureRmProfile;
8585
if (defaultProfile != null && string.Equals(AzureSession.Instance?.ARMContextSaveMode, "CurrentUser"))
8686
{
87-
AzureSession.Instance.SetProperty(AzureSession.Property.Environment, DefaultContext.Environment.Name);
87+
if (!string.IsNullOrEmpty(DefaultContext?.Environment?.Name))
88+
{
89+
AzureSession.Instance.SetProperty(AzureSession.Property.Environment, DefaultContext.Environment.Name);
90+
}
8891
defaultProfile.RefreshContextsFromCache(_cmdletContext);
8992
}
9093
}

‎src/Accounts/Accounts/Context/RemoveAzureRmContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public override void ExecuteCmdlet()
9090
}
9191
else
9292
{
93-
if (!tokenCacheProvider.TryRemoveAccount(removedContext.Account.Id, removedContext.Environment.ActiveDirectoryAuthority))
93+
if (!tokenCacheProvider.TryRemoveAccount(removedContext.Account.Id, removedContext?.Environment?.ActiveDirectoryAuthority))
9494
{
9595
WriteWarning(string.Format(Resources.NoContextsRemain, removedContext.Account.Id));
9696
}

‎src/Accounts/Authentication/Factories/AuthenticationFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,10 +523,10 @@ public void RemoveUser(IAzureAccount account, IAzureEnvironment environment)
523523
// make best effort to remove credentials
524524
}
525525

526-
RemoveFromTokenCache(account, environment.ActiveDirectoryAuthority);
526+
RemoveFromTokenCache(account, environment?.ActiveDirectoryAuthority);
527527
break;
528528
case AzureAccount.AccountType.User:
529-
RemoveFromTokenCache(account, environment.ActiveDirectoryAuthority);
529+
RemoveFromTokenCache(account, environment?.ActiveDirectoryAuthority);
530530
break;
531531
}
532532
}

0 commit comments

Comments
 (0)