MailKit.Security.SaslMechanismNtlm.Challenge C# (CSharp) Method

Challenge() protected method

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. /// /// 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.
return byte[]
		protected override byte[] Challenge (byte[] token, int startIndex, int length)
		{
			if (IsAuthenticated)
				throw new InvalidOperationException ();

			var cred = Credentials.GetCredential (Uri, MechanismName);
			string password = cred.Password ?? string.Empty;
			string userName = cred.UserName;
			string domain = cred.Domain;
			MessageBase message;

			if (string.IsNullOrEmpty (domain)) {
				int index = userName.IndexOf ('\\');
				if (index == -1)
					index = userName.IndexOf ('/');

				if (index >= 0) {
					domain = userName.Substring (0, index);
					userName = userName.Substring (index + 1);
				}
			}

			switch (state) {
			case LoginState.Initial:
				message = GetInitialResponse (domain);
				state = LoginState.Challenge;
				break;
			case LoginState.Challenge:
				message = GetChallengeResponse (userName, password, domain, token, startIndex, length);
				IsAuthenticated = true;
				break;
			default:
				throw new IndexOutOfRangeException ("state");
			}

			return message.Encode ();
		}

Usage Example

コード例 #1
0
		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.SaslMechanismNtlm::Challenge