1- using System ;
2- using System . Text ;
1+ using System . Text ;
2+ using Microsoft . AspNetCore . Authentication ;
33
44namespace IdentityManager2 . Extensions
55{
@@ -8,48 +8,13 @@ public static class EncodingExtensions
88 public static string ToBase64UrlEncoded ( this string s )
99 {
1010 if ( s == null ) return null ;
11-
12- var bytes = Encoding . UTF8 . GetBytes ( s ) ;
13-
14- return bytes . ToBase64UrlEncoded ( ) ;
15- }
16-
17- public static string ToBase64UrlEncoded ( this byte [ ] bytes )
18- {
19- if ( bytes == null ) return null ;
20-
21- var s = Convert . ToBase64String ( bytes ) ;
22- s = s . Split ( '=' ) [ 0 ] ; // Remove any trailing '='s
23- s = s . Replace ( '+' , '-' ) ; // 62nd char of encoding
24- s = s . Replace ( '/' , '_' ) ; // 63rd char of encoding
25-
26- return s ;
11+ return Base64UrlTextEncoder . Encode ( Encoding . UTF8 . GetBytes ( s ) ) ;
2712 }
2813
2914 public static string FromBase64UrlEncoded ( this string s )
3015 {
3116 if ( s == null ) return null ;
32-
33- var bytes = s . FromBase64UrlEncodedBytes ( ) ;
34- return Encoding . UTF8 . GetString ( bytes ) ;
35- }
36-
37- public static byte [ ] FromBase64UrlEncodedBytes ( this string s )
38- {
39- if ( s == null ) return null ;
40-
41- s = s . Replace ( '-' , '+' ) ; // 62nd char of encoding
42- s = s . Replace ( '_' , '/' ) ; // 63rd char of encoding
43-
44- switch ( s . Length % 4 ) // Pad with trailing '='s
45- {
46- case 0 : break ; // No pad chars in this case
47- case 2 : s += "==" ; break ; // Two pad chars
48- case 3 : s += "=" ; break ; // One pad char
49- default : throw new Exception ( "Illegal base64url string!" ) ;
50- }
51-
52- return Convert . FromBase64String ( s ) ;
17+ return Encoding . UTF8 . GetString ( Base64UrlTextEncoder . Decode ( s ) ) ;
5318 }
5419 }
5520}
0 commit comments