Logging into https using C #

I try to write a small program that registers on the Verizon website, and then checks how many minutes are left per month. I need help figuring out how to log in using C #. I know that I need to use webrequest to publish login information, but I'm not sure how to do this. The site with the login form is https://login.verizonwireless.com/amserver/UI/Login , but I'm not sure what information I should publish on the site to login and how to do it. The following is what I found for the source of the website. If someone can help me understand how to log in using a C # program, I would really appreciate it. Thanks for any help.

form method = "post" autocomplete = "off" action = "https://login.verizonwireless.com:443/amserver/UI/Login" name = "loginForm" id = "loginForm" onsubmit = "return disableBut (); " >
input type = "hidden" name = "realm" value = "vzw" / ">
input type =" hidden "name =" goto "value =" "/">
input type = "hidden" name = "gotoOnFail" value = "" / ">
input type =" hidden "name =" gx_charset "value =" UTF-8 "/">
input type = "hidden" name = "remember UserNameCheckBoxExists" value = "Y" / ">
h2 style =" padding-left: 0px; ">Enter My Verizon
div class = "clear10"> / div>

+5
source share
2 answers

First of all, you are missing two important fields :) If you look at HTML, there are two additional fields in the form - IDToken1(this is the username) and IDToken2(this is the password).

If you pass them into a POST request, you must return some cookies, which can then be used on subsequent requests. They identify you as a registered user.

Of course, I cannot fully verify this, since I do not have a valid login, but here is the beginning:

class VerizonLogin
{
    CookieContainer Cookies = new CookieContainer();

    void Main()
    {
        Login("test","testpass");

        // Now the cookies in "Cookies" are all set.
        // Ensure you set CookieContainer on all subsequent requests
    }

    void Login(string username, string password)
    {
        var wr = (HttpWebRequest)WebRequest.Create("https://login.verizonwireless.com:443/amserver/UI/Login");
        wr.Method = "POST";
        wr.ContentType = "application/x-www-form-urlencoded";
        wr.Referer = "https://login.verizonwireless.com/amserver/UI/Login"; // my tests show this is needed
        wr.CookieContainer = Cookies;

        var parameters = new Dictionary<string,string>{
            {"realm", "vzw"},
            {"goto",""},
            {"gotoOnFail",""},
            {"gx_charset", "UTF-8"},
            {"rememberUserNameCheckBoxExists","Y"},
            {"IDToken1", username},
            {"IDToken2", password}
        };

        using (var requestStream = wr.GetRequestStream())
        using (var writer = new StreamWriter(requestStream,Encoding.UTF8))
            writer.Write(ParamsToFormEncoded(parameters));

        using (var response = (HttpWebResponse)wr.GetResponse())
        {
            // here you need to detect a correct login... this might be one of the cookies.
            // if incorrect throw an exception or something.
        }
    }

    string ParamsToFormEncoded(Dictionary<string,string> parameters)
    {
        return string.Join("&", parameters.Select(kvp => 
            Uri.EscapeDataString(kvp.Key).Replace("%20","+") + "=" + Uri.EscapeDataString(kvp.Value).Replace("%20","+")
        ).ToArray());
    }
}
+2
source

2 , . , , webrequest, , https. .

#

private bool ValidateCert(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
    return true;
}

private string PostToSite(string url)
{
    string result = string.empty;

    byte[] postBuffer = System.Text.Encoding.GetEncoding(1252).GetBytes("realm=vzw&goto=&gotoOnFail=&gx_charset=UTF-8&rememberUserNameCheckBoxExists=Y");

    HttpWebRequest webRequest = (HttpWebRequest)Net.WebRequest.Create(_endpoint);
    webRequest.KeepAlive = false;
    webRequest.AllowAutoRedirect = false;

    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateCert);

    webRequest.ContentLength = postBuffer.Length;
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";

    using (Stream str = webRequest.GetRequestStream()) {
        str.Write(postBuffer, 0, postBuffer.Length);
    }

    using (System.Net.HttpWebResponse res = (HttpWebResponse)webRequest.GetResponse()) {
        using (StreamReader sr = new StreamReader(res.GetResponseStream())) {
            result = sr.ReadToEnd();
        }
    }

    return result;
}

VB.NET

Private Function ValidateCert(ByVal sender As Object, ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
 Return True
End Function

Private Function PostToSite(url as string) as string
    Dim result as string = string.empty

    Dim postBuffer As Byte() = System.Text.Encoding.GetEncoding(1252).GetBytes("realm=vzw&goto=&gotoOnFail=&gx_charset=UTF-8&rememberUserNameCheckBoxExists=Y")

    Dim webRequest As HttpWebRequest = CType(Net.WebRequest.Create(_endpoint), HttpWebRequest)
    webRequest.KeepAlive = False
    webRequest.AllowAutoRedirect = False

    ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf ValidateCert)

    webRequest.ContentLength = postBuffer.Length
    webRequest.Method = "POST"
    webRequest.ContentType = "application/x-www-form-urlencoded"

    Using str As Stream = webRequest.GetRequestStream()
        str.Write(postBuffer, 0, postBuffer.Length)
    End Using

    Using res As System.Net.HttpWebResponse = CType(webRequest.GetResponse(), HttpWebResponse)
        Using sr As New StreamReader(res.GetResponseStream())
            result = sr.ReadToEnd()
        End Using
    End Using

    return result
End Function
+1

All Articles