1- using System ;
2- using System . Collections . Generic ;
1+ using System ;
32using System . Text ;
43
54namespace GxClasses . Helpers
65{
76 public class BasicAuthenticationHeaderValue
87 {
8+ const char UserNamePasswordSeparator = ':' ;
99 public BasicAuthenticationHeaderValue ( string authenticationHeaderValue )
1010 {
1111 if ( ! string . IsNullOrWhiteSpace ( authenticationHeaderValue ) )
@@ -19,7 +19,7 @@ public BasicAuthenticationHeaderValue(string authenticationHeaderValue)
1919 }
2020
2121 private readonly string _authenticationHeaderValue ;
22- private string [ ] _splitDecodedCredentials ;
22+ private string _usernamePassword ;
2323
2424 public bool IsValidBasicAuthenticationHeaderValue { get ; private set ; }
2525 public string UserIdentifier { get ; private set ; }
@@ -32,11 +32,11 @@ private bool TryDecodeHeaderValue()
3232 {
3333 return false ;
3434 }
35- var encodedCredentials = _authenticationHeaderValue . Substring ( headerSchemeLength ) ;
35+ string encodedCredentials = _authenticationHeaderValue . Substring ( headerSchemeLength ) ;
3636 try
3737 {
38- var decodedCredentials = Convert . FromBase64String ( encodedCredentials ) ;
39- _splitDecodedCredentials = System . Text . Encoding . ASCII . GetString ( decodedCredentials ) . Split ( ':' ) ;
38+ byte [ ] decodedCredentials = Convert . FromBase64String ( encodedCredentials ) ;
39+ _usernamePassword = Encoding . ASCII . GetString ( decodedCredentials ) ;
4040 return true ;
4141 }
4242 catch ( FormatException )
@@ -47,13 +47,19 @@ private bool TryDecodeHeaderValue()
4747
4848 private void ReadAuthenticationHeaderValue ( )
4949 {
50- IsValidBasicAuthenticationHeaderValue = _splitDecodedCredentials != null && _splitDecodedCredentials . Length == 2
51- && ! string . IsNullOrWhiteSpace ( _splitDecodedCredentials [ 0 ] )
52- && ! string . IsNullOrWhiteSpace ( _splitDecodedCredentials [ 1 ] ) ;
50+ IsValidBasicAuthenticationHeaderValue = ! string . IsNullOrEmpty ( _usernamePassword ) && _usernamePassword . Contains ( UserNamePasswordSeparator ) ;
5351 if ( IsValidBasicAuthenticationHeaderValue )
5452 {
55- UserIdentifier = _splitDecodedCredentials [ 0 ] ;
56- UserPassword = _splitDecodedCredentials [ 1 ] ;
53+ int separatorIndex = _usernamePassword . IndexOf ( UserNamePasswordSeparator ) ;
54+ UserIdentifier = _usernamePassword . Substring ( 0 , separatorIndex ) ;
55+ if ( separatorIndex + 1 < _usernamePassword . Length )
56+ {
57+ UserPassword = _usernamePassword . Substring ( separatorIndex + 1 ) ;
58+ }
59+ else
60+ {
61+ UserPassword = string . Empty ;
62+ }
5763 }
5864 }
5965 }
0 commit comments