简体   繁体   中英

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)

Here my both codes :

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

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. 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. Help please ^^ !

You can control login with WebBrowser component with this codes:

    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:
- Added get cookie

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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