MailKit.Security.SaslMechanismLogin.Challenge C# (CSharp) Méthode

Challenge() protected méthode

Parses the server's challenge token and returns the next challenge response.
Parses the server's challenge token and returns the next challenge response.
/// The SASL mechanism is already authenticated. /// /// The SASL mechanism does not support SASL-IR. /// /// An error has occurred while parsing the server's challenge token. ///
protected Challenge ( byte token, int startIndex, int length ) : byte[]
token byte The server's challenge token.
startIndex int The index into the token specifying where the server's challenge begins.
length int The length of the server's challenge.
Résultat byte[]
		protected override byte[] Challenge (byte[] token, int startIndex, int length)
		{
			var cred = Credentials.GetCredential (Uri, MechanismName);
			byte[] challenge;

			if (token == null)
				throw new NotSupportedException ("LOGIN does not support SASL-IR.");

			switch (state) {
			case LoginState.UserName:
				challenge = encoding.GetBytes (cred.UserName);
				state = LoginState.Password;
				break;
			case LoginState.Password:
				challenge = encoding.GetBytes (cred.Password);
				IsAuthenticated = true;
				break;
			default:
				throw new InvalidOperationException ();
			}

			return challenge;
		}

Usage Example

		public void TestArgumentExceptions ()
		{
			var credentials = new NetworkCredential ("username", "password");
			var uri = new Uri ("smtp://localhost");
			SaslMechanism sasl;

			Assert.Throws<ArgumentNullException> (() => new SaslException (null, SaslErrorCode.MissingChallenge, "message"));

			sasl = new SaslMechanismCramMd5 (uri, credentials);
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismCramMd5 (null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismCramMd5 (uri, null));
			Assert.Throws<NotSupportedException> (() => sasl.Challenge (null));

			sasl = new SaslMechanismDigestMd5 (uri, credentials);
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismDigestMd5 (null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismDigestMd5 (uri, null));
			Assert.Throws<NotSupportedException> (() => sasl.Challenge (null));

			sasl = new SaslMechanismLogin (uri, credentials);
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismLogin (uri, null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismLogin (null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismLogin (uri, null));
			Assert.Throws<NotSupportedException> (() => sasl.Challenge (null));

			sasl = new SaslMechanismNtlm (uri, credentials);
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismNtlm (null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismNtlm (uri, null));
			Assert.DoesNotThrow (() => sasl.Challenge (null));

			sasl = new SaslMechanismOAuth2 (uri, credentials);
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismOAuth2 (null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismOAuth2 (uri, null));
			Assert.DoesNotThrow (() => sasl.Challenge (null));

			sasl = new SaslMechanismPlain (uri, credentials);
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismPlain (uri, null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismPlain (null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismPlain (uri, null));
			Assert.DoesNotThrow (() => sasl.Challenge (null));

			sasl = new SaslMechanismScramSha1 (uri, credentials);
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismScramSha1 (null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismScramSha1 (uri, null));
			Assert.DoesNotThrow (() => sasl.Challenge (null));

			sasl = new SaslMechanismScramSha256 (uri, credentials);
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismScramSha256 (null, credentials));
			Assert.Throws<ArgumentNullException> (() => new SaslMechanismScramSha256 (uri, null));
			Assert.DoesNotThrow (() => sasl.Challenge (null));
		}
All Usage Examples Of MailKit.Security.SaslMechanismLogin::Challenge