C#的WebRequest登录会话 [英] C# WebRequest Login Session

查看:213
本文介绍了C#的WebRequest登录会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我试着问这个问题,但昨天我不知道,如果我给了足够的信息,我得到了一个答案,但它并没有为我工作。用户打开这个窗口形式的应用程序和日志基本上我做的事情是,他们Afterwhich输入一些文本的文本框,然后点击运行。在这一点上运行功能正在作出的WebRequest到需要登录(它们打开程序后最初进行登录的服务器。出于某种原因它仍然没有看到该用户正在执行第二请求,即使当在记录饼干中添加过一个cookie的容器中。我不知道我做错了,但我会后我的代码,以便您可以进一步帮助我。

Okay I tried asking this question yesterday but i'm not sure if I gave enough info, i got an answer but it hasn't worked for me. Basically what i'm doing is the user opens this windows forms application and logs in. Afterwhich they enter some text into a textbox and click run. At this point the run function is making a webrequest to a server that requires a login (the login that is initially done after they open the program. For some reason its still not seeing that the user is logged in when performing the second request even though the cookies are added too a cookie container. I'm not sure what i'm doing wrong but I will post my code so you can further help me.

这是这是因为当用户进入应用程序的登录执行的功能。

This is the function that is performed for the login when the user enters the application.

private void button1_Click(object sender, EventArgs e)
{
    string paramaters = "authmethod=on&chkRememberMe=on&login-form-type=pwd&password=" + pw.Text + "&userid=" + uid.Text + "&username=" + uid.Text;
    string strResponse;
    HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://www.url.com/login.form");
    requestLogin.Method = "POST";
    requestLogin.CookieContainer = cookieJar;
    requestLogin.ContentType = "application/x-www-form-urlencoded";

    requestLogin.ContentLength = paramaters.Length;
    StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
    stOut.Write(paramaters);
    stOut.Close();

    HttpWebResponse responseLogin = (HttpWebResponse)requestLogin.GetResponse();
    StreamReader stIn = new StreamReader(responseLogin.GetResponseStream());
    strResponse = stIn.ReadToEnd();
    stIn.Close();

    //Add cookies to CookieJar (Cookie Container)
    foreach (Cookie cookie in responseLogin.Cookies)
    {
        cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
        richTextBox2.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
    }

    if (strResponse.Contains("Log On Successful") || strResponse.Contains("already has a webseal session"))
    {
        foreach (Control cont in this.Controls)
        {
            cont.Visible = true;
        }
        loginPanel.SendToBack();
        loginPanel.Visible = false;
    }
    else
    {
        MessageBox.Show("Login failed.");
    }
}

这是跑的功能,当用户点击运行按钮来启动对消费者账户测试。

This is the function that is ran when the user clicks the "run" button to initiate the tests on a consumer account.

private string runTestRequest(Uri url, string parameters)
{
    string testResults = string.Empty;
    HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create(url);
    runTest.CookieContainer = cookieJar;
    runTest.Method = "POST";
    runTest.ContentType = "application/x-www-form-urlencoded";
    StreamWriter stOut = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII);
    stOut.Write(parameters);
    stOut.Close();
    StreamReader stIn = new StreamReader(runTest.GetResponse().GetResponseStream());
    testResults = stIn.ReadToEnd();
    stIn.Close();
    return testResults;
}



当然,这是我的cookie容器对象

And of course this is my cookie container object

public CookieContainer cookieJar = new CookieContainer();



P.S:该webrequests的领域是不同的。首先是abc.com第二是123.com唯一的问题是,第一个域(也就是登录)是内部Web应用程序,如123.com全局登录,所以如何将我使用的登录会话从第1域第二域?

P.S.: The domains of the webrequests are different. First being abc.com 2nd being 123.com The only problem is that the first domain (which is the login) is a global login for internal web applications like 123.com, so how would i use the login session from the 1st domain with the 2nd domain?

您可以请协助帮助我找出我做错了。

Can you please assist in helping me figure out what I am doing wrong.

推荐答案

我发现发生了什么事是有人redircting在同一域作为第二(123.com)使用的登录的子域。显然,他们有建立在多个域这一全球登录系统传递的cookie。上面的代码不工作,我也有它现在的工作。谢谢!

I found out that what was happening was it was redircting to a subdomain on the same domain as the 2nd (123.com) to use the login. Evidently they had this global login system built on the multiple domains to pass the cookies. The code above DOES work and i do have it working now. Thanks!!

这篇关于C#的WebRequest登录会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆