hi i'm trying to build an app that allow to login to instagram using c# when i run my application and logged in , i need to be redirected to fixed page without any reaction from me here's my code :

private void button1_Click(object sender, EventArgs e)
{
HtmlDocument doc = web.Document;
HtmlElement username = doc.GetElementById("id_username");
HtmlElement password = doc.GetElementById("id_password");
username.SetAttribute("value", "****");
password.SetAttribute("value", "****");
StringBuilder sb = new StringBuilder();
foreach (HtmlElement elm in web.Document.All)
if (elm.GetAttribute("value") == "Log in")
{
sb.Append(elm.InnerHtml);
Thread.Sleep(3000);
elm.InvokeMember("click");
// web.Navigate(@""); // it navigate me but required to login again
}
}
private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (web.ReadyState != WebBrowserReadyState.Complete)
{
MessageBox.Show("not logged in");
}
}

Dani AI

Generated

Quick summary and recommended path. Programmatically filling the Instagram form and invoking the button (as does) is fragile: Instagram’s login uses JavaScript + CSRF tokens and sets HTTP-only session cookies, so the robust, long-term solution is to use the official OAuth flow (register an app and use a redirect_uri that Instagram will return to after login). For desktop apps that need an automatic redirect, open the system browser for the OAuth URL and listen for the redirect (or run a local HTTPS redirect endpoint) rather than trying to emulate a browser inside WebBrowser. (justinstolpe.com)

Practical debugging steps and quick fixes. As suggested, capture the real browser login with a network inspector and compare headers/cookies — look for the CSRF token and the server-issued cookies (Instagram commonly sets a sessionid plus related cookies). If you must continue with the WebBrowser control, do three things: (1) ensure the control uses a modern IE emulation (set FEATURE_BROWSER_EMULATION for your EXE to IE11), (2) stop using Thread.Sleep on the UI thread and instead handle navigation events correctly, and (3) detect login completion by checking the final navigation (ignore frame DocumentCompleted events) and by checking for the server session cookie before navigating to the target page. (stackoverflow.com)

Minimal C# pattern to detect login (replace targetUrl):

// P/Invoke to read WinINet cookies (HTTPOnly)
[DllImport("wininet.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern bool InternetGetCookieEx(string url, string cookieName, StringBuilder cookieData, ref uint size, int flags, IntPtr reserved);
const int INTERNET_COOKIE_HTTPONLY = 0x00002000;

private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url != web.Url) return; // ignore frames
    uint size = 0;
    InternetGetCookieEx(web.Url.ToString(), null, null, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero);
    var sb = new StringBuilder((int)size);
    if (InternetGetCookieEx(web.Url.ToString(), null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
        if (sb.ToString().Contains("sessionid="))
            web.Navigate(targetUrl); // only navigate after cookie is present
}

(The WinINet call and usage pattern above is standard; see docs/examples.) (learn.microsoft.com)

Caveats. Automated credential-based logins break often, may trigger rate limits/2FA, and are generally less stable than OAuth. Also note the platform changes: the older Basic Display flow has been deprecated and Meta has been steering apps toward the Instagram/Graph OAuth flows (Business/Creator requirements for some endpoints), so prefer official API/OAuth migration when possible. (help.zapier.com)

Notes specific to the thread: the DocumentCompleted check in the first post is backwards (don’t show “not logged in” based solely on ReadyState), and scanning all elements for value == "Log in" can click the wrong element. Use direct element IDs/classes where stable, or rely on cookie detection as shown above.

Fiddler is your friend here. Could be cookies, or header related. Look at a normal logon in fiddler and compare it with yours. http://www.telerik.com/fiddler

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.