1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Linq ;
4
3
using Renci . SshNet . Common ;
5
4
6
5
namespace Renci . SshNet
@@ -48,10 +47,10 @@ public void Authenticate(IConnectionInfoInternal connectionInfo, ISession sessio
48
47
49
48
private static bool TryAuthenticate ( ISession session ,
50
49
AuthenticationState authenticationState ,
51
- ICollection < string > allowedAuthenticationMethods ,
50
+ string [ ] allowedAuthenticationMethods ,
52
51
ref SshAuthenticationException authenticationException )
53
52
{
54
- if ( allowedAuthenticationMethods . Count == 0 )
53
+ if ( allowedAuthenticationMethods . Length == 0 )
55
54
{
56
55
authenticationException = new SshAuthenticationException ( "No authentication methods defined on SSH server." ) ;
57
56
return false ;
@@ -60,10 +59,10 @@ private static bool TryAuthenticate(ISession session,
60
59
// we want to try authentication methods in the order in which they were
61
60
// passed in the ctor, not the order in which the SSH server returns
62
61
// 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 )
65
64
{
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 ) ) ) ;
67
66
return false ;
68
67
}
69
68
@@ -108,11 +107,31 @@ private static bool TryAuthenticate(ISession session,
108
107
return false ;
109
108
}
110
109
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 )
112
131
{
113
132
var skippedAuthenticationMethods = new List < IAuthenticationMethod > ( ) ;
114
133
115
- for ( var i = 0 ; i < matchingAuthenticationMethods . Length ; i ++ )
134
+ for ( var i = 0 ; i < matchingAuthenticationMethods . Count ; i ++ )
116
135
{
117
136
var authenticationMethod = matchingAuthenticationMethods [ i ] ;
118
137
@@ -162,7 +181,7 @@ public AuthenticationState(IList<IAuthenticationMethod> supportedAuthenticationM
162
181
/// <value>
163
182
/// The list of supported authentication methods.
164
183
/// </value>
165
- public IEnumerable < IAuthenticationMethod > SupportedAuthenticationMethods
184
+ public IList < IAuthenticationMethod > SupportedAuthenticationMethods
166
185
{
167
186
get { return _supportedAuthenticationMethods ; }
168
187
}
0 commit comments