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)
{
NetEventSource.Fail(this, "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);
}