11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
43using Renci . SshNet . Common ;
54
65namespace Renci . SshNet
@@ -48,10 +47,10 @@ public void Authenticate(IConnectionInfoInternal connectionInfo, ISession sessio
4847
4948 private static bool TryAuthenticate ( ISession session ,
5049 AuthenticationState authenticationState ,
51- ICollection < string > allowedAuthenticationMethods ,
50+ string [ ] allowedAuthenticationMethods ,
5251 ref SshAuthenticationException authenticationException )
5352 {
54- if ( allowedAuthenticationMethods . Count == 0 )
53+ if ( allowedAuthenticationMethods . Length == 0 )
5554 {
5655 authenticationException = new SshAuthenticationException ( "No authentication methods defined on SSH server." ) ;
5756 return false ;
@@ -60,10 +59,10 @@ private static bool TryAuthenticate(ISession session,
6059 // we want to try authentication methods in the order in which they were
6160 // passed in the ctor, not the order in which the SSH server returns
6261 // the allowed authentication methods
63- var matchingAuthenticationMethods = authenticationState . SupportedAuthenticationMethods . Where ( a => allowedAuthenticationMethods . Contains ( a . Name ) ) . ToArray ( ) ;
64- if ( matchingAuthenticationMethods . Length == 0 )
62+ var matchingAuthenticationMethods = GetAllowedAuthenticationMethodsThatAreSupported ( authenticationState , allowedAuthenticationMethods ) ;
63+ if ( matchingAuthenticationMethods . Count == 0 )
6564 {
66- authenticationException = new SshAuthenticationException ( string . Format ( "No suitable authentication method found to complete authentication ({0})." , string . Join ( "," , allowedAuthenticationMethods . ToArray ( ) ) ) ) ;
65+ authenticationException = new SshAuthenticationException ( string . Format ( "No suitable authentication method found to complete authentication ({0})." , string . Join ( "," , allowedAuthenticationMethods ) ) ) ;
6766 return false ;
6867 }
6968
@@ -108,11 +107,31 @@ private static bool TryAuthenticate(ISession session,
108107 return false ;
109108 }
110109
111- private static IEnumerable < IAuthenticationMethod > GetOrderedAuthenticationMethods ( AuthenticationState authenticationState , IAuthenticationMethod [ ] matchingAuthenticationMethods )
110+ private static List < IAuthenticationMethod > GetAllowedAuthenticationMethodsThatAreSupported ( AuthenticationState authenticationState ,
111+ string [ ] allowedAuthenticationMethods )
112+ {
113+ var result = new List < IAuthenticationMethod > ( ) ;
114+
115+ foreach ( var supportedAuthenticationMethod in authenticationState . SupportedAuthenticationMethods )
116+ {
117+ for ( var i = 0 ; i < allowedAuthenticationMethods . Length ; i ++ )
118+ {
119+ if ( allowedAuthenticationMethods [ i ] == supportedAuthenticationMethod . Name )
120+ {
121+ result . Add ( supportedAuthenticationMethod ) ;
122+ break ;
123+ }
124+ }
125+ }
126+
127+ return result ;
128+ }
129+
130+ private static IEnumerable < IAuthenticationMethod > GetOrderedAuthenticationMethods ( AuthenticationState authenticationState , List < IAuthenticationMethod > matchingAuthenticationMethods )
112131 {
113132 var skippedAuthenticationMethods = new List < IAuthenticationMethod > ( ) ;
114133
115- for ( var i = 0 ; i < matchingAuthenticationMethods . Length ; i ++ )
134+ for ( var i = 0 ; i < matchingAuthenticationMethods . Count ; i ++ )
116135 {
117136 var authenticationMethod = matchingAuthenticationMethods [ i ] ;
118137
@@ -162,7 +181,7 @@ public AuthenticationState(IList<IAuthenticationMethod> supportedAuthenticationM
162181 /// <value>
163182 /// The list of supported authentication methods.
164183 /// </value>
165- public IEnumerable < IAuthenticationMethod > SupportedAuthenticationMethods
184+ public IList < IAuthenticationMethod > SupportedAuthenticationMethods
166185 {
167186 get { return _supportedAuthenticationMethods ; }
168187 }
0 commit comments