Ejemplo n.º 1
0
 private static void AuthenticateContinueCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         ConnectAndHandshakeAsyncResult thisPtr = (ConnectAndHandshakeAsyncResult)result.AsyncState !;
         try
         {
             LineInfo info = AuthCommand.EndSend(result);
             if ((int)info.StatusCode == 235)
             {
                 thisPtr._connection._authenticationModules[thisPtr._currentModule].CloseContext(thisPtr._connection);
                 thisPtr._connection._isConnected = true;
                 thisPtr.InvokeCallback();
                 return;
             }
             else if ((int)info.StatusCode == 334)
             {
                 thisPtr._authResponse = info.Line;
                 if (!thisPtr.AuthenticateContinue())
                 {
                     return;
                 }
             }
             thisPtr.Authenticate();
         }
         catch (Exception e)
         {
             thisPtr.InvokeCallback(e);
         }
     }
 }
Ejemplo n.º 2
0
            private bool AuthenticateContinue()
            {
                while (true)
                {
                    // We don't need credential on the continued auth assuming they were captured on the first call.
                    // That should always work, otherwise what if a new credential has been returned?
                    Authorization?auth = _connection._authenticationModules[_currentModule].Authenticate(_authResponse, null, _connection, _connection._client !.TargetName, null);
                    if (auth == null)
                    {
                        throw new SmtpException(SR.SmtpAuthenticationFailed);
                    }

                    IAsyncResult result = AuthCommand.BeginSend(_connection, auth.Message, s_authenticateContinueCallback, this);
                    if (!result.CompletedSynchronously)
                    {
                        return(false);
                    }

                    LineInfo info = AuthCommand.EndSend(result);
                    if ((int)info.StatusCode == 235)
                    {
                        _connection._authenticationModules[_currentModule].CloseContext(_connection);
                        _connection._isConnected = true;
                        InvokeCallback();
                        return(false);
                    }
                    else if ((int)info.StatusCode != 334)
                    {
                        return(true);
                    }
                    _authResponse = info.Line;
                }
            }
            bool AuthenticateContinue()        //10
            {
                for (; ;)
                {
                    // We don't need credential on the continued auth assuming they were captured on the first call.
                    // That should always work, otherwise what if a new credential has been returned?
                    Authorization auth = connection.authenticationModules[currentModule].Authenticate(authResponse, null, connection, connection.client.TargetName, connection.channelBindingToken);
                    if (auth == null)
                    {
                        throw new SmtpException(SR.GetString(SR.SmtpAuthenticationFailed));
                    }

                    IAsyncResult result = AuthCommand.BeginSend(connection, auth.Message, authenticateContinueCallback, this);
                    if (!result.CompletedSynchronously)
                    {
                        return(false);
                    }

                    LineInfo info = AuthCommand.EndSend(result);
                    if ((int)info.StatusCode == 235)
                    {
                        connection.authenticationModules[currentModule].CloseContext(connection);
                        connection.isConnected = true;
                        InvokeCallback();
                        return(false);
                    }
                    else if ((int)info.StatusCode != 334)
                    {
                        return(true);
                    }
                    authResponse = info.Line;
                }
            }
Ejemplo n.º 4
0
            void Authenticate() //8
            {
                //if no credentials were supplied, try anonymous
                //servers don't appear to anounce that they support anonymous login.
                if (connection.credentials != null)
                {
                    while (++currentModule < connection.authenticationModules.Length)
                    {
                        //only authenticate if the auth protocol is supported
                        ISmtpAuthenticationModule module = connection.authenticationModules[currentModule];
                        if (!connection.AuthSupported(module))
                        {
                            continue;
                        }

                        NetworkCredential credential = connection.credentials.GetCredential(host, port, module.AuthenticationType);
                        if (credential == null)
                        {
                            continue;
                        }
                        Authorization auth = connection.SetContextAndTryAuthenticate(module, credential, m_OuterResult);

                        if (auth != null && auth.Message != null)
                        {
                            IAsyncResult result = AuthCommand.BeginSend(connection, connection.authenticationModules[currentModule].AuthenticationType, auth.Message, authenticateCallback, this);
                            if (!result.CompletedSynchronously)
                            {
                                return;
                            }

                            LineInfo info = AuthCommand.EndSend(result);

                            if ((int)info.StatusCode == 334)
                            {
                                authResponse = info.Line;
                                if (!AuthenticateContinue())
                                {
                                    return;
                                }
                            }
                            else if ((int)info.StatusCode == 235)
                            {
                                module.CloseContext(connection);
                                connection.isConnected = true;
                                break;
                            }
                        }
                    }

                    //try anonymous if didn't authenticate
                    //if (!connection.isConnected) {
                    //    throw new SmtpException(SR.GetString(SR.SmtpAuthenticationFailed));
                    // }
                }

                connection.isConnected = true;
                InvokeCallback();
            }
Ejemplo n.º 5
0
            private void Authenticate()
            {
                //if no credentials were supplied, try anonymous
                //servers don't appear to anounce that they support anonymous login.
                if (_connection._credentials != null)
                {
                    while (++_currentModule < _connection._authenticationModules.Length)
                    {
                        //only authenticate if the auth protocol is supported
                        ISmtpAuthenticationModule module = _connection._authenticationModules[_currentModule];
                        if (!_connection.AuthSupported(module))
                        {
                            continue;
                        }

                        NetworkCredential credential = _connection._credentials.GetCredential(_host, _port, module.AuthenticationType);
                        if (credential == null)
                        {
                            continue;
                        }
                        Authorization auth = _connection.SetContextAndTryAuthenticate(module, credential, _outerResult);

                        if (auth != null && auth.Message != null)
                        {
                            IAsyncResult result = AuthCommand.BeginSend(_connection, _connection._authenticationModules[_currentModule].AuthenticationType, auth.Message, s_authenticateCallback, this);
                            if (!result.CompletedSynchronously)
                            {
                                return;
                            }

                            LineInfo info = AuthCommand.EndSend(result);

                            if ((int)info.StatusCode == 334)
                            {
                                _authResponse = info.Line;
                                if (!AuthenticateContinue())
                                {
                                    return;
                                }
                            }
                            else if ((int)info.StatusCode == 235)
                            {
                                module.CloseContext(_connection);
                                _connection._isConnected = true;
                                break;
                            }
                        }
                    }
                }

                _connection._isConnected = true;
                InvokeCallback();
            }
 private void Authenticate()
 {
     if (this.connection.credentials != null)
     {
         while (++this.currentModule < this.connection.authenticationModules.Length)
         {
             ISmtpAuthenticationModule module = this.connection.authenticationModules[this.currentModule];
             if (this.connection.AuthSupported(module))
             {
                 NetworkCredential credential = this.connection.credentials.GetCredential(this.host, this.port, module.AuthenticationType);
                 if (credential != null)
                 {
                     Authorization authorization = this.connection.SetContextAndTryAuthenticate(module, credential, this.m_OuterResult);
                     if ((authorization != null) && (authorization.Message != null))
                     {
                         IAsyncResult result = AuthCommand.BeginSend(this.connection, this.connection.authenticationModules[this.currentModule].AuthenticationType, authorization.Message, authenticateCallback, this);
                         if (!result.CompletedSynchronously)
                         {
                             return;
                         }
                         LineInfo info = AuthCommand.EndSend(result);
                         if (info.StatusCode == ((SmtpStatusCode)0x14e))
                         {
                             this.authResponse = info.Line;
                             if (!this.AuthenticateContinue())
                             {
                                 return;
                             }
                         }
                         else if (info.StatusCode == ((SmtpStatusCode)0xeb))
                         {
                             module.CloseContext(this.connection);
                             this.connection.isConnected = true;
                             break;
                         }
                     }
                 }
             }
         }
     }
     this.connection.isConnected = true;
     base.InvokeCallback();
 }