Search code examples
c#httpvbulletin

vbulletin Http login


So I'm making a small application for a vbulleting site but need to authenticate the user when he opens my application, I have code to send login request, but I am unsure how to actually check if the login was successful.

This is what I have so far:

 public string Login(string username, string password)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(MPGHLogin);
        string cookie = "";
        string values = "vb_login_username=" + username + "&vb_login_password=" + password
                        + "&securitytoken=guest&"
                        + "cookieuser=checked&"
                        + "do=login";
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = values.Length;
        CookieContainer a = new CookieContainer();
        req.CookieContainer = a;

        System.Net.ServicePointManager.Expect100Continue = false;

        using (StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
        {
            writer.Write(values);
        }

        HttpWebResponse c = (HttpWebResponse)req.GetResponse();
        foreach (Cookie cook in c.Cookies)
        {
            cookie = cookie + cook.ToString() + ";";
        }



        if (c.StatusCode != HttpStatusCode.OK)
            return "FAILED_CONNECTION";

        return cookie;
    }

But how can I check if the authentication was successful?


Solution

  • For other people that may have the same problem, I completely forgot about checking the respone stream for a login successful message, so below is the full code.

     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(MPGHLogin);
                    req.AllowAutoRedirect = true;
                    string cookie = "";
                    string values = "vb_login_username=" + username + "&vb_login_password=" + password
                                    + "&securitytoken=guest&"
                                    + "cookieuser=checked&"
                                    + "do=login";
                    req.Method = "POST";
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.ContentLength = values.Length;
                    CookieContainer a = new CookieContainer();
                    req.CookieContainer = a;
    
                    System.Net.ServicePointManager.Expect100Continue = false;
    
                    StreamWriter writer = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
                    writer.Write(values);
                    writer.Close();
    
                    req.Timeout = 5000;
                    HttpWebResponse c;
                    try {
                        c = (HttpWebResponse)req.GetResponse(); // da response 
                    } catch(Exception e)
                    {
                        MessageBox.Show(e.Message, "Web Exception");
                        return "WebException";
                    }
    
                    foreach (Cookie cook in c.Cookies)
                    {
                        cookie = cookie + cook.ToString() + ";";
                    }
    
                    Stream resp = c.GetResponseStream();
                    StreamReader reader = new StreamReader(resp, Encoding.GetEncoding(c.CharacterSet));
                    string response = reader.ReadToEnd();
                    reader.Close();
                    reader.Dispose();
    
                    if (response.Contains("Thank you for logging in, " + username))
                    {
                        c.Dispose();
                        return cookie;
                    }
                    else
                        return "FAILED_AUTH";