Exemple #1
0
        public bool validURLAccess(System.String sURL, System.String sUserName, System.String sUserPass)
        {
            try
            {
                string     strResponse = string.Empty;
                string     strMethod   = "GET";
                System.Uri requestURL  = new Uri(sURL);
                // Chequear si es una solicitud FTP
                System.Net.WebRequest  WRequest;
                System.Net.WebResponse WResponse;

                /*
                 * if(sURL.Substring(0,6).ToLower()=="ftp://")
                 * {
                 *      FtpRequestCreator Creator = new FtpRequestCreator();
                 *      System.Net.WebRequest.RegisterPrefix("ftp:", Creator);
                 *      strMethod = "dir";
                 * }
                 */

                WRequest        = System.Net.WebRequest.Create(sURL);
                WRequest.Method = strMethod;

                if (sUserName != string.Empty)              // we will add the user and password for basic auth.
                {
                    System.Net.NetworkCredential myCred             = new System.Net.NetworkCredential(sUserName, sUserPass);
                    System.Net.CredentialCache   MyCrendentialCache = new System.Net.CredentialCache();
                    MyCrendentialCache.Add(requestURL, "Basic", myCred);
                    WRequest.Credentials = MyCrendentialCache;
                }
                else                 //Set the default Credentials. This will allow NTLM or Kerbeors authentication with out prompting the user
                {
                    // the default credentials are usually the Windows credentials (user name, password, and domain) of the user running the application
                    WRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
                }

                WResponse = (System.Net.WebResponse)WRequest.GetResponse();
                System.IO.StreamReader sr = new System.IO.StreamReader(WResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("iso-8859-1"));
                //Convert the stream to a string
                strResponse = sr.ReadToEnd();
                System.Diagnostics.Debug.WriteLine(strResponse);
                sr.Close();
                return(true);
            }

            catch (System.Exception Ex)
            {
                System.Diagnostics.Debug.WriteLine(Ex.ToString());
                throw new System.Exception(Ex.Message.ToString(), Ex);
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        /// <param name="domain">域</param>
        /// <param name="loginUrl">登录地址</param>
        /// <param name="IsLogin">不使用缓存,重新登录</param>
        public void LogLan(string userName, string passWord, string domain, string loginUrl = "", bool IsLogin = false)
        {
            if (string.IsNullOrEmpty(loginUrl))
            {
                loginUrl = LoginUrl;
            }
            else
            {
                LoginUrl = loginUrl;
            }

            HttpWebRequest  WRequest;
            HttpWebResponse WResponse;

            if (IsLogin)
            {
                myCredCache = null;
            }
            if (myCredCache == null)
            {
                myCredCache = new CredentialCache();
            }
            if (myCredCache.GetCredential(new Uri(LoginUrl), "NTLM") == null)
            {
                myCredCache.Add(new Uri(LoginUrl), "NTLM", new NetworkCredential(userName, passWord, domain));
                // Pre-authenticate the request.
                WRequest = (HttpWebRequest)HttpWebRequest.Create(LoginUrl);
                // Set the username and the password.
                WRequest.Credentials = myCredCache;
                // This property must be set to true for Kerberos authentication.

                // Keep the connection alive.

                WRequest.UserAgent = "Upload Test";
                WRequest.Method    = "HEAD";
                WRequest.Timeout   = 10000;
                WResponse          = (HttpWebResponse)WRequest.GetResponse();
                WResponse.Close();
            }
        }