Renci.SshNet.NoneAuthenticationMethod.Authenticate C# (CSharp) Method

Authenticate() public method

Authenticates the specified session.
is null.
public Authenticate ( Session session ) : AuthenticationResult
session Session The session.
return AuthenticationResult
        public override AuthenticationResult Authenticate(Session session)
        {
            if (session == null)
                throw new ArgumentNullException("session");

            session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived;
            session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived;

            try
            {
                session.SendMessage(new RequestMessageNone(ServiceName.Connection, Username));
                session.WaitOnHandle(_authenticationCompleted);
            }
            finally
            {
                session.UserAuthenticationSuccessReceived -= Session_UserAuthenticationSuccessReceived;
                session.UserAuthenticationFailureReceived -= Session_UserAuthenticationFailureReceived;
            }

            return _authenticationResult;
        }

Usage Example

        /// <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);
        }
All Usage Examples Of Renci.SshNet.NoneAuthenticationMethod::Authenticate