繁体   English   中英

C#Winforms应用程序和FormsAuthentication

[英]C# Winforms Application and FormsAuthentication

我有一个WinForms应用程序(只有一个试图获取Default.aspx页面的按钮)和一个带有FormsAuthentication的WebSite(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,因为身份验证无效。 请帮助^^!

您可以使用以下代码通过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

暂无
暂无

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

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