Exemplo n.º 1
0
        public static async Task <bool> LoginAsync(string data)
        {
            string     authenticationURL = "https://ivle.nus.edu.sg/api/Lapi.svc/Login_JSON";
            HttpClient client            = new HttpClient();

            // encode the request content in url encoding format
            HttpContent         payload  = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded");
            HttpResponseMessage response = client.PostAsync(authenticationURL, payload).Result;

            // wait for the string response
            string responseString = await response.Content.ReadAsStringAsync();

            // remove the last "}"
            responseString = responseString.Remove(responseString.Length - 1);

            // remove the first "{" and its associated header
            responseString = responseString.Substring(responseString.IndexOf(":") + 1);

            // store the newly received token
            _token = JsonConvert.DeserializeObject <DataStructure.Token>(responseString);

            payload.Dispose();
            response.Dispose();
            client.Dispose();

            return(_token != null && _token.TokenSuccess);
        }
Exemplo n.º 2
0
        public static void RemoveToken()
        {
            _token = null;

            Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

            roamingSettings.Values[DataStructure.ConstName.Token] = null;
        }
Exemplo n.º 3
0
        public static void RemoveToken()
        {
            _token = null;

            Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

            roamingSettings.Values[DataStructure.ConstName.Token] = null;
        }
Exemplo n.º 4
0
        // to check if token has been stored in application data
        // if so, restore the token from stored data, else, return false
        public static bool IsTokenExisting()
        {
            Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

            if (roamingSettings.Values.ContainsKey(DataStructure.ConstName.Token))
            {
                // load token from application data
                _token = new DataStructure.Token(roamingSettings.Values[DataStructure.ConstName.Token].ToString());
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 5
0
        // to check if token has been stored in application data
        // if so, restore the token from stored data, else, return false
        public static bool IsTokenExisting()
        {
            Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

            if (roamingSettings.Values.ContainsKey(DataStructure.ConstName.Token))
            {
                // load token from application data
                _token = new DataStructure.Token(roamingSettings.Values[DataStructure.ConstName.Token].ToString());
                return true;
            }
            else
            {
                return false;
            }
        }
Exemplo n.º 6
0
        // asynchronous function to validate token, to
        // update token if necessary or to return false
        // if token is invalid
        public static async Task <bool> IsTokenValid()
        {
            HttpClient client = new HttpClient();

            // http get request to validate token
            HttpResponseMessage response = await client.GetAsync(Utils.LAPI.GenerateValidateURL());

            // make sure the http reponse is successful
            response.EnsureSuccessStatusCode();

            // convert http response to string
            string responseString = await response.Content.ReadAsStringAsync();

            _token = JsonConvert.DeserializeObject <DataStructure.Token>(responseString);

            response.Dispose();
            client.Dispose();

            return(_token != null && _token.TokenSuccess);
        }
Exemplo n.º 7
0
        // asynchronous function to validate token, to 
        // update token if necessary or to return false
        // if token is invalid
        public static async Task<bool> IsTokenValid()
        {
            HttpClient client = new HttpClient();

            // http get request to validate token
            HttpResponseMessage response = await client.GetAsync(Utils.LAPI.GenerateValidateURL());

            // make sure the http reponse is successful
            response.EnsureSuccessStatusCode();

            // convert http response to string
            string responseString = await response.Content.ReadAsStringAsync();

            _token = JsonConvert.DeserializeObject<DataStructure.Token>(responseString);

            response.Dispose();
            client.Dispose();

            return (_token != null && _token.TokenSuccess);
        }
Exemplo n.º 8
0
        public static async Task<bool> LoginAsync(string data)
        {
            string authenticationURL = "https://ivle.nus.edu.sg/api/Lapi.svc/Login_JSON";
            HttpClient client = new HttpClient();

            // encode the request content in url encoding format
            HttpContent payload = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded");
            HttpResponseMessage response = client.PostAsync(authenticationURL, payload).Result;

            // wait for the string response
            string responseString = await response.Content.ReadAsStringAsync();

            // remove the last "}"
            responseString = responseString.Remove(responseString.Length - 1);

            // remove the first "{" and its associated header
            responseString = responseString.Substring(responseString.IndexOf(":") + 1);

            // store the newly received token
            _token = JsonConvert.DeserializeObject<DataStructure.Token>(responseString);

            payload.Dispose();
            response.Dispose();
            client.Dispose();

            return (_token != null && _token.TokenSuccess);
        }