Exemplo n.º 1
0
        /// <summary>
        /// Checks Url resource for access. Looks in cache, tries windows cache, and then prompts.
        /// Returns CommandResults.Successful on success.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="force">if set to <c>true</c> [force].</param>
        /// <param name="credentials">The credentials.</param>
        /// <returns>CommandResults.</returns>
        private CommandResults CheckResourceCredentialsUrl(string path, bool force, NetworkCredential credentials = null)
        {
            HttpGet httpGet = new HttpGet();
            string  result  = string.Empty;

            try
            {
                string uriHost = new Uri(path).Host;
                CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUri:treating as HTTP");

                if (CredentialsList.ContainsKey(uriHost) && !force)
                {
                    CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUri:http skipping credentials");
                    return(CredentialsList[uriHost].Status);
                }

                if (CredentialsList.ContainsKey(Resources.SessionName))
                {
                    CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUrl:trying utility credentials");
                    result = httpGet.GetRequest(path, CredentialsList[Resources.SessionName], true);

                    // try with utility credentials if available
                    if (CheckUrlReturnCode(httpGet.Status))
                    {
                        return(AddToCredentialList(CredentialsList[Resources.SessionName], uriHost));
                    }
                }

                if (CredentialsList.ContainsKey(uriHost) && CanPromptForCredentials())
                {
                    // Try with cached creds
                    httpGet = new HttpGet();

                    result = httpGet.GetRequest(path, CredentialsList[uriHost], true);

                    if (!CheckUrlReturnCode(httpGet.Status))
                    {
                        // this queries windows credential manager for cached credential
                        credentials = Credentials.PromptForCredentials(credentials.UserName, credentials.Password,
                                                                       uriHost);
                        CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUri:Error:" + httpGet.Status);

                        result = httpGet.GetRequest(path, credentials, true);
                        if (!CheckUrlReturnCode(httpGet.Status))
                        {
                            CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUri2:Error:" + httpGet.Status);
                            return(AddToCredentialList(credentials, uriHost, false, CommandResults.Fail));
                        }
                        else
                        {
                            return(AddToCredentialList(credentials, uriHost));
                        }
                    }
                    else
                    {
                        return(AddToCredentialList(credentials, uriHost));
                    }
                }

                CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUri:http using default credentials");
                httpGet = new HttpGet();
                result  = httpGet.GetRequest(path, CredentialCache.DefaultNetworkCredentials, true);

                if (!CheckUrlReturnCode(httpGet.Status) && CanPromptForCredentials())
                {
                    // this queries windows credential manager for cached credential
                    CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUri:Error:" + httpGet.Status);
                    credentials = Credentials.PromptForCredentials(credentials.UserName, credentials.Password,
                                                                   uriHost);
                    result = httpGet.GetRequest(path, credentials, true);

                    if (!CheckUrlReturnCode(httpGet.Status))
                    {
                        CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUri:Error2:" + httpGet.Status);
                        return(AddToCredentialList(credentials, uriHost, false, CommandResults.Fail));
                    }
                    else
                    {
                        return(AddToCredentialList(credentials, uriHost));
                    }
                }
                else
                {
                    return(AddToCredentialList(CredentialCache.DefaultNetworkCredentials, uriHost, true,
                                               CommandResults.Successful));
                }
            }
            catch (Exception e)
            {
                CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUrl:Exception:" + e.ToString());
                return(AddToCredentialList(credentials, path, false, CommandResults.Fail));
            }
        }