44using System . IdentityModel . Tokens . Jwt ;
55using System . Linq ;
66using System . Text . Json ;
7+ using System . Text . Json . Nodes ;
78using Microsoft . Extensions . Configuration ;
89using Microsoft . Extensions . Configuration . UserSecrets ;
910using Microsoft . Extensions . Tools . Internal ;
@@ -84,35 +85,36 @@ public static byte[] CreateSigningKeyMaterial(string userSecretsId, bool reset =
8485 var secretsFilePath = PathHelper . GetSecretsPathFromSecretsId ( userSecretsId ) ;
8586 Directory . CreateDirectory ( Path . GetDirectoryName ( secretsFilePath ) ) ;
8687
87- IDictionary < string , string > secrets = null ;
88+ JsonObject secrets = null ;
8889 if ( File . Exists ( secretsFilePath ) )
8990 {
9091 using var secretsFileStream = new FileStream ( secretsFilePath , FileMode . Open , FileAccess . Read ) ;
9192 if ( secretsFileStream . Length > 0 )
9293 {
93- secrets = JsonSerializer . Deserialize < IDictionary < string , string > > ( secretsFileStream ) ?? new Dictionary < string , string > ( ) ;
94+ secrets = JsonSerializer . Deserialize < JsonObject > ( secretsFileStream ) ;
9495 }
9596 }
9697
97- secrets ??= new Dictionary < string , string > ( ) ;
98+ secrets ??= new JsonObject ( ) ;
9899
99100 if ( reset && secrets . ContainsKey ( DevJwtsDefaults . SigningKeyConfigurationKey ) )
100101 {
101102 secrets . Remove ( DevJwtsDefaults . SigningKeyConfigurationKey ) ;
102103 }
103- secrets . Add ( DevJwtsDefaults . SigningKeyConfigurationKey , Convert . ToBase64String ( newKeyMaterial ) ) ;
104+ secrets . Add ( DevJwtsDefaults . SigningKeyConfigurationKey , JsonValue . Create ( Convert . ToBase64String ( newKeyMaterial ) ) ) ;
104105
105106 using var secretsWriteStream = new FileStream ( secretsFilePath , FileMode . Create , FileAccess . Write ) ;
106107 JsonSerializer . Serialize ( secretsWriteStream , secrets ) ;
107108
108109 return newKeyMaterial ;
109110 }
110111
111- public static string [ ] GetAudienceCandidatesFromLaunchSettings ( string project )
112+ public static List < string > GetAudienceCandidatesFromLaunchSettings ( string project )
112113 {
113114 ArgumentException . ThrowIfNullOrEmpty ( nameof ( project ) ) ;
114115
115116 var launchSettingsFilePath = Path . Combine ( Path . GetDirectoryName ( project ) ! , "Properties" , "launchSettings.json" ) ;
117+ var applicationUrls = new List < string > ( ) ;
116118 if ( File . Exists ( launchSettingsFilePath ) )
117119 {
118120 using var launchSettingsFileStream = new FileStream ( launchSettingsFilePath , FileMode . Open , FileAccess . Read ) ;
@@ -124,26 +126,65 @@ public static string[] GetAudienceCandidatesFromLaunchSettings(string project)
124126 var profilesEnumerator = profiles . EnumerateObject ( ) ;
125127 foreach ( var profile in profilesEnumerator )
126128 {
127- if ( profile . Value . TryGetProperty ( "commandName" , out var commandName ) )
129+ if ( ExtractKestrelUrlsFromProfile ( profile ) is { } kestrelUrls )
128130 {
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 ) ;
140137 }
141138 }
142139 }
143140 }
144141 }
145142
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+ }
147188 }
148189
149190 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
163204 : string . Join ( ", " , jwt . Scopes ) ;
164205 reporter . Output ( $ "{ Resources . JwtPrint_Scopes } : { scopesValue } ") ;
165206 }
166-
207+
167208 if ( ! jwt . Roles . IsNullOrEmpty ( ) || showAll )
168209 {
169210 var rolesValue = jwt . Roles . IsNullOrEmpty ( )
170211 ? "none"
171- : String . Join ( ", " , jwt . Roles ) ;
212+ : string . Join ( ", " , jwt . Roles ) ;
172213 reporter . Output ( $ "{ Resources . JwtPrint_Roles } : [{ rolesValue } ]") ;
173214 }
174215
0 commit comments