Change AuthenticationMethod.AllowedAuthentications from IList<string> to string[].

Remove usage of Linq.
This commit is contained in:
Gert Driesen
2017-09-10 20:54:14 +02:00
parent 0e083cfe60
commit 03644f00af
3 changed files with 32 additions and 12 deletions
+3 -2
View File
@@ -1,4 +1,5 @@
using System;
using Renci.SshNet.Common;
using System;
using System.Collections.Generic;
namespace Renci.SshNet
@@ -24,7 +25,7 @@ namespace Renci.SshNet
/// <summary>
/// Gets list of allowed authentications.
/// </summary>
public IList<string> AllowedAuthentications { get; protected set; }
public string[] AllowedAuthentications { get; protected set; }
/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationMethod"/> class.
+28 -9
View File
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Renci.SshNet.Common;
namespace Renci.SshNet
@@ -48,10 +47,10 @@ namespace Renci.SshNet
private static bool TryAuthenticate(ISession session,
AuthenticationState authenticationState,
ICollection<string> allowedAuthenticationMethods,
string[] allowedAuthenticationMethods,
ref SshAuthenticationException authenticationException)
{
if (allowedAuthenticationMethods.Count == 0)
if (allowedAuthenticationMethods.Length == 0)
{
authenticationException = new SshAuthenticationException("No authentication methods defined on SSH server.");
return false;
@@ -60,10 +59,10 @@ namespace Renci.SshNet
// we want to try authentication methods in the order in which they were
// passed in the ctor, not the order in which the SSH server returns
// the allowed authentication methods
var matchingAuthenticationMethods = authenticationState.SupportedAuthenticationMethods.Where(a => allowedAuthenticationMethods.Contains(a.Name)).ToArray();
if (matchingAuthenticationMethods.Length == 0)
var matchingAuthenticationMethods = GetAllowedAuthenticationMethodsThatAreSupported(authenticationState, allowedAuthenticationMethods);
if (matchingAuthenticationMethods.Count == 0)
{
authenticationException = new SshAuthenticationException(string.Format("No suitable authentication method found to complete authentication ({0}).", string.Join(",", allowedAuthenticationMethods.ToArray())));
authenticationException = new SshAuthenticationException(string.Format("No suitable authentication method found to complete authentication ({0}).", string.Join(",", allowedAuthenticationMethods)));
return false;
}
@@ -108,11 +107,31 @@ namespace Renci.SshNet
return false;
}
private static IEnumerable<IAuthenticationMethod> GetOrderedAuthenticationMethods(AuthenticationState authenticationState, IAuthenticationMethod[] matchingAuthenticationMethods)
private static List<IAuthenticationMethod> GetAllowedAuthenticationMethodsThatAreSupported(AuthenticationState authenticationState,
string[] allowedAuthenticationMethods)
{
var result = new List<IAuthenticationMethod>();
foreach (var supportedAuthenticationMethod in authenticationState.SupportedAuthenticationMethods)
{
for (var i = 0; i < allowedAuthenticationMethods.Length; i++)
{
if (allowedAuthenticationMethods[i] == supportedAuthenticationMethod.Name)
{
result.Add(supportedAuthenticationMethod);
break;
}
}
}
return result;
}
private static IEnumerable<IAuthenticationMethod> GetOrderedAuthenticationMethods(AuthenticationState authenticationState, List<IAuthenticationMethod> matchingAuthenticationMethods)
{
var skippedAuthenticationMethods = new List<IAuthenticationMethod>();
for (var i = 0; i < matchingAuthenticationMethods.Length; i++)
for (var i = 0; i < matchingAuthenticationMethods.Count; i++)
{
var authenticationMethod = matchingAuthenticationMethods[i];
@@ -162,7 +181,7 @@ namespace Renci.SshNet
/// <value>
/// The list of supported authentication methods.
/// </value>
public IEnumerable<IAuthenticationMethod> SupportedAuthenticationMethods
public IList<IAuthenticationMethod> SupportedAuthenticationMethods
{
get { return _supportedAuthenticationMethods; }
}
+1 -1
View File
@@ -22,7 +22,7 @@ namespace Renci.SshNet
/// <value>
/// The list of allowed authentications.
/// </value>
IList<string> AllowedAuthentications { get; }
string[] AllowedAuthentications { get; }
/// <summary>
/// Gets the name of the authentication method.