gpt4 book ai didi

c# - 如何使用 C# 登录到 ASP.Net Forms Authenticated 站点?

转载 作者:行者123 更新时间:2023-11-30 18:40:39 25 4
gpt4 key购买 nike

我正在尝试对使用 ASP.Net Forms Authentication 的网站进行屏幕抓取。我有以下代码,它发出 Get 获取 cookie 的请求,然后我想将登录信息与我刚刚到达 login.aspx 页面的 cookie 一起发布。

当我使用 Fiddler 观看提交到 login.aspx 页面的内容时,我看到了已发布的数据,但对于提交的 cookie,我看到了消息 ”,但它说“此请求未发送任何 cookie 数据。”

如果我使用 Internet Explorer 登录应用程序,我可以看到 cookie 和发布的数据已提交到 login.aspx 页面并且一切正常。

但是如果我循环遍历,我可以打印出我假设应该使用此 block 与请求一起发送的 cookie

   foreach (System.Net.Cookie cook in getCookies.CookieContainer.GetCookies(new Uri("https://app.example.com")))
{
Console.WriteLine(cook.Name + ": " + cook.Value);
}

我做错了什么导致 cookie 没有随我的请求一起发送?

    public void Login()
{
HttpWebRequest getCookies = (HttpWebRequest)WebRequest.Create("https://app.example.com");

CookieContainer cookieJar = new CookieContainer();
getCookies.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-xpsdocument, application/x-ms-application, application/x-ms-xbap, application/xaml+xml, */*";
getCookies.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0E; .NET4.0C)";
getCookies.Headers.Add("Accept-Encoding", "gzip, deflate");
getCookies.AllowAutoRedirect = true;

getCookies.CookieContainer = cookieJar;
using (HttpWebResponse cookieResponse = getCookies.GetResponse() as HttpWebResponse)
{
StreamReader responseReader = new StreamReader(cookieResponse.GetResponseStream());
string responseData = responseReader.ReadToEnd();

string ViewState = this.GetViewStateFromHtml(responseData);

getCookies = (HttpWebRequest)WebRequest.Create("https://app.example.com/Membership/Login.aspx?ReturnUrl=%2fHome%2fHomeSummary.aspx");
getCookies.Method = "Post";
getCookies.ContentType = "application/x-www-form-urlencoded";
getCookies.Accept = "text/html, application/xhtml+xml. */*";
getCookies.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0E; .NET4.0C)";
getCookies.Headers.Add("Accept-Encoding", "gzip, deflate");
getCookies.AllowAutoRedirect = true;

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] byte1 = encoding.GetBytes(this.GetPostData(Username, Password));

getCookies.ContentLength = byte1.Length;

StreamWriter requestWriter = new StreamWriter(getCookies.GetRequestStream());
requestWriter.Write(this.GetPostData(Username, Password));
requestWriter.Close();
getCookies.CookieContainer = cookieJar;

foreach (System.Net.Cookie cook in getCookies.CookieContainer.GetCookies(new Uri("https://app.example.com")))
{
Console.WriteLine(cook.Name + ": " + cook.Value);
}

using (HttpWebResponse postResponse = (HttpWebResponse)getCookies.GetResponse())
{
StreamReader postLoginResponseReader = new StreamReader(postResponse.GetResponseStream());
string postLoginResponseData = postLoginResponseReader.ReadToEnd();
File.WriteAllText(@"C:\postLoginResponse.txt", postLoginResponseData);
}
Console.WriteLine(@"Check C:\postLoginResponse.txt");
Console.ReadLine();
}

最佳答案

我看不到您在哪里将表单例份验证 cookie 添加到 HttpWebRequest cookie 容器。

我以前做过这个

var request = (HttpWebRequest) WebRequest.Create(remoteFilename);
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(new Cookie(".ASPXAUTH", "71AE9C8F6CFDC86BD7FD3AD7B214C4E1", "/", "build.mercaridirect.com.au"));

次要点您应该将变量的名称 getCookies 更改为 webRequest 或类似名称,以提高可读性。

关于c# - 如何使用 C# 登录到 ASP.Net Forms Authenticated 站点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7881816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com