Skip to content

Commit 00812f1

Browse files
erich-wangwyunchi-ms
authored andcommitted
Fallback to first available context as DefaultContext if current default context key is 'Default' (#14281)
* Fallback to first available context as DefaultContext if current default context key is 'Default' * address comment
1 parent 11e4c96 commit 00812f1

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
* Overview of change #1
1818
- Additional information about change #1
1919
-->
20+
2021
## Upcoming Release
22+
* Fallback to first valid context if current default context key is "Default" which is invalid
2123

2224
## Version 2.2.7
2325
* Fixed incorrect warning message on Windows PowerShell [#14556]

src/Accounts/Authentication.ResourceManager/AzureRmProfile.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ public virtual IAzureContext DefaultContext
7171
}
7272

7373
IAzureContext result = null;
74-
if (DefaultContextKey == "Default" && Contexts.Any(c => c.Key != "Default"))
74+
if (DefaultContextKey == Constants.DefaultValue && Contexts.Any(c => c.Key != Constants.DefaultValue))
7575
{
76-
// If the default context is "Default", but there are other contexts set, remove the "Default" context to throw the below exception
77-
TryCacheRemoveContext("Default");
76+
// If the default context is "Default", but there are other contexts set, remove the "Default" context and select first avaiable context as default
77+
EnqueueDebugMessage($"Incorrect default context key '{DefaultContextKey}' found. Trying to remove it and falling back to the first available context.");
78+
TryRemoveContext(Constants.DefaultValue);
7879
}
7980

8081
if (!string.IsNullOrEmpty(DefaultContextKey) && Contexts != null && Contexts.ContainsKey(DefaultContextKey))
@@ -292,6 +293,7 @@ public void Save(IFileProvider provider, bool serializeCache = true)
292293

293294
try
294295
{
296+
TryRemoveContext(Constants.DefaultValue);
295297
string contents = ToString(serializeCache);
296298
string diskContents = string.Empty;
297299
diskContents = provider.CreateReader().ReadToEnd();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Common.Authentication.ResourceManager
16+
{
17+
internal static class Constants
18+
{
19+
public const string DefaultValue = "Default";
20+
}
21+
}

0 commit comments

Comments
 (0)