Skip to content

Commit 59f1a01

Browse files
committed
Fix issue with context changes not copying the token cache
1 parent a661f2b commit 59f1a01

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

src/Common/Commands.Common/GeneralUtilities.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,5 @@ public static void ClearCurrentStorageAccount(bool clearSMContext = false)
442442
}
443443
}
444444
}
445-
446445
}
447446
}

src/ResourceManager/Common/Commands.ResourceManager.Common/AccessTokenExtensions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
using System;
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+
using System;
216
using System.Linq;
317
using Microsoft.Azure.Common.Authentication;
418

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
using System.Text;
19+
using System.Threading.Tasks;
20+
using Microsoft.Azure.Common.Authentication.Models;
21+
22+
namespace Microsoft.Azure.Commands.ResourceManager.Common
23+
{
24+
public static class AzureRMProfileExtensions
25+
{
26+
/// <summary>
27+
/// Set the context for the current profile, preserving token cache information
28+
/// </summary>
29+
/// <param name="profile">The profile to change the context for</param>
30+
/// <param name="newContext">The new context</param>
31+
public static void SetContext(this AzureRMProfile profile, AzureContext newContext)
32+
{
33+
var currentContext = profile.Context;
34+
if (currentContext != null && currentContext.TokenCache != null && currentContext.TokenCache.Length > 0)
35+
{
36+
newContext.TokenCache = currentContext.TokenCache;
37+
}
38+
39+
profile.Context = newContext;
40+
}
41+
}
42+
}

src/ResourceManager/Common/Commands.ResourceManager.Common/Commands.ResourceManager.Common.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<ItemGroup>
139139
<Compile Include="AccessTokenExtensions.cs" />
140140
<Compile Include="AzureRmCmdlet.cs" />
141+
<Compile Include="AzureRMProfileExtensions.cs" />
141142
<Compile Include="Generated\AuthorizationClient.cs" />
142143
<Compile Include="Generated\AuthorizationClientExtensions.cs" />
143144
<Compile Include="Generated\DeploymentOperationOperations.cs" />

src/ResourceManager/Profile/Commands.Profile/Context/SetAzureRMContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ protected override void ProcessRecord()
5555
{
5656
if (ParameterSetName == ContextParameterSet)
5757
{
58-
AzureRmProfileProvider.Instance.Profile.Context = new AzureContext(Context.Subscription, Context.Account,
59-
Context.Environment, Context.Tenant);
58+
AzureRmProfileProvider.Instance.Profile.SetContext(new AzureContext(Context.Subscription, Context.Account,
59+
Context.Environment, Context.Tenant));
6060
}
6161
else
6262
{

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,11 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
105105
{
106106
if (!string.IsNullOrWhiteSpace(tenantId))
107107
{
108-
_profile.Context = new AzureContext(
108+
_profile.SetContext(new AzureContext(
109109
_profile.Context.Subscription,
110110
_profile.Context.Account,
111111
_profile.Context.Environment,
112-
new AzureTenant() { Id = new Guid(tenantId) });
112+
new AzureTenant() { Id = new Guid(tenantId) }));
113113

114114
if (_profile.Context.Account != null)
115115
{
@@ -139,11 +139,11 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
139139
newSubscription.Name = null;
140140
}
141141

142-
_profile.Context = new AzureContext(
142+
_profile.SetContext(new AzureContext(
143143
newSubscription,
144144
_profile.Context.Account,
145145
_profile.Context.Environment,
146-
_profile.Context.Tenant);
146+
_profile.Context.Tenant));
147147
}
148148

149149
return _profile.Context;

0 commit comments

Comments
 (0)