/// <summary> /// Authenticates the specified session. /// </summary> /// <param name="session">The session to be authenticated.</param> /// <returns>true if authenticated; otherwise false.</returns> /// <exception cref="ArgumentNullException"><paramref name="session"/> is null.</exception> /// <exception cref="SshAuthenticationException">No suitable authentication method found to complete authentication.</exception> public bool Authenticate(Session session) { var authenticated = AuthenticationResult.Failure; if (session == null) { throw new ArgumentNullException("session"); } session.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"); session.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"); session.RegisterMessage("SSH_MSG_USERAUTH_BANNER"); session.UserAuthenticationBannerReceived += Session_UserAuthenticationBannerReceived; // Try to authenticate against none var noneAuthenticationMethod = new NoneAuthenticationMethod(this.Username); authenticated = noneAuthenticationMethod.Authenticate(session); var allowedAuthentications = noneAuthenticationMethod.AllowedAuthentications; var triedAuthentications = new List <string>(); while (authenticated != AuthenticationResult.Success) { // Find first authentication method var method = this.AuthenticationMethods.Where((a) => allowedAuthentications.Contains(a.Name) && !triedAuthentications.Contains(a.Name)).FirstOrDefault(); if (method == null) { throw new SshAuthenticationException("No suitable authentication method found to complete authentication."); } triedAuthentications.Add(method.Name); authenticated = method.Authenticate(session); if (authenticated == AuthenticationResult.PartialSuccess) { // If further authentication is required then continue to try another method allowedAuthentications = method.AllowedAuthentications; continue; } // If authentication was successful or failure, exit break; } session.UserAuthenticationBannerReceived -= Session_UserAuthenticationBannerReceived; session.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"); session.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"); session.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"); this.IsAuthenticated = authenticated == AuthenticationResult.Success; return(authenticated == AuthenticationResult.Success); }
/// <summary> /// Authenticates the specified session. /// </summary> /// <param name="session">The session to be authenticated.</param> /// <exception cref="ArgumentNullException"><paramref name="session"/> is null.</exception> /// <exception cref="SshAuthenticationException">No suitable authentication method found to complete authentication, or permission denied.</exception> public void Authenticate(Session session) { if (session == null) { throw new ArgumentNullException("session"); } session.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"); session.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"); session.RegisterMessage("SSH_MSG_USERAUTH_BANNER"); session.UserAuthenticationBannerReceived += Session_UserAuthenticationBannerReceived; try { // the exception to report an authentication failure with SshAuthenticationException authenticationException = null; // try to authenticate against none var noneAuthenticationMethod = new NoneAuthenticationMethod(this.Username); var authenticated = noneAuthenticationMethod.Authenticate(session); if (authenticated != AuthenticationResult.Success) { var failedAuthenticationMethods = new List <AuthenticationMethod>(); if (TryAuthenticate(session, noneAuthenticationMethod.AllowedAuthentications.ToList(), failedAuthenticationMethods, ref authenticationException)) { authenticated = AuthenticationResult.Success; } } this.IsAuthenticated = authenticated == AuthenticationResult.Success; if (!IsAuthenticated) { throw authenticationException; } } finally { session.UserAuthenticationBannerReceived -= Session_UserAuthenticationBannerReceived; session.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"); session.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"); session.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"); } }
/// <summary> /// Authenticates the specified session. /// </summary> /// <param name="session">The session to be authenticated.</param> /// <exception cref="ArgumentNullException"><paramref name="session"/> is null.</exception> /// <exception cref="SshAuthenticationException">No suitable authentication method found to complete authentication, or permission denied.</exception> public void Authenticate(Session session) { if (session == null) throw new ArgumentNullException("session"); session.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"); session.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"); session.RegisterMessage("SSH_MSG_USERAUTH_BANNER"); session.UserAuthenticationBannerReceived += Session_UserAuthenticationBannerReceived; try { // the exception to report an authentication failure with SshAuthenticationException authenticationException = null; // try to authenticate against none var noneAuthenticationMethod = new NoneAuthenticationMethod(this.Username); var authenticated = noneAuthenticationMethod.Authenticate(session); if (authenticated != AuthenticationResult.Success) { var failedAuthenticationMethods = new List<AuthenticationMethod>(); if (TryAuthenticate(session, noneAuthenticationMethod.AllowedAuthentications.ToList(), failedAuthenticationMethods, ref authenticationException)) { authenticated = AuthenticationResult.Success; } } this.IsAuthenticated = authenticated == AuthenticationResult.Success; if (!IsAuthenticated) throw authenticationException; } finally { session.UserAuthenticationBannerReceived -= Session_UserAuthenticationBannerReceived; session.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"); session.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"); session.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"); } }
/// <summary> /// Authenticates the specified session. /// </summary> /// <param name="session">The session to be authenticated.</param> /// <returns>true if authenticated; otherwise false.</returns> /// <exception cref="ArgumentNullException"><paramref name="session"/> is null.</exception> /// <exception cref="SshAuthenticationException">No suitable authentication method found to complete authentication.</exception> public bool Authenticate(Session session) { var authenticated = AuthenticationResult.Failure; if (session == null) throw new ArgumentNullException("session"); session.RegisterMessage("SSH_MSG_USERAUTH_FAILURE"); session.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS"); session.RegisterMessage("SSH_MSG_USERAUTH_BANNER"); session.UserAuthenticationBannerReceived += Session_UserAuthenticationBannerReceived; // Try to authenticate against none var noneAuthenticationMethod = new NoneAuthenticationMethod(this.Username); authenticated = noneAuthenticationMethod.Authenticate(session); var allowedAuthentications = noneAuthenticationMethod.AllowedAuthentications; var triedAuthentications = new List<string>(); while (authenticated != AuthenticationResult.Success) { // Find first authentication method var method = this.AuthenticationMethods.Where((a) => allowedAuthentications.Contains(a.Name) && !triedAuthentications.Contains(a.Name)).FirstOrDefault(); if (method == null) throw new SshAuthenticationException("No suitable authentication method found to complete authentication."); triedAuthentications.Add(method.Name); authenticated = method.Authenticate(session); if (authenticated == AuthenticationResult.PartialSuccess) { // If further authentication is required then continue to try another method allowedAuthentications = method.AllowedAuthentications; continue; } // If authentication was successful or failure, exit break; } session.UserAuthenticationBannerReceived -= Session_UserAuthenticationBannerReceived; session.UnRegisterMessage("SSH_MSG_USERAUTH_FAILURE"); session.UnRegisterMessage("SSH_MSG_USERAUTH_SUCCESS"); session.UnRegisterMessage("SSH_MSG_USERAUTH_BANNER"); this.IsAuthenticated = authenticated == AuthenticationResult.Success; return authenticated == AuthenticationResult.Success; }