@@ -235,28 +235,35 @@ public static List<Dictionary<string, long>> getTimeData()
235235 /// </summary>
236236 /// <param name="session"></param>
237237 /// <returns></returns>
238- public static dynamic DecodeSid ( string session )
238+ public static Dictionary < string , object > DecodeSid ( string sid )
239239 {
240- // Replace characters and adjust padding
241- session = session . Replace ( '-' , '_' ) . Replace ( '+' , '/' ) ;
242- int padding = session . Length % 4 ;
243- if ( padding > 0 )
240+ sid = sid . Replace ( '-' , '+' ) . Replace ( '_' , '/' ) ;
241+
242+ int padding = 4 - ( sid . Length % 4 ) ;
243+ if ( padding < 4 )
244244 {
245- session = session . PadRight ( session . Length + ( 4 - padding ) , '=' ) ;
245+ sid += new string ( '=' , padding ) ;
246246 }
247247
248- // Decode base64, remove the first byte, and remove the last 21 bytes
249- byte [ ] bytes = Convert . FromBase64String ( session ) ;
250- bytes = bytes . Skip ( 1 ) . Take ( bytes . Length - 21 ) . ToArray ( ) ;
248+ byte [ ] decodedBytes = Convert . FromBase64String ( sid ) ;
249+
250+ byte [ ] trimmedBytes = new byte [ decodedBytes . Length - 21 ] ;
251+ Array . Copy ( decodedBytes , 1 , trimmedBytes , 0 , trimmedBytes . Length ) ;
252+ string jsonString = Encoding . UTF8 . GetString ( trimmedBytes ) ;
253+
254+ // Deserialize JSON string to dictionary
255+ var options = new JsonSerializerOptions
256+ {
257+ PropertyNameCaseInsensitive = true
258+ } ;
259+ var dictionary = System . Text . Json . JsonSerializer . Deserialize < Dictionary < string , object > > ( jsonString , options ) ;
251260
252- // Convert bytes to UTF-8 string and parse JSON directly
253- string jsonString = Encoding . UTF8 . GetString ( bytes ) ;
254- return System . Text . Json . JsonSerializer . Deserialize < dynamic > ( jsonString ) ;
261+ return dictionary ;
255262 }
256263
257- public static string sid_to_uid ( string session ) { return DecodeSid ( session ) [ "2" ] ; }
258- public static string sid_to_ip_address ( string session ) { return DecodeSid ( session ) [ "4" ] ; }
259- public static string sid_created_time ( string session ) { return DecodeSid ( session ) [ "5" ] ; }
260- public static string sid_to_client_type ( string session ) { return DecodeSid ( session ) [ "6" ] ; }
264+ public static string sid_to_uid ( string session ) { return DecodeSid ( session ) [ "2" ] . ToString ( ) ; }
265+ public static string sid_to_ip_address ( string session ) { return DecodeSid ( session ) [ "4" ] . ToString ( ) ; }
266+ public static string sid_created_time ( string session ) { return DecodeSid ( session ) [ "5" ] . ToString ( ) ; }
267+ public static string sid_to_client_type ( string session ) { return DecodeSid ( session ) [ "6" ] . ToString ( ) ; }
261268 }
262269}
0 commit comments