简体   繁体   English

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

[英]Programatiically login to a website that uses IIS authentication

Hi all am trying to login to a website programmatically.I have already worked on this but is was a php page and i used the below code (found some where on Stack Overflow) to login to it and it worked great. 大家好,我正在尝试以编程方式登录网站。我已经在处理此问题,但是这是一个php页面,我使用了下面的代码(在Stack Overflow上的某些位置找到了)来登录它,效果很好。

 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;
            }

        }

Where access_login & access_password is the name of the input box that accepts the credentials.I have no clue how to implement it for a iis Login prompt as shown below.Please help 其中access_login和access_password是接受凭据的输入框的名称,我不知道如何针对iis登录提示实现它,如下所示。

在此处输入图片说明

If you can login through a browser and use Fiddler to intercept the request, you can use the Fiddler add-on Request-To-Code to generate C# code that will perform the request. 如果您可以通过浏览器登录并使用Fiddler拦截请求,则可以使用Fiddler附加组件的Request-To-Code生成将执行请求的C#代码。 The generated code would probably be a good place for you to start at least. 生成的代码至少可能是您入门的好地方。

I have successfully used Fiddler with Request-To-Code in similar situations. 在类似情况下,我已经成功地将Fiddler与Request-To-Code一起使用。

Try using HttpWebRequest class and NetworkCredentials classes to accomplish this. 尝试使用HttpWebRequest类和NetworkCredentials类来完成此操作。

Here is a piece of code that should get you in right direction. 这是一段代码,可以帮助您朝正确的方向发展。

// 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM