简体   繁体   English

C#Winforms应用程序和FormsAuthentication

[英]C# Winforms Application and FormsAuthentication

I've a WinForms Application (only a button which tries to get the Default.aspx page) and a WebSite with a FormsAuthentication (Logon.aspx and Default.aspx) 我有一个WinForms应用程序(只有一个试图获取Default.aspx页面的按钮)和一个带有FormsAuthentication的WebSite(Logon.aspx和Default.aspx)

Here my both codes : 这是我的两个代码:

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

I'm trying to use the CookieContainer Class in order to authenticate myself to the website... but it doesn t works... 我正在尝试使用CookieContainer类来向网站进行身份验证...但是它不起作用...

Could you check where I'm wrong ? 你能检查我哪里错了吗? I mean, it has been maybe 5 days I'm on it... I really can't find the answer. 我的意思是,大概已经进行了5天了……我真的找不到答案。 Thanks ! 谢谢 !

EDIT : 编辑:

I have this part of my code, that doesn t work: 我的代码中有这部分,它不起作用:

        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...
        }
    }

On the result string, you could see the redirection to Logon.aspx, because the authentication didnt work. 在结果字符串上,您可以看到重定向到Logon.aspx,因为身份验证无效。 Help please ^^ ! 请帮助^^!

You can control login with WebBrowser component with this codes: 您可以使用以下代码通过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;
    }

EDIT 1: 编辑1:
- Added get cookie -添加了获取cookie

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

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