【问题标题】:C# Winforms Application and FormsAuthenticationC# Winforms 应用程序和 FormsAuthentication
【发布时间】:2015-03-17 14:05:17
【问题描述】:

我有一个 WinForms 应用程序(只是一个尝试获取 Default.aspx 页面的按钮)和一个带有 FormsAuthentication(Logon.aspx 和 Default.aspx)的网站

这是我的两个代码:

https://www.dropbox.com/s/40qib4a9gynrs00/test.zip?dl=0

我正在尝试使用 CookieContainer 类向网站验证自己的身份...但它不起作用...

你能检查我哪里错了吗?我的意思是,我可能已经有 5 天了……我真的找不到答案。谢谢!

编辑:

我有这部分代码,但不起作用:

        private void button1_Click(object sender, EventArgs e)
    {
        using (var client = new CookieWebClient())
        {
            var values = new NameValueCollection { { "user", "admin" }, { "password", "cool" } };

            string result1 = client.DownloadString("http://localhost:49689/Default.aspx"); //result1  : Redirect to Logon.aspx
            client.UploadValues("http://localhost:49689/Logon.aspx", values);
            string result = client.DownloadString(new Uri("http://localhost:49689/Default.aspx"));
            MessageBox.Show(result); //All the DefaultPage (if that works), but of course it doesnt works...
        }
    }

在结果字符串上,您可以看到重定向到 Logon.aspx,因为身份验证不起作用。请帮忙^^!

【问题讨论】:

  • @GrantWinney 你说得对,格兰特;)。我只是希望一些很酷的灵魂可以帮助我调试这个烂摊子...... ^^
  • 是的!但在我身边是我第一次面对这些问题... :/ 希望我能尽快解决它们^^' !

标签: c# forms-authentication webclient


【解决方案1】:

您可以使用以下代码使用 WebBrowser 组件控制登录:

    private void button1_Click(object sender, EventArgs e)
    {
        var wb = new WebBrowser
        {
            ScriptErrorsSuppressed = true
        };

        wb.Navigate("http://localhost:49689/Logon.aspx");
        while (wb.ReadyState != WebBrowserReadyState.Complete)
            Application.DoEvents();

        wb.Document.GetElementById("user").InnerText = "admin";
        wb.Document.GetElementById("password").InnerText = "cool";
        wb.Document.GetElementById("Submit1").InvokeMember("click");

        wb.DocumentCompleted += wb_DocumentCompleted;

        while (!completed)
            Application.DoEvents();

        var resultHeader = wb.Document.Url.OriginalString;
        var result = wb.Document.Body.InnerHtml;

        var cookie = wb.Document.Cookie;
        MessageBox.Show(cookie, "Cookie");
        MessageBox.Show(result, resultHeader);
    }

    private bool completed;
    private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        completed = true;
        ((WebBrowser)sender).DocumentCompleted -= wb_DocumentCompleted;
    }

编辑 1:
- 添加获取cookie

【讨论】:

  • 好的,这段代码工作得很好,但我不能保留 cookie。所有这一切的主要目标是使用通过 FormsAuthentication 方法进行身份验证的 WebService。所以即使我可以触发Form的好信息,我也不能保留cookie,所以我不能消费WebServices。也许你有别的想法? ;)
  • 我在登录后添加了获取 cookie,但如果这不起作用,我无能为力。
猜你喜欢
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多