Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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# 登录到使用Live.com身份验证的网站_C#_Http_Authentication_Https - Fatal编程技术网

C# 登录到使用Live.com身份验证的网站

C# 登录到使用Live.com身份验证的网站,c#,http,authentication,https,C#,Http,Authentication,Https,我一直在尝试自动登录我经常访问的网站www.bungie.net。该网站与Microsoft和Xbox Live相关联,因此在用户登录其网站时使用Windows Live ID API 我对创建网络蜘蛛/机器人比较陌生,我担心我误解了一些最基本的概念。我曾模拟登录Facebook和Gmail等其他网站,但live.com给我带来的只是麻烦 不管怎样,我一直在使用Wireshark和Firefox插件篡改数据,试图找出我需要发布什么,以及我需要在请求中包含哪些cookies。据我所知,这些是登录此

我一直在尝试自动登录我经常访问的网站www.bungie.net。该网站与Microsoft和Xbox Live相关联,因此在用户登录其网站时使用Windows Live ID API

我对创建网络蜘蛛/机器人比较陌生,我担心我误解了一些最基本的概念。我曾模拟登录Facebook和Gmail等其他网站,但live.com给我带来的只是麻烦

不管怎样,我一直在使用Wireshark和Firefox插件篡改数据,试图找出我需要发布什么,以及我需要在请求中包含哪些cookies。据我所知,这些是登录此站点必须遵循的步骤

1.访问https://login.live.com/login.srf?wa=wsignn1.0&rpsnv=11&ct=1268167141&rver=5.5.4177.0&wp=LBI&wreply=http://2F%2Fwww.bungie.net%2ffault.aspx&id=42917

2.接收饼干MSPRequ和MSPOK

3.将表单ID“PPSX”中的值、表单ID“PPFT”中的值、用户名和密码全部发布到一个不断变化的URL,类似于:https://login.live.com/ppsecure/Post.srf?wa=wsignin1.0&rpsnv=11&ct= (该URL末尾有几个数字会发生变化)

4.Live.com会向用户返回一个包含更多隐藏表单的页面。然后,客户端将来自“ANON”表单的值、来自“ANONExp”表单的值和来自“t”表单的值发布到URL:http://www.bung ie.net/Default.aspx?wa=wsignin1.0

5.发布数据后,用户会收到各种cookie,其中最重要的是“BNGAuth”,这是站点的登录cookie

我遇到的问题是在第五步,但这并不一定意味着我已经正确地完成了所有其他步骤。我发布了来自“ANON”、“ANONExp”和“t”的数据,但返回的不是BNGAuth cookie,而是名为“RSPMaybe”的cookie,并重定向到主页

当我查看Wireshark日志时,我注意到,在我使用Firefox登录时的日志和我的程序运行时的日志之间,有一点瞬间让我感到与众不同。它可能是什么,但我会包括在这里的图片供您审查。在第四步发布数据之前,我从站点返回了一个HTTP数据包。我不确定这是怎么发生的,但这一定是我在HTTPS步骤中做错了什么的副作用

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Net;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Web;

namespace SpiderFromScratch
{
    class Program
    {   
        static void Main(string[] args)
        {
            CookieContainer cookies = new CookieContainer();
            Uri url = new Uri("https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1268167141&rver=5.5.4177.0&wp=LBI&wreply=http:%2F%2Fwww.bungie.net%2FDefault.aspx&id=42917");
            HttpWebRequest http = (HttpWebRequest)HttpWebRequest.Create(url);

            http.Timeout = 30000;
            http.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 (.NET CLR 3.5.30729)";
            http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            http.Headers.Add("Accept-Language", "en-us,en;q=0.5");
            http.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            http.Headers.Add("Keep-Alive", "300");
            http.Referer = "http://www.bungie.net/";
            http.ContentType = "application/x-www-form-urlencoded";
            http.CookieContainer = new CookieContainer();
            http.Method = WebRequestMethods.Http.Get;

            HttpWebResponse response = (HttpWebResponse)http.GetResponse();
            StreamReader readStream = new StreamReader(response.GetResponseStream());
            string HTML = readStream.ReadToEnd();
            readStream.Close();

            //gets the cookies (they are set in the eighth header)
            string[] strCookies = response.Headers.GetValues(8);
            response.Close();

            string name, value;
            Cookie manualCookie;
            for (int i = 0; i < strCookies.Length; i++)
            {
                name = strCookies[i].Substring(0, strCookies[i].IndexOf("="));
                value = strCookies[i].Substring(strCookies[i].IndexOf("=") + 1, strCookies[i].IndexOf(";") - strCookies[i].IndexOf("=") - 1);
                manualCookie = new Cookie(name, "\"" + value + "\"");

                Uri manualURL = new Uri("http://login.live.com");
                http.CookieContainer.Add(manualURL, manualCookie);
            }


            //stores the cookies to be used later
            cookies = http.CookieContainer;

            //Get the PPSX value
            string PPSX = HTML.Remove(0, HTML.IndexOf("PPSX"));
            PPSX = PPSX.Remove(0, PPSX.IndexOf("value") + 7);
            PPSX = PPSX.Substring(0, PPSX.IndexOf("\""));

            //Get this random PPFT value
            string PPFT = HTML.Remove(0, HTML.IndexOf("PPFT"));
            PPFT = PPFT.Remove(0, PPFT.IndexOf("value") + 7);
            PPFT = PPFT.Substring(0, PPFT.IndexOf("\""));

            //Get the random URL you POST to
            string POSTURL = HTML.Remove(0, HTML.IndexOf("https://login.live.com/ppsecure/post.srf?wa=wsignin1.0&rpsnv=11&ct="));
            POSTURL = POSTURL.Substring(0, POSTURL.IndexOf("\""));


            //POST with cookies
            http = (HttpWebRequest)HttpWebRequest.Create(POSTURL);

            http.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 (.NET CLR 3.5.30729)";
            http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            http.Headers.Add("Accept-Language", "en-us,en;q=0.5");
            http.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            http.Headers.Add("Keep-Alive", "300");
            http.CookieContainer = cookies;
            http.Referer = "https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1268158321&rver=5.5.4177.0&wp=LBI&wreply=http:%2F%2Fwww.bungie.net%2FDefault.aspx&id=42917";
            http.ContentType = "application/x-www-form-urlencoded";
            http.Method = WebRequestMethods.Http.Post;

            Stream ostream = http.GetRequestStream();

            //used to convert strings into bytes
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

            //Post information
            byte[] buffer = encoding.GetBytes("PPSX=" + PPSX +"&PwdPad=IfYouAreReadingThisYouHaveTooMuc&login=YOUREMAILGOESHERE&passwd=YOURWORDGOESHERE" +
            "&LoginOptions=2&PPFT=" + PPFT);
            ostream.Write(buffer, 0, buffer.Length);
            ostream.Close();

            HttpWebResponse response2 = (HttpWebResponse)http.GetResponse();
            readStream = new StreamReader(response2.GetResponseStream());
            HTML = readStream.ReadToEnd();

            response2.Close();
            ostream.Dispose();
            foreach (Cookie cookie in response2.Cookies)
            {
                Console.WriteLine(cookie.Name + ": ");
                Console.WriteLine(cookie.Value);
                Console.WriteLine(cookie.Expires);
                Console.WriteLine();
            }

            //SET POSTURL value
            string POSTANON = "http://www.bungie.net/Default.aspx?wa=wsignin1.0";

            //Get the ANON value
            string ANON = HTML.Remove(0, HTML.IndexOf("ANON"));
            ANON = ANON.Remove(0, ANON.IndexOf("value") + 7);
            ANON = ANON.Substring(0, ANON.IndexOf("\""));
            ANON = HttpUtility.UrlEncode(ANON);

            //Get the ANONExp value
            string ANONExp = HTML.Remove(0, HTML.IndexOf("ANONExp"));
            ANONExp = ANONExp.Remove(0, ANONExp.IndexOf("value") + 7);
            ANONExp = ANONExp.Substring(0, ANONExp.IndexOf("\""));
            ANONExp = HttpUtility.UrlEncode(ANONExp);

            //Get the t value
            string t = HTML.Remove(0, HTML.IndexOf("id=\"t\""));
            t = t.Remove(0, t.IndexOf("value") + 7);
            t = t.Substring(0, t.IndexOf("\""));
            t = HttpUtility.UrlEncode(t);

            //POST the Info and Accept the Bungie Cookies
            http = (HttpWebRequest)HttpWebRequest.Create(POSTANON);

            http.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8 (.NET CLR 3.5.30729)";
            http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            http.Headers.Add("Accept-Language", "en-us,en;q=0.5");
            http.Headers.Add("Accept-Encoding", "gzip,deflate");
            http.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            http.Headers.Add("Keep-Alive", "115");
            http.CookieContainer = new CookieContainer();
            http.ContentType = "application/x-www-form-urlencoded";
            http.Method = WebRequestMethods.Http.Post;

            http.Expect = null;

            ostream = http.GetRequestStream();
            int test = ANON.Length;
            int test1 = ANONExp.Length;
            int test2 = t.Length;
            buffer = encoding.GetBytes("ANON=" + ANON +"&ANONExp=" + ANONExp + "&t=" + t);
            ostream.Write(buffer, 0, buffer.Length);
            ostream.Close();

            //Here lies the problem, I am not returned the correct cookies.
            HttpWebResponse response3 = (HttpWebResponse)http.GetResponse();
            GZipStream gzip = new GZipStream(response3.GetResponseStream(), CompressionMode.Decompress);
            readStream = new StreamReader(gzip);
            HTML = readStream.ReadToEnd();

            //gets both cookies
            string[] strCookies2 = response3.Headers.GetValues(11);

            response3.Close();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.Specialized;
使用系统文本;
Net系统;
使用System.IO;
使用系统IO压缩;
使用System.Security.Cryptography;
使用System.Security.Cryptography.X509证书;
使用System.Web;
命名空间SpiderFromScratch
{
班级计划
{   
静态void Main(字符串[]参数)
{
CookieContainer cookies=新CookieContainer();
Uri url=新的Uri(“https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=1268167141&rver=5.5.4177.0&wp=LBI&wreply=http:%2F%2Fwww.bungie.net%2FDefault.aspx&id=42917");
HttpWebRequest http=(HttpWebRequest)HttpWebRequest.Create(url);
http.Timeout=30000;
http.UserAgent=“Mozilla/5.0(Windows;U;Windows NT 5.1;en-US;rv:1.9.1.8)Gecko/20100202 Firefox/3.5.8(.NET CLR 3.5.30729)”;
http.Accept=“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”;
Add(“接受语言”,“en-us,en;q=0.5”);
添加(“接受字符集”、“ISO-8859-1,utf-8;q=0.7,*;q=0.7”);
添加(“保持活动”、“300”);
http.Referer=”http://www.bungie.net/";
http.ContentType=“application/x-www-form-urlencoded”;
http.CookieContainer=新的CookieContainer();
http.Method=WebRequestMethods.http.Get;
HttpWebResponse=(HttpWebResponse)http.GetResponse();
StreamReader readStream=新的StreamReader(response.GetResponseStream());
字符串HTML=readStream.ReadToEnd();
readStream.Close();
//获取cookies(它们在第八个标头中设置)
string[]strCookies=response.Headers.GetValues(8);
response.Close();
字符串名称、值;
饼干;
for(int i=0;i