/// <exception cref="AuthenticationException" />
        private async Task <Response> Authenticate(byte[] token, IAuthenticator authenticator)
        {
            var request  = new AuthResponseRequest(token);
            var response = await Send(request).ConfigureAwait(false);

            if (response is AuthSuccessResponse)
            {
                // It is now authenticated, dispose Authenticator if it implements IDisposable()
                // ReSharper disable once SuspiciousTypeConversion.Global
                var disposableAuthenticator = authenticator as IDisposable;
                if (disposableAuthenticator != null)
                {
                    disposableAuthenticator.Dispose();
                }
                return(response);
            }
            if (response is AuthChallengeResponse)
            {
                token = authenticator.EvaluateChallenge(((AuthChallengeResponse)response).Token);
                if (token == null)
                {
                    // If we get a null response, then authentication has completed
                    // return without sending a further response back to the server.
                    return(response);
                }
                return(await Authenticate(token, authenticator).ConfigureAwait(false));
            }
            throw new ProtocolErrorException("Expected SASL response, obtained " + response.GetType().Name);
        }
示例#2
0
 /// <exception cref="AuthenticationException" />
 private Task<Response> Authenticate(byte[] token, IAuthenticator authenticator)
 {
     var request = new AuthResponseRequest(ProtocolVersion, token);
     return Send(request)
         .Then(response =>
         {
             if (response is AuthSuccessResponse)
             {
                 //It is now authenticated
                 return TaskHelper.ToTask(response);
             }
             if (response is AuthChallengeResponse)
             {
                 token = authenticator.EvaluateChallenge(((AuthChallengeResponse)response).Token);
                 if (token == null)
                 {
                     // If we get a null response, then authentication has completed
                     //return without sending a further response back to the server.
                     return TaskHelper.ToTask(response);
                 }
                 return Authenticate(token, authenticator);
             }
             throw new ProtocolErrorException("Expected SASL response, obtained " + response.GetType().Name);
         });
 }