Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- namespace T4SHubMobile
- {
- public partial class MainPage : ContentPage
- {
- // Strings
- string Cookies = null;
- // Ints
- int Redirections;
- // Views
- ScrollView ContainerScrollView;
- StackLayout WrapperStackLayout;
- Image T4Sicon;
- Label HeaderLabel;
- Label ErrorTextLabel;
- Entry UsernameInput;
- Entry PasswordInput;
- Button LoginButton;
- WebView LoginWebView;
- public MainPage()
- {
- InitializeComponent();
- // Get references to views
- ContainerScrollView = this.FindByName<ScrollView>("containerScrollView");
- WrapperStackLayout = this.FindByName<StackLayout>("wrapperStackLayout");
- T4Sicon = this.FindByName<Image>("t4Sicon");
- HeaderLabel = this.FindByName<Label>("headerLabel");
- ErrorTextLabel = this.FindByName<Label>("errorTextLabel");
- UsernameInput = this.FindByName<Entry>("usernameInput");
- PasswordInput = this.FindByName<Entry>("passwordInput");
- LoginButton = this.FindByName<Button>("loginButton");
- LoginWebView = this.FindByName<WebView>("loginWebView");
- // Init variables
- Redirections = 0;
- // logout for testing
- LoginWebView.Source = "https://www.t4shub.com/Account/Signout";
- }
- public void OnLoginButtonClick()
- {
- // Check if input fields are empty, if not then proceed to login
- if (!String.IsNullOrWhiteSpace(UsernameInput.Text) && !String.IsNullOrWhiteSpace(PasswordInput.Text))
- {
- App.Username = UsernameInput.Text;
- App.Password = PasswordInput.Text;
- // Add OnPageFinished method to webview
- LoginWebView.Navigated += OnPageFinished;
- // Login
- Redirections = 0;
- Login();
- }
- else
- {
- ErrorTextLabel.Text = "Username/Password cannot be left empty!";
- }
- }
- public void Login()
- {
- LoginWebView.Source = "https://www.t4shub.com/Account/SignIn";
- //Application.Current.MainPage = new NavigationPage(new SearchPage());
- }
- public void OnPageFinished(object sender, WebNavigatedEventArgs e)
- {
- Debug.WriteLine("PAGE " + e.Url + " FINISHED LOADING!");
- // If the login succeeds and the user is redirected to the t4shub homepage, go to search page
- if (e.Url.Contains("https://www.t4shub.com/") && e.Result == WebNavigationResult.Success)
- {
- Debug.WriteLine("Url contains \"https://www.t4shub.com/\"");
- }
- else if (e.Url.Contains("login.microsoftonline") && Redirections == 2)
- {
- WebView view = sender as WebView;
- InjectScriptFile(view);
- }
- Redirections++; Debug.WriteLine("Redirections: " + Redirections);
- }
- private void InjectScriptFile(WebView view)
- {
- var jscall = "var parent = document.getElementsByTagName('head').item(0);" + "\n" +
- "var script = document.createElement('script');" + "\n" +
- "script.type = 'text/javascript';" + "\n" +
- "var useridView = document.getElementById('cred_userid_inputtext');" + "\n" +
- "var passwordView = document.getElementById('cred_password_inputtext');" + "\n" +
- "var signInButton = document.getElementById('cred_sign_in_button');" + "\n" +
- "var userid = \"" + App.Username + "\";" + "\n" +
- "var password = \"" + App.Password + "\";" + "\n" +
- "useridView.value = userid;" + "\n" +
- "passwordView.value = password;" + "\n" +
- "signInButton.click();" + "\n" +
- "parent.appendChild(script);";
- try
- {
- view.Eval(jscall);
- Debug.WriteLine("Javascript:\n\n" + jscall + "\n\nevaluated!");
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- Debug.WriteLine("JAVASCRIPT INJECTION ERROR: " + e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement