I have written a C# function using HttpWebRequest that checks if a user successfully logged in to a web and how much time in Milliseconds it took him to log in. The problem is, that even though I used try and catch, when I enter the wrong Username and Password it's still not going to catch and I can't verify if it succeeded. How can I check if the user actually logged in successfully?
private int[] LoginCheck(string TargetWebApp)
{
int[] result = new int[2];
var watch = System.Diagnostics.Stopwatch.StartNew();
try
{
string formUrl = "https://XXXXX.com/user/login/default"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("email_address={0}&password={1}", "XXXXX", "XXXXX");
string cookieHeader;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;;
var elapsedMs = watch.ElapsedMilliseconds;
watch.Stop();
Console.WriteLine("Login to WebApp succeed in {0} Milliseconds", elapsedMs);
cookieHeader = resp.Headers["Set-cookie"];
result[0] = 1;
result[1] = (int)elapsedMs;
}
catch (Exception e)
{
//Any exception will return false.
Console.WriteLine(e.Message);
result[0] = 0;
result[1] = 0;
}
return result;
}