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)); }
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)); }
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) { // 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)); }
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); }
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); }
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); }