Beispiel #1
0
        /// <summary>
        /// Use the SetCredentials method to specify the credentials to use for a proxy or remote server user authentication request.
        /// </summary>
        /// <param name="cred">Identifies the user's credentials to use for user authentication.</param>
        public void SetCredentials(System.Net.NetworkCredential cred)
        {
            ReqJob2();
            BG_AUTH_CREDENTIALS ac = new BG_AUTH_CREDENTIALS();

            ac.Target = BG_AUTH_TARGET.BG_AUTH_TARGET_SERVER;
            if (string.IsNullOrEmpty(cred.Domain))
            {
                if (!string.IsNullOrEmpty(cred.UserName))
                {
                    ac.Scheme = BG_AUTH_SCHEME.BG_AUTH_SCHEME_BASIC;
                    ac.Credentials.Basic.UserName = cred.UserName;
                    ac.Credentials.Basic.Password = cred.Password;
                }
                else
                {
                    ac.Scheme = BG_AUTH_SCHEME.BG_AUTH_SCHEME_NTLM;
                }
            }
            else
            {
                ac.Scheme = BG_AUTH_SCHEME.BG_AUTH_SCHEME_NTLM;
                ac.Credentials.Basic.UserName = string.Concat(cred.Domain, '\\', cred.UserName);
                ac.Credentials.Basic.Password = cred.Password;
            }
            try
            {
                m_ijob2.SetCredentials(ref ac);
            }
            catch (COMException cex)
            {
                HandleCOMException(cex);
            }
        }
        internal BG_AUTH_CREDENTIALS GetNative()
        {
            var cr = new BG_AUTH_CREDENTIALS {
                Scheme = (BG_AUTH_SCHEME)Scheme, Target = (BG_AUTH_TARGET)Target
            };

            cr.Credentials.Basic.UserName = UserName;
            cr.Credentials.Basic.Password = Password;
            return(cr);
        }
Beispiel #3
0
        /// <summary>
        /// Method responsible for checking the 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 VerifyAndSetBackgroundCopyJobCredentials(IBackgroundCopyJob backgroundCopyJob, DownloadTask task)
        {
            try
            {
                var          copyJob = (IBackgroundCopyJob2)backgroundCopyJob;
                ICredentials creds   = task.DownloadItem.Credentials;
                var          uri     = new Uri(task.DownloadItem.File.Source);

                if (creds != null)
                {
                    //Specify HTTP Authentication (Basic) credentials
                    var credentials = new BG_AUTH_CREDENTIALS();
                    credentials.Credentials.Basic.UserName = creds.GetCredential(uri, "Basic").UserName;
                    credentials.Credentials.Basic.Password = creds.GetCredential(uri, "Basic").Password;
                    credentials.Scheme = BG_AUTH_SCHEME.BG_AUTH_SCHEME_BASIC;
                    credentials.Target = BG_AUTH_TARGET.BG_AUTH_TARGET_SERVER;
                    copyJob.SetCredentials(ref credentials);

                    //Specify HTTP Authentication (Digest) credentials
                    credentials = new BG_AUTH_CREDENTIALS();
                    credentials.Credentials.Basic.UserName = creds.GetCredential(uri, "Digest").UserName;
                    credentials.Credentials.Basic.Password = creds.GetCredential(uri, "Digest").Password;
                    credentials.Scheme = BG_AUTH_SCHEME.BG_AUTH_SCHEME_DIGEST;
                    credentials.Target = BG_AUTH_TARGET.BG_AUTH_TARGET_SERVER;
                    copyJob.SetCredentials(ref credentials);

                    //Specify NTLM credentials
                    credentials = new BG_AUTH_CREDENTIALS();
                    credentials.Credentials.Basic.UserName =
                        string.IsNullOrEmpty(creds.GetCredential(uri, "NTLM").Domain)
                            ?
                        creds.GetCredential(uri, "NTLM").UserName
                            :
                        creds.GetCredential(uri, "NTLM").Domain + "\\" +
                        creds.GetCredential(uri, "NTLM").UserName;
                    credentials.Credentials.Basic.Password = creds.GetCredential(uri, "NTLM").Password;
                    credentials.Scheme = BG_AUTH_SCHEME.BG_AUTH_SCHEME_NTLM;
                    credentials.Target = BG_AUTH_TARGET.BG_AUTH_TARGET_SERVER;
                    copyJob.SetCredentials(ref credentials);
                } //if(creds != null)
            }
            catch (Exception e)
            {
                Logger.Error("Error in VerifyAndSetBackgroundCopyJobCredentials():", e);
            }
        }
Beispiel #4
0
 public void AddCredentials(BitsCredentials credentials)
 {
     try
     {
         if (job2 != null)   // only supported from IBackgroundCopyJob2 and above
         {
             BG_AUTH_CREDENTIALS bgCredentials = new BG_AUTH_CREDENTIALS();
             bgCredentials.Scheme = (BG_AUTH_SCHEME)credentials.AuthenticationScheme;
             bgCredentials.Target = (BG_AUTH_TARGET)credentials.AuthenticationTarget;
             bgCredentials.Credentials.Basic.Password = credentials.Password.ToString();
             bgCredentials.Credentials.Basic.UserName = credentials.UserName.ToString();
             job2.SetCredentials(ref bgCredentials);
         }
     }
     catch (COMException exception)
     {
         manager.PublishException(this, exception);
     }
 }
Beispiel #5
0
        /// <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);
            }
        }