4
4
using System . IdentityModel . Tokens . Jwt ;
5
5
using System . Linq ;
6
6
using System . Text . Json ;
7
+ using System . Text . Json . Nodes ;
7
8
using Microsoft . Extensions . Configuration ;
8
9
using Microsoft . Extensions . Configuration . UserSecrets ;
9
10
using Microsoft . Extensions . Tools . Internal ;
@@ -84,35 +85,36 @@ public static byte[] CreateSigningKeyMaterial(string userSecretsId, bool reset =
84
85
var secretsFilePath = PathHelper . GetSecretsPathFromSecretsId ( userSecretsId ) ;
85
86
Directory . CreateDirectory ( Path . GetDirectoryName ( secretsFilePath ) ) ;
86
87
87
- IDictionary < string , string > secrets = null ;
88
+ JsonObject secrets = null ;
88
89
if ( File . Exists ( secretsFilePath ) )
89
90
{
90
91
using var secretsFileStream = new FileStream ( secretsFilePath , FileMode . Open , FileAccess . Read ) ;
91
92
if ( secretsFileStream . Length > 0 )
92
93
{
93
- secrets = JsonSerializer . Deserialize < IDictionary < string , string > > ( secretsFileStream ) ?? new Dictionary < string , string > ( ) ;
94
+ secrets = JsonSerializer . Deserialize < JsonObject > ( secretsFileStream ) ;
94
95
}
95
96
}
96
97
97
- secrets ??= new Dictionary < string , string > ( ) ;
98
+ secrets ??= new JsonObject ( ) ;
98
99
99
100
if ( reset && secrets . ContainsKey ( DevJwtsDefaults . SigningKeyConfigurationKey ) )
100
101
{
101
102
secrets . Remove ( DevJwtsDefaults . SigningKeyConfigurationKey ) ;
102
103
}
103
- secrets . Add ( DevJwtsDefaults . SigningKeyConfigurationKey , Convert . ToBase64String ( newKeyMaterial ) ) ;
104
+ secrets . Add ( DevJwtsDefaults . SigningKeyConfigurationKey , JsonValue . Create ( Convert . ToBase64String ( newKeyMaterial ) ) ) ;
104
105
105
106
using var secretsWriteStream = new FileStream ( secretsFilePath , FileMode . Create , FileAccess . Write ) ;
106
107
JsonSerializer . Serialize ( secretsWriteStream , secrets ) ;
107
108
108
109
return newKeyMaterial ;
109
110
}
110
111
111
- public static string [ ] GetAudienceCandidatesFromLaunchSettings ( string project )
112
+ public static List < string > GetAudienceCandidatesFromLaunchSettings ( string project )
112
113
{
113
114
ArgumentException . ThrowIfNullOrEmpty ( nameof ( project ) ) ;
114
115
115
116
var launchSettingsFilePath = Path . Combine ( Path . GetDirectoryName ( project ) ! , "Properties" , "launchSettings.json" ) ;
117
+ var applicationUrls = new List < string > ( ) ;
116
118
if ( File . Exists ( launchSettingsFilePath ) )
117
119
{
118
120
using var launchSettingsFileStream = new FileStream ( launchSettingsFilePath , FileMode . Open , FileAccess . Read ) ;
@@ -124,26 +126,65 @@ public static string[] GetAudienceCandidatesFromLaunchSettings(string project)
124
126
var profilesEnumerator = profiles . EnumerateObject ( ) ;
125
127
foreach ( var profile in profilesEnumerator )
126
128
{
127
- if ( profile . Value . TryGetProperty ( "commandName" , out var commandName ) )
129
+ if ( ExtractKestrelUrlsFromProfile ( profile ) is { } kestrelUrls )
128
130
{
129
- if ( commandName . ValueEquals ( "Project" ) )
130
- {
131
- if ( profile . Value . TryGetProperty ( "applicationUrl" , out var applicationUrl ) )
132
- {
133
- var value = applicationUrl . GetString ( ) ;
134
- if ( value is { } applicationUrls )
135
- {
136
- return applicationUrls . Split ( ';' ) ;
137
- }
138
- }
139
- }
131
+ applicationUrls . AddRange ( kestrelUrls ) ;
132
+ }
133
+
134
+ if ( ExtractIISExpressUrlFromProfile ( profile ) is { } iisUrls )
135
+ {
136
+ applicationUrls . AddRange ( iisUrls ) ;
140
137
}
141
138
}
142
139
}
143
140
}
144
141
}
145
142
146
- return null ;
143
+ return applicationUrls ;
144
+
145
+ static List < string > ExtractIISExpressUrlFromProfile ( JsonProperty profile )
146
+ {
147
+ if ( profile . NameEquals ( "iisSettings" ) )
148
+ {
149
+ if ( profile . Value . TryGetProperty ( "iisExpress" , out var iisExpress ) )
150
+ {
151
+ List < string > iisUrls = new ( ) ;
152
+ if ( iisExpress . TryGetProperty ( "applicationUrl" , out var iisUrl ) )
153
+ {
154
+ iisUrls . Add ( iisUrl . GetString ( ) ) ;
155
+ }
156
+
157
+ if ( iisExpress . TryGetProperty ( "sslPort" , out var sslPort ) )
158
+ {
159
+ iisUrls . Add ( $ "https://localhost:{ sslPort . GetInt32 ( ) } ") ;
160
+ }
161
+
162
+ return iisUrls ;
163
+ }
164
+ }
165
+
166
+ return null ;
167
+ }
168
+
169
+ static string [ ] ExtractKestrelUrlsFromProfile ( JsonProperty profile )
170
+ {
171
+ if ( profile . Value . TryGetProperty ( "commandName" , out var commandName ) )
172
+ {
173
+ if ( commandName . ValueEquals ( "Project" ) )
174
+ {
175
+ if ( profile . Value . TryGetProperty ( "applicationUrl" , out var applicationUrl ) )
176
+ {
177
+ var value = applicationUrl . GetString ( ) ;
178
+ if ( value is { } urls )
179
+ {
180
+ return urls . Split ( ';' ) ;
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+ return null ;
187
+ }
147
188
}
148
189
149
190
public static void PrintJwt ( IReporter reporter , Jwt jwt , bool showAll , JwtSecurityToken fullToken = null )
@@ -163,12 +204,12 @@ public static void PrintJwt(IReporter reporter, Jwt jwt, bool showAll, JwtSecuri
163
204
: string . Join ( ", " , jwt . Scopes ) ;
164
205
reporter . Output ( $ "{ Resources . JwtPrint_Scopes } : { scopesValue } ") ;
165
206
}
166
-
207
+
167
208
if ( ! jwt . Roles . IsNullOrEmpty ( ) || showAll )
168
209
{
169
210
var rolesValue = jwt . Roles . IsNullOrEmpty ( )
170
211
? "none"
171
- : String . Join ( ", " , jwt . Roles ) ;
212
+ : string . Join ( ", " , jwt . Roles ) ;
172
213
reporter . Output ( $ "{ Resources . JwtPrint_Roles } : [{ rolesValue } ]") ;
173
214
}
174
215
0 commit comments