internal bool AuthSupported(ISmtpAuthenticationModule module) { if (module is SmtpLoginAuthenticationModule) { if ((this.supportedAuth & SupportedAuth.Login) > SupportedAuth.None) { return(true); } } else if (module is SmtpNegotiateAuthenticationModule) { if ((this.supportedAuth & SupportedAuth.GSSAPI) > SupportedAuth.None) { this.sawNegotiate = true; return(true); } } else if (module is SmtpNtlmAuthenticationModule) { if (!this.sawNegotiate && ((this.supportedAuth & SupportedAuth.NTLM) > SupportedAuth.None)) { return(true); } } else if ((module is SmtpDigestAuthenticationModule) && ((this.supportedAuth & SupportedAuth.WDigest) > SupportedAuth.None)) { return(true); } return(false); }
private Authorization?SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential?credential, ContextAwareResult?context) { // We may need to restore user thread token here if (ReferenceEquals(credential, CredentialCache.DefaultNetworkCredentials)) { #if DEBUG Debug.Assert(context == null || context.IdentityRequested, "Authentication required when it wasn't expected. (Maybe Credentials was changed on another thread?)"); #endif try { ExecutionContext?x = context == null ? null : context.ContextCopy; if (x != null) { AuthenticateCallbackContext authenticationContext = new AuthenticateCallbackContext(this, module, credential, _client !.TargetName, null); ExecutionContext.Run(x, s_AuthenticateCallback, authenticationContext); return(authenticationContext._result); } else { return(module.Authenticate(null, credential, this, _client !.TargetName, null)); } } catch { // Prevent the impersonation from leaking to upstack exception filters. throw; } } return(module.Authenticate(null, credential, this, _client !.TargetName, null)); }
internal bool AuthSupported(ISmtpAuthenticationModule module) { if (module is SmtpLoginAuthenticationModule) { if ((this.supportedAuth & SupportedAuth.Login) > SupportedAuth.None) { return true; } } else if (module is SmtpNegotiateAuthenticationModule) { if ((this.supportedAuth & SupportedAuth.GSSAPI) > SupportedAuth.None) { this.sawNegotiate = true; return true; } } else if (module is SmtpNtlmAuthenticationModule) { if (!this.sawNegotiate && ((this.supportedAuth & SupportedAuth.NTLM) > SupportedAuth.None)) { return true; } } else if ((module is SmtpDigestAuthenticationModule) && ((this.supportedAuth & SupportedAuth.WDigest) > SupportedAuth.None)) { return true; } return false; }
private Authorization SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential credential, ContextAwareResult context) { if ((credential is SystemNetworkCredential) && ComNetOS.IsWinNt) { WindowsIdentity identity = (context == null) ? null : context.Identity; try { IDisposable disposable = (identity == null) ? null : identity.Impersonate(); if (disposable != null) { using (disposable) { return(module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken)); } } ExecutionContext executionContext = (context == null) ? null : context.ContextCopy; if (executionContext != null) { AuthenticateCallbackContext state = new AuthenticateCallbackContext(this, module, credential, this.client.TargetName, this.channelBindingToken); ExecutionContext.Run(executionContext, s_AuthenticateCallback, state); return(state.result); } return(module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken)); } catch { throw; } } return(module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken)); }
internal bool AuthSupported(ISmtpAuthenticationModule module) { if (module is SmtpLoginAuthenticationModule) { if ((_supportedAuth & SupportedAuth.Login) > 0) { return(true); } } else if (module is SmtpNegotiateAuthenticationModule) { if ((_supportedAuth & SupportedAuth.GSSAPI) > 0) { _sawNegotiate = true; return(true); } } else if (module is SmtpNtlmAuthenticationModule) { // Don't try ntlm if negotiate has been tried if ((!_sawNegotiate && (_supportedAuth & SupportedAuth.NTLM) > 0)) { return(true); } } return(false); }
internal SmtpConnection(SmtpTransport parent, SmtpClient client, ICredentialsByHost credentials, ISmtpAuthenticationModule[] authenticationModules) { this.client = client; this.credentials = credentials; this.authenticationModules = authenticationModules; this.parent = parent; this.onCloseHandler = new EventHandler(this.OnClose); }
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(); }
internal static void Register(ISmtpAuthenticationModule module) { ArgumentNullException.ThrowIfNull(module); lock (s_modules) { s_modules.Add(module); } }
internal static ISmtpAuthenticationModule[] GetModules() { lock (s_modules) { ISmtpAuthenticationModule[] copy = new ISmtpAuthenticationModule[s_modules.Count]; s_modules.CopyTo(0, copy, 0, s_modules.Count); return(copy); } }
/* // Consider removing. internal static void Unregister(ISmtpAuthenticationModule module) { if (module == null) throw new ArgumentNullException("module"); lock (modules) { modules.Remove(module); } } */ /* // Consider removing. internal static void Unregister(string authenticationType) { if (authenticationType == null) throw new ArgumentNullException("authenticationType"); lock (modules) { foreach (ISmtpAuthenticationModule module in modules) { if (0 == string.Compare(module.AuthenticationType, authenticationType, StringComparison.OrdinalIgnoreCase)) { modules.Remove(module); } } } } */ internal static ISmtpAuthenticationModule[] GetModules() { lock (modules) { ISmtpAuthenticationModule[] copy = new ISmtpAuthenticationModule[modules.Count]; modules.CopyTo(0, copy, 0, modules.Count); return copy; } }
internal SmtpConnection(SmtpTransport parent, SmtpClient client, ICredentialsByHost credentials, ISmtpAuthenticationModule[] authenticationModules) { _client = client; _credentials = credentials; _authenticationModules = authenticationModules; _parent = parent; _tcpClient = new TcpClient(); _onCloseHandler = new EventHandler(OnClose); }
internal AuthenticateCallbackContext(SmtpConnection thisPtr, ISmtpAuthenticationModule module, NetworkCredential credential, string spn, ChannelBinding Token) { this.thisPtr = thisPtr; this.module = module; this.credential = credential; this.spn = spn; this.token = Token; this.result = null; }
internal static ISmtpAuthenticationModule[] GetModules() { lock (modules) { ISmtpAuthenticationModule[] array = new ISmtpAuthenticationModule[modules.Count]; modules.CopyTo(0, array, 0, modules.Count); return(array); } }
internal static void Register(ISmtpAuthenticationModule module) { if (module == null) throw new ArgumentNullException("module"); lock (modules) { modules.Add(module); } }
internal AuthenticateCallbackContext(SmtpConnection thisPtr, ISmtpAuthenticationModule module, NetworkCredential credential, string?spn, ChannelBinding?Token) { _thisPtr = thisPtr; _module = module; _credential = credential; _spn = spn; _token = Token; _result = null; }
internal SmtpTransport(SmtpClient client, ISmtpAuthenticationModule[] authenticationModules) { this.timeout = 0x186a0; this.failedRecipientExceptions = new ArrayList(); this.client = client; if (authenticationModules == null) { throw new ArgumentNullException("authenticationModules"); } this.authenticationModules = authenticationModules; }
internal SmtpTransport(SmtpClient client, ISmtpAuthenticationModule[] authenticationModules) { this.client = client; if (authenticationModules == null) { throw new ArgumentNullException("authenticationModules"); } this.authenticationModules = authenticationModules; }
internal static void Register(ISmtpAuthenticationModule module) { if (module == null) { throw new ArgumentNullException("module"); } lock (modules) { modules.Add(module); } }
internal SmtpTransport(SmtpClient client, ISmtpAuthenticationModule[] authenticationModules) { _client = client; if (authenticationModules == null) { throw new ArgumentNullException(nameof(authenticationModules)); } _authenticationModules = authenticationModules; }
internal bool AuthSupported(ISmtpAuthenticationModule module) { if (module is SmtpLoginAuthenticationModule) { if ((_supportedAuth & SupportedAuth.Login) > 0) { return(true); } } return(false); }
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 Authorization SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential credential, ContextAwareResult context) { #if !FEATURE_PAL // We may need to restore user thread token here if (credential is SystemNetworkCredential) { // #if DEBUG GlobalLog.Assert(context == null || context.IdentityRequested, "SmtpConnection#{0}::SetContextAndTryAuthenticate|Authentication required when it wasn't expected. (Maybe Credentials was changed on another thread?)", ValidationHelper.HashString(this)); #endif WindowsIdentity w = context == null ? null : context.Identity; try { IDisposable ctx = w == null ? null : w.Impersonate(); if (ctx != null) { using (ctx) { return(module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken)); } } else { ExecutionContext x = context == null ? null : context.ContextCopy; if (x != null) { AuthenticateCallbackContext authenticationContext = new AuthenticateCallbackContext(this, module, credential, this.client.TargetName, this.channelBindingToken); ExecutionContext.Run(x, s_AuthenticateCallback, authenticationContext); return(authenticationContext.result); } else { return(module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken)); } } } catch { // Prevent the impersonation from leaking to upstack exception filters. throw; } } #endif // !FEATURE_PAL return(module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken)); }
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(); }
private Authorization SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential credential, ContextAwareResult context) { // We may need to restore user thread token here if (ReferenceEquals(credential, CredentialCache.DefaultNetworkCredentials)) { #if DEBUG if (context != null && !context.IdentityRequested) { if (GlobalLog.IsEnabled) { GlobalLog.AssertFormat("SmtpConnection#{0}::SetContextAndTryAuthenticate|Authentication required when it wasn't expected. (Maybe Credentials was changed on another thread?)", LoggingHash.HashString(this)); } Debug.Fail("SmtpConnection#" + LoggingHash.HashString(this) + "::SetContextAndTryAuthenticate|Authentication required when it wasn't expected. (Maybe Credentials was changed on another thread?)"); } #endif try { ExecutionContext x = context == null ? null : context.ContextCopy; if (x != null) { AuthenticateCallbackContext authenticationContext = new AuthenticateCallbackContext(this, module, credential, _client.TargetName, _channelBindingToken); ExecutionContext.Run(x, s_AuthenticateCallback, authenticationContext); return(authenticationContext._result); } else { return(module.Authenticate(null, credential, this, _client.TargetName, _channelBindingToken)); } } catch { // Prevent the impersonation from leaking to upstack exception filters. throw; } } return(module.Authenticate(null, credential, this, _client.TargetName, _channelBindingToken)); }
internal bool AuthSupported(ISmtpAuthenticationModule module) { if (module is SmtpLoginAuthenticationModule) { if ((supportedAuth & SupportedAuth.Login) > 0) { return(true); } } #if !FEATURE_PAL else if (module is SmtpNegotiateAuthenticationModule) { if ((supportedAuth & SupportedAuth.GSSAPI) > 0) { sawNegotiate = true; return(true); } } else if (module is SmtpNtlmAuthenticationModule) { //don't try ntlm if negotiate has been tried if ((!sawNegotiate && (supportedAuth & SupportedAuth.NTLM) > 0)) { return(true); } } else if (module is SmtpDigestAuthenticationModule) { if ((supportedAuth & SupportedAuth.WDigest) > 0) { return(true); } } #endif // FEATURE_PAL return(false); }
internal static void Register(ISmtpAuthenticationModule module !!) { lock (s_modules)
internal bool AuthSupported(ISmtpAuthenticationModule module) { if (module is SmtpLoginAuthenticationModule) { if ((_supportedAuth & SupportedAuth.Login) > 0) { return true; } } return false; }
internal AuthenticateCallbackContext(SmtpConnection thisPtr, ISmtpAuthenticationModule module, NetworkCredential credential, string spn, ChannelBinding Token) { _thisPtr = thisPtr; _module = module; _credential = credential; _spn = spn; _token = Token; _result = null; }
private Authorization SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential credential, ContextAwareResult context) { // We may need to restore user thread token here if (ReferenceEquals(credential, CredentialCache.DefaultNetworkCredentials)) { #if DEBUG if (context != null && !context.IdentityRequested) { if (GlobalLog.IsEnabled) { GlobalLog.AssertFormat("SmtpConnection#{0}::SetContextAndTryAuthenticate|Authentication required when it wasn't expected. (Maybe Credentials was changed on another thread?)", LoggingHash.HashString(this)); } Debug.Fail("SmtpConnection#" + LoggingHash.HashString(this) + "::SetContextAndTryAuthenticate|Authentication required when it wasn't expected. (Maybe Credentials was changed on another thread?)"); } #endif try { ExecutionContext x = context == null ? null : context.ContextCopy; if (x != null) { AuthenticateCallbackContext authenticationContext = new AuthenticateCallbackContext(this, module, credential, _client.TargetName, _channelBindingToken); ExecutionContext.Run(x, s_AuthenticateCallback, authenticationContext); return authenticationContext._result; } else { return module.Authenticate(null, credential, this, _client.TargetName, _channelBindingToken); } } catch { // Prevent the impersonation from leaking to upstack exception filters. throw; } } return module.Authenticate(null, credential, this, _client.TargetName, _channelBindingToken); }
internal bool AuthSupported(ISmtpAuthenticationModule module){ if (module is SmtpLoginAuthenticationModule) { if ((supportedAuth & SupportedAuth.Login) > 0) { return true; } } #if !FEATURE_PAL else if (module is SmtpNegotiateAuthenticationModule) { if ((supportedAuth & SupportedAuth.GSSAPI) > 0) { sawNegotiate = true; return true; } } else if (module is SmtpNtlmAuthenticationModule) { //don't try ntlm if negotiate has been tried if ((!sawNegotiate && (supportedAuth & SupportedAuth.NTLM) > 0)) { return true; } } else if (module is SmtpDigestAuthenticationModule) { if ((supportedAuth & SupportedAuth.WDigest) > 0) { return true; } } #endif // FEATURE_PAL return false; }
private Authorization SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential credential, ContextAwareResult context) { #if !FEATURE_PAL // We may need to restore user thread token here if (credential is SystemNetworkCredential) { // #if DEBUG GlobalLog.Assert(context == null || context.IdentityRequested, "SmtpConnection#{0}::SetContextAndTryAuthenticate|Authentication required when it wasn't expected. (Maybe Credentials was changed on another thread?)", ValidationHelper.HashString(this)); #endif WindowsIdentity w = context == null ? null : context.Identity; try { IDisposable ctx = w == null ? null : w.Impersonate(); if (ctx != null) { using (ctx) { return module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken); } } else { ExecutionContext x = context == null ? null : context.ContextCopy; if (x != null) { AuthenticateCallbackContext authenticationContext = new AuthenticateCallbackContext(this, module, credential, this.client.TargetName, this.channelBindingToken); ExecutionContext.Run(x, s_AuthenticateCallback, authenticationContext); return authenticationContext.result; } else { return module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken); } } } catch { // Prevent the impersonation from leaking to upstack exception filters. throw; } } #endif // !FEATURE_PAL return module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken); }
private Authorization SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential credential, ContextAwareResult context) { if ((credential is SystemNetworkCredential) && ComNetOS.IsWinNt) { WindowsIdentity identity = (context == null) ? null : context.Identity; try { IDisposable disposable = (identity == null) ? null : identity.Impersonate(); if (disposable != null) { using (disposable) { return module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken); } } ExecutionContext executionContext = (context == null) ? null : context.ContextCopy; if (executionContext != null) { AuthenticateCallbackContext state = new AuthenticateCallbackContext(this, module, credential, this.client.TargetName, this.channelBindingToken); ExecutionContext.Run(executionContext, s_AuthenticateCallback, state); return state.result; } return module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken); } catch { throw; } } return module.Authenticate(null, credential, this, this.client.TargetName, this.channelBindingToken); }
internal bool AuthSupported(ISmtpAuthenticationModule module) { return(module is SmtpLoginAuthenticationModule && (_supportedAuth & SupportedAuth.Login) > 0); }
internal SmtpConnection(ISmtpAuthenticationModule[] authenticationModules) { _authenticationModules = authenticationModules; }
internal bool AuthSupported(ISmtpAuthenticationModule module) { return module is SmtpLoginAuthenticationModule && (_supportedAuth & SupportedAuth.Login) > 0; }
internal bool AuthSupported(ISmtpAuthenticationModule module) { if (module is SmtpLoginAuthenticationModule) { if ((_supportedAuth & SupportedAuth.Login) > 0) { return true; } } else if (module is SmtpNegotiateAuthenticationModule) { if ((_supportedAuth & SupportedAuth.GSSAPI) > 0) { _sawNegotiate = true; return true; } } else if (module is SmtpNtlmAuthenticationModule) { // Don't try ntlm if negotiate has been tried if ((!_sawNegotiate && (_supportedAuth & SupportedAuth.NTLM) > 0)) { return true; } } return false; }