Esempio n. 1
0
        /// <summary>
        /// Authenticates using the CRAM-MD5 authentication method
        /// </summary>
        /// <param name="username">The username</param>
        /// <param name="password">The user password</param>
        /// <exception cref="NotSupportedException">Thrown when the server does not support AUTH CRAM-MD5</exception>
        /// <exception cref="InvalidLoginException">If the user credentials was not accepted</exception>
        /// <exception cref="PopServerLockedException">If the server said the the mailbox was locked</exception>
        /// <exception cref="LoginDelayException">If the server rejects the login because of too recent logins</exception>
        private void AuthenticateUsingCramMd5(string username, string password)
        {
            // Example of communication:
            // C: AUTH CRAM-MD5
            // S: + PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ+
            // C: dGltIGI5MTNhNjAyYzdlZGE3YTQ5NWI0ZTZlNzMzNGQzODkw
            // S: +OK CRAM authentication successful

            // Other example, where AUTH CRAM-MD5 is not supported
            // C: AUTH CRAM-MD5
            // S: -ERR Authentication method CRAM-MD5 not supported

            try
            {
                SendCommand("AUTH CRAM-MD5");
            }
            catch (PopServerException e)
            {
                // A PopServerException will be thrown if the server responds with a -ERR not supported
                throw new NotSupportedException("CRAM-MD5 authentication not supported", e);
            }

            // Fetch out the challenge from the server response
            string challenge = LastServerResponse.Substring(2);

            // Compute the challenge response
            string response = CramMd5.ComputeDigest(username, password, challenge);

            // Send the response to the server
            SendCommand(response);

            // Authentication was successful if no exceptions thrown before getting here
        }