/// <summary> /// Method responsible for checking the proxy and proxy authentication type and setting the /// appropriate credentials. If the NTLM authentication is used then /// if the username and password are not provided then we use null values. For /// all other authentication schemes we need a username and password. /// </summary> /// <param name="backgroundCopyJob">BackgroundJob on which we need to set the credentials.</param> /// <param name="task">DownloadTask. Provides the infos about download credentials</param> private static void VerifyAndSetBackgroundCopyJobProxy(IBackgroundCopyJob backgroundCopyJob, DownloadTask task) { // Specify the proxy URL // see also http://msdn.microsoft.com/library/en-us/bits/bits/ibackgroundcopyjob_setproxysettings.asp try { IWebProxy proxy = task.DownloadItem.Proxy; var sourceUri = new Uri(task.DownloadItem.File.Source); Uri proxyUri = proxy.GetProxy(sourceUri); //trim trailing '/' because it causes BITS to throw an exception string proxyUriStr = proxyUri.ToString().TrimEnd('/'); if (!proxy.IsBypassed(proxyUri)) { backgroundCopyJob.SetProxySettings(BG_JOB_PROXY_USAGE.BG_JOB_PROXY_USAGE_OVERRIDE, proxyUriStr, null); } //specify proxy credentials if (proxy.Credentials != null) { ICredentials creds = proxy.Credentials; var copyJob = (IBackgroundCopyJob2)backgroundCopyJob; var credentials = new BG_AUTH_CREDENTIALS(); credentials.Credentials.Basic.UserName = string.IsNullOrEmpty(creds.GetCredential(sourceUri, "NTLM").Domain) ? creds.GetCredential(sourceUri, "NTLM").UserName : creds.GetCredential(sourceUri, "NTLM").Domain + "\\" + creds.GetCredential(sourceUri, "NTLM").UserName; credentials.Credentials.Basic.Password = creds.GetCredential(sourceUri, "NTLM").Password; credentials.Scheme = BG_AUTH_SCHEME.BG_AUTH_SCHEME_NTLM; credentials.Target = BG_AUTH_TARGET.BG_AUTH_TARGET_PROXY; copyJob.SetCredentials(ref credentials); } } catch (Exception e) { Logger.Error("Error in VerifyAndSetBackgroundCopyJobProxy():", e); } }