Skip to content

Commit b3c26e5

Browse files
committed
Ensure that TokenCache is transmitted to context when context is set
1 parent 59f1a01 commit b3c26e5

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

src/ResourceManager/Common/Commands.ResourceManager.Common/AzureRMProfileExtensions.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
using System.Linq;
1818
using System.Text;
1919
using System.Threading.Tasks;
20+
using Microsoft.Azure.Commands.ResourceManager.Common.Properties;
2021
using Microsoft.Azure.Common.Authentication.Models;
22+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2123

2224
namespace Microsoft.Azure.Commands.ResourceManager.Common
2325
{
@@ -27,15 +29,20 @@ public static class AzureRMProfileExtensions
2729
/// Set the context for the current profile, preserving token cache information
2830
/// </summary>
2931
/// <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+
/// <param name="newContext">The new context, with no token cache information.</param>
33+
public static void SetContextWithCache(this AzureRMProfile profile, AzureContext newContext)
3234
{
33-
var currentContext = profile.Context;
34-
if (currentContext != null && currentContext.TokenCache != null && currentContext.TokenCache.Length > 0)
35+
if (profile == null)
3536
{
36-
newContext.TokenCache = currentContext.TokenCache;
37+
throw new ArgumentNullException("profile", Resources.ProfileCannotBeNull);
3738
}
3839

40+
if (newContext == null)
41+
{
42+
throw new ArgumentNullException("newContext", Resources.ContextCannotBeNull);
43+
}
44+
45+
newContext.TokenCache = TokenCache.DefaultShared.Serialize();
3946
profile.Context = newContext;
4047
}
4148
}

src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.Designer.cs

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

src/ResourceManager/Common/Commands.ResourceManager.Common/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="ContextCannotBeNull" xml:space="preserve">
121+
<value>Context cannot be null. Please log in using Add-AzureRmAccount.</value>
122+
</data>
120123
<data name="DataCollectionActivity" xml:space="preserve">
121124
<value>Microsoft Azure PowerShell Data Collection Confirmation</value>
122125
</data>
@@ -151,6 +154,9 @@ Select Y to enable data collection [Y/N]:</value>
151154
<data name="NoSubscriptionFound" xml:space="preserve">
152155
<value>The provided account {0} does not have access to any subscriptions. Please try logging in with different credentials.</value>
153156
</data>
157+
<data name="ProfileCannotBeNull" xml:space="preserve">
158+
<value>Profile cannot be null. Please run Add-AzureRmAccount.</value>
159+
</data>
154160
<data name="ResourceProviderRegisterAttempt" xml:space="preserve">
155161
<value>Attempting to register resource provider '{0}'</value>
156162
</data>

src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System;
2020
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
2121
using System.Collections.Generic;
22+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2223
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2324

2425
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
@@ -131,5 +132,18 @@ public void NoSubscriptionsInListThrows()
131132
AzureSubscription subValue;
132133
Assert.False(client.TryGetSubscription(DefaultTenant.ToString(), DefaultSubscription.ToString(), out subValue));
133134
}
135+
136+
[Fact]
137+
[Trait(Category.AcceptanceType, Category.CheckIn)]
138+
public void SetContextPreservesTokenCache()
139+
{
140+
AzureRMProfile profile = null;
141+
AzureContext context = new AzureContext(null, null, null, null);
142+
Assert.Throws<ArgumentNullException>(() =>profile.SetContextWithCache(context));
143+
profile = new AzureRMProfile();
144+
Assert.Throws<ArgumentNullException>(() => profile.SetContextWithCache(null));
145+
profile.SetContextWithCache(context);
146+
Assert.Equal(TokenCache.DefaultShared.Serialize(), profile.Context.TokenCache);
147+
}
134148
}
135149
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected override void ProcessRecord()
5555
{
5656
if (ParameterSetName == ContextParameterSet)
5757
{
58-
AzureRmProfileProvider.Instance.Profile.SetContext(new AzureContext(Context.Subscription, Context.Account,
58+
AzureRmProfileProvider.Instance.Profile.SetContextWithCache(new AzureContext(Context.Subscription, Context.Account,
5959
Context.Environment, Context.Tenant));
6060
}
6161
else

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
105105
{
106106
if (!string.IsNullOrWhiteSpace(tenantId))
107107
{
108-
_profile.SetContext(new AzureContext(
108+
_profile.SetContextWithCache(new AzureContext(
109109
_profile.Context.Subscription,
110110
_profile.Context.Account,
111111
_profile.Context.Environment,
@@ -139,7 +139,7 @@ public AzureContext SetCurrentContext(string subscriptionId, string tenantId)
139139
newSubscription.Name = null;
140140
}
141141

142-
_profile.SetContext(new AzureContext(
142+
_profile.SetContextWithCache(new AzureContext(
143143
newSubscription,
144144
_profile.Context.Account,
145145
_profile.Context.Environment,

0 commit comments

Comments
 (0)