Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式登录到使用IIS身份验证的网站_C#_Asp.net_Authentication - Fatal编程技术网

C# 以编程方式登录到使用IIS身份验证的网站

C# 以编程方式登录到使用IIS身份验证的网站,c#,asp.net,authentication,C#,Asp.net,Authentication,大家好,我正在尝试以编程方式登录一个网站。我已经完成了这项工作,但它是一个php页面,我使用了下面的代码(在堆栈溢出中找到了一些地方)登录到它,效果非常好 private static string GetDataFromPHP(string formUrl, string getUrl, string username, string password, out bool status) { try {

大家好,我正在尝试以编程方式登录一个网站。我已经完成了这项工作,但它是一个php页面,我使用了下面的代码(在堆栈溢出中找到了一些地方)登录到它,效果非常好

 private static string GetDataFromPHP(string formUrl, string getUrl, string username, string password, out bool status)
        {

            try
            {

                string formParams = string.Format("access_login={0}&access_password={1}", username, password);
                string cookieHeader;
                WebRequest req = WebRequest.Create(formUrl);
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                byte[] bytes = Encoding.ASCII.GetBytes(formParams);
                req.ContentLength = bytes.Length;
                using (Stream os = req.GetRequestStream())
                {
                    os.Write(bytes, 0, bytes.Length);
                }

                WebResponse resp = req.GetResponse();
                cookieHeader = resp.Headers["Set-cookie"];
                string pageSource;

                WebRequest getRequest = WebRequest.Create(getUrl);
                getRequest.Headers.Add("Cookie", cookieHeader);
                WebResponse getResponse = getRequest.GetResponse();
                using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
                {
                    pageSource = sr.ReadToEnd();
                }
                status = true;

                return pageSource;
            }



            catch (System.Exception ex)
            {
                status = false;
                return string.Empty;
            }

        }
其中access\u login&access\u password是接受凭据的输入框的名称。我不知道如何在iis登录提示中实现它,如下所示。请帮助


如果您可以通过浏览器登录并使用来拦截请求,则可以使用Fiddler插件生成执行请求的C#代码。生成的代码可能至少是您开始工作的好地方


在类似的情况下,我已经成功地将Fiddler与Request-To-Code结合使用。

尝试使用HttpWebRequest类和NetworkCredentials类来完成此任务

下面是一段代码,它应该能让您朝着正确的方向前进

// Create Request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.yourdomain/whatever");

// Create Client
WebClient client = new WebClient();

// Assign Credentials
client.Credentials = new NetworkCredential("user", "password");

// Grab Data
string htmlCode = client.DownloadString("http://www.yourdomain/whatever");
它抛出(500)个内部服务器错误。