15
15
using Microsoft . Azure . Commands . Common . Authentication ;
16
16
using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
17
17
using Microsoft . Azure . Commands . Common . Authentication . Models ;
18
+ using Microsoft . Azure . Commands . Common . Authentication . ResourceManager ;
18
19
using Microsoft . Azure . Commands . Profile . Common ;
19
20
using Microsoft . Azure . Commands . Profile . Models ;
20
21
// TODO: Remove IfDef
21
22
#if NETSTANDARD
23
+ using Microsoft . Azure . Commands . Common . Authentication . Core ;
22
24
using Microsoft . Azure . Commands . Profile . Models . Core ;
23
25
#endif
24
26
using Microsoft . Azure . Commands . Profile . Properties ;
25
27
using System ;
28
+ using System . Linq ;
26
29
using System . Management . Automation ;
27
30
28
31
namespace Microsoft . Azure . Commands . Profile
@@ -50,6 +53,55 @@ protected override void BeginProcessing()
50
53
// Do not access the DefaultContext when loading a profile
51
54
}
52
55
56
+ void CopyProfile ( AzureRmProfile source , IProfileOperations target )
57
+ {
58
+ if ( source == null || target == null )
59
+ {
60
+ return ;
61
+ }
62
+
63
+ foreach ( var environment in source . Environments )
64
+ {
65
+ IAzureEnvironment merged ;
66
+ target . TrySetEnvironment ( environment , out merged ) ;
67
+ }
68
+
69
+ foreach ( var context in source . Contexts )
70
+ {
71
+ target . TrySetContext ( context . Key , context . Value ) ;
72
+ }
73
+
74
+ if ( ! string . IsNullOrWhiteSpace ( source . DefaultContextKey ) )
75
+ {
76
+ target . TrySetDefaultContext ( source . DefaultContextKey ) ;
77
+ }
78
+
79
+ AzureRmProfileProvider . Instance . SetTokenCacheForProfile ( target . ToProfile ( ) ) ;
80
+ EnsureProtectedCache ( target , source . DefaultContext ? . TokenCache ? . CacheData ) ;
81
+ }
82
+
83
+ void EnsureProtectedCache ( IProfileOperations profile , byte [ ] cacheData )
84
+ {
85
+ if ( profile == null || cacheData == null )
86
+ {
87
+ return ;
88
+ }
89
+
90
+ AzureRmAutosaveProfile autosave = profile as AzureRmAutosaveProfile ;
91
+ var protectedcache = AzureSession . Instance . TokenCache as ProtectedFileTokenCache ;
92
+ if ( autosave != null && protectedcache == null && cacheData . Any ( ) )
93
+ {
94
+ try
95
+ {
96
+ var cache = new ProtectedFileTokenCache ( cacheData , AzureSession . Instance . DataStore ) ;
97
+ }
98
+ catch
99
+ {
100
+ WriteWarning ( Resources . ImportAuthenticationFailure ) ;
101
+ }
102
+ }
103
+ }
104
+
53
105
public override void ExecuteCmdlet ( )
54
106
{
55
107
bool executionComplete = false ;
@@ -65,11 +117,9 @@ public override void ExecuteCmdlet()
65
117
Path ) ) ;
66
118
}
67
119
68
- ModifyContext ( ( profile , client ) =>
120
+ ModifyProfile ( ( profile ) =>
69
121
{
70
- var newProfile = new AzureRmProfile ( Path ) ;
71
- profile . TryCopyProfile ( newProfile ) ;
72
- AzureRmProfileProvider . Instance . SetTokenCacheForProfile ( newProfile ) ;
122
+ CopyProfile ( new AzureRmProfile ( Path ) , profile ) ;
73
123
executionComplete = true ;
74
124
} ) ;
75
125
} ) ;
@@ -78,10 +128,9 @@ public override void ExecuteCmdlet()
78
128
{
79
129
ConfirmAction ( Resources . ProcessImportContextFromObject , Resources . ImportContextTarget , ( ) =>
80
130
{
81
- ModifyContext ( ( profile , client ) =>
131
+ ModifyProfile ( ( profile ) =>
82
132
{
83
- profile . TryCopyProfile ( AzureContext ) ;
84
- AzureRmProfileProvider . Instance . SetTokenCacheForProfile ( AzureContext ) ;
133
+ CopyProfile ( AzureContext , profile ) ;
85
134
executionComplete = true ;
86
135
} ) ;
87
136
} ) ;
0 commit comments