Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
in asp.net c# using web browser control to implement automatically login in live site. its works only in internet explorer,but its not work in Firefox and chrome how to solve this error ..

and also am tried to run in live its shown HTTP 503 error and service unavailable.



C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    static string password = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtUsername.Text = "********";
            password = "********";
            for (int i = 0; i < password.Length; i++)
            {
                txtPassword.Text = txtPassword.Text + "*";
            }


            Thread thread = new Thread(delegate()
            {

                using (WebBrowser browser = new WebBrowser())
                {
                    browser.ScrollBarsEnabled = false;
                    browser.AllowNavigation = true;
                    browser.Navigate("URL");
                    browser.Width = 1024;
                    browser.Height = 768;
                    browser.ScriptErrorsSuppressed = true;
                    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted2);
                    while (browser.ReadyState != WebBrowserReadyState.Complete)
                    {
                        System.Windows.Forms.Application.DoEvents();
                    }
                }
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            capture();
            Response.Redirect("URL");

        }

    }
    void capture()
    {

        if (password == "********" && txtUsername.Text == "********")
        {

            Thread thread = new Thread(delegate()
            {
                using (WebBrowser browser = new WebBrowser())
                {
                    browser.ScrollBarsEnabled = false;
                    browser.AllowNavigation = true;
                    browser.Navigate("URL");
                    browser.Width = 1024;
                    browser.ScriptErrorsSuppressed = true;
                    browser.Height = 768;
                    browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);
                    while (browser.ReadyState != WebBrowserReadyState.Complete)
                    {
                        System.Windows.Forms.Application.DoEvents();
                    }
                }
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
        }
        else
        {
            Response.Write("<script>alert('Invalid Username And Password!!!')</script>");
        }
    }


    private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser browser1 = sender as WebBrowser;

        HtmlDocument doc = browser1.Document;

        string user = txtUsername.Text.Trim();
        string pass = password;

        doc.GetElementById("username").SetAttribute("value", user);
        doc.GetElementById("passwd").SetAttribute("value", pass);

        //get Button on page and fire its click event
        doc.GetElementById(".save").InvokeMember("Click");

        Thread thread = new Thread(delegate()
        {
            using (WebBrowser browser = new WebBrowser())
            {
                browser.ScrollBarsEnabled = false;
                browser.AllowNavigation = true;
                browser.Navigate("Navigate URL");
                browser.Width = 1024;
                browser.Height = 768;

                browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted1);
                while (browser.ReadyState != WebBrowserReadyState.Complete)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
            }
        });
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();
        //  


    }
    private void DocumentCompleted1(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser browser = sender as WebBrowser;
        using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
        {
            browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                byte[] bytes = stream.ToArray();
                imgScreenShot.Visible = true;
                imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
            }
        }
        // Response.Redirect("URL");

    }

    private void DocumentCompleted2(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser browser = sender as WebBrowser;
        using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height))
        {
            browser.DrawToBitmap(bitmap, new Rectangle(0, 0, browser.Width, browser.Height));
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                byte[] bytes = stream.ToArray();
                imgScreenShot.Visible = true;
                imgScreenShot.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
            }
        }
    }
}
Posted
Updated 17-Mar-17 2:02am
v2
Comments
ZurdoDev 18-Feb-15 8:39am    
What do you mean it does not work in Chrome or FireFox? You said you are using the WebBrowser control, not an actual browser.
Richard Deeming 18-Feb-15 9:44am    
This is never going to work. You are logging in on the server, which sets the authentication cookies in Internet Explorer on the server, and then redirecting the client to the protected page, when the client hasn't been authenticated.

It might appear to work when you run the site from Visual Studio, but that's only because, in that particular case, the client and server are the same computer.
Karthic Raja 18-Feb-15 23:24pm    
am signin gmail account in normal asp.net project that time i want login automatically in chrome side (my gmail account).

i want to implement auto login in live website using asp.net c# and its need to working all browsers. how to solve this ...please reply....
Richard Deeming 19-Feb-15 7:12am    
Are you logging the users into your GMail account, or their GMail account?

If it's your account, then you might as well just give them the username and password. You're giving the user complete control over your account anyway; they could easily change the login credentials to whatever they want.

If it's their account, then this would be a phishing page, used to steal other people's credentials. That is illegal, and you won't find any help here.
Sergey Alexandrovich Kryukov 18-Feb-15 12:19pm    
What makes you thinking that using this control can help?
—SA

1 solution

Its not possible to allow in chrome or firefox because its secure.
there are not permission for auto login using c#
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900