public static object MakeRequest(string RequestURL, string ContentType, string Method, string AuthHeader, bool ReturnAccessToken = false) { //builds request HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestURL); request.ContentType = ContentType; request.Method = Method; //passes in clientid+secret request.Headers.Add(HttpRequestHeader.Authorization, AuthHeader); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.OK) { Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode); } //reads response using (StreamReader reader = new StreamReader(response.GetResponseStream())) { var content = reader.ReadToEnd(); if (ReturnAccessToken) { AccessObject Token = JsonConvert.DeserializeObject <AccessObject>(content); return(Token); } return(response); } } }
public override void ViewDidLoad() { base.ViewDidLoad(); NavigationController.SetNavigationBarHidden(true, false); // Perform any additional setup after loading the view, typically from a nib. if (SaveUser != "") { UserNameTextField.Text = SaveUser; } if (SavePass != "") { Password.Text = SavePass; } Login.AccessibilityIdentifier = "myButton"; Login.TouchUpInside += delegate { try { Password.ResignFirstResponder(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://goingpro.azurewebsites.net/api/Logins?loginid=" + UserNameTextField.Text); request.Method = "Get"; //request.ContentType = @"application/json"; request.Accept = @"application/json"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); StreamReader getjson = new StreamReader(stream); String getcontent = getjson.ReadToEnd(); using (response) { if (response.StatusCode != HttpStatusCode.OK) { Console.WriteLine("Error fetching data. Server returned status code " + response.StatusCode); } else { //unhash password and check against TBPassword.Text LoginInfo thisinfo = JsonConvert.DeserializeObject <LoginInfo>(getcontent); if (!checkpassword(thisinfo.Login_Password, Password.Text)) { Console.WriteLine("Wrong!"); Toast.MakeText("Password Incorrect").Show(); Password.Text = ""; } else { //Requests an access token from powerschool that we use for getting data; Token = (AccessObject)MakeRequest(string.Format(@"http://powerschool.kentisd.org/oauth/access_token?grant_type=client_credentials"), "application/x-www-form-urlencoded;charset=UTF-8", "POST", "Basic ZWRlMjY4ZmMtOTM5Mi00Y2NkLTgxNjktNjk2ZjI0YmNjZTU2OmU5MDRlNzYwLTEzZjQtNDY5My1iYWM5LWIwZTMyYTJhM2Y3Ng==", true); StaffFirst = thisinfo.First_Name; StaffLast = thisinfo.Last_Name; StaffEmail = thisinfo.Email; StaffUserName = UserNameTextField.Text; SavePass = Password.Text; SaveUser = UserNameTextField.Text; Toast.MakeText("Login Successful").Show(); PerformSegue("GoToButtonPage", this.Self); } } } } catch { Toast.MakeText("Could Not Connect To Service (Perhaps Powerschool or Wi-Fi is down.)").Show(); return; } }; }