Exemplo n.º 1
0
        void SendRegistrationToServer(string token)
        {
            // Add custom implementation, as needed.
            string deviceId = prefs.GetString(Constants.DEVICEID, "");

            if (!deviceId.Equals(token))
            {
                var model = new DeviceIdModel
                {
                    UserId   = prefs.GetString(CONSTANTS.USERID, ""),
                    DeviceId = token
                };

                try
                {
                    var client = new HttpClient();
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + prefs.GetString(CONSTANTS.AUTH_HEADER, ""));
                    client.PostAsync(CONSTANTS.POST_DEVICEID_URL, new FormUrlEncodedContent(model.ToDict())).ContinueWith((response) => {
                        Console.Write(response);
                    });
                }catch (Exception e)
                {
                    Toast.MakeText(this, e.Message, ToastLength.Short).Show();
                }

                //post
                Console.Write("token is " + token);
            }
        }
Exemplo n.º 2
0
        private async void OnLoginClicked(object sender, EventArgs e)
        {
            if (!IsInValidInput())
            {
                ConnectivityManager service = (ConnectivityManager)GetSystemService(ConnectivityService);
                NetworkInfo         info    = service.ActiveNetworkInfo;
                if (info != null)
                {
                    HttpClient client    = new HttpClient();
                    var        loginData = new LoginModel
                    {
                        UserName  = etUsername.Text,
                        Password  = etPassword.Text,
                        GrantType = "password"
                    };

                    try
                    {
                        HttpResponseMessage result = await client.PostAsync(CONSTANTS.LOGIN_URL, new FormUrlEncodedContent(loginData.ToDict()));

                        if (result.IsSuccessStatusCode)
                        {
                            string jsonResult = await result.Content.ReadAsStringAsync();

                            // TokenResult is a custom model class for deserialization of the Token Endpoint

                            var resultObject = JsonConvert.DeserializeObject <TokenModel>(jsonResult);


                            client.DefaultRequestHeaders.Add("Authorization", "Bearer" + resultObject.Access_Token);
                            var profile = await client.GetAsync(String.Format(CONSTANTS.GET_USERINFO_URL, resultObject.Access_Token));

                            var jsonProfile = await profile.Content.ReadAsStringAsync();

                            var user = JsonConvert.DeserializeObject <UserInfoModel>(jsonProfile);

                            user.DeviceId = prefs.GetString(CONSTANTS.DEVICEID, "");


                            var model = new DeviceIdModel
                            {
                                UserId   = user.Id,
                                DeviceId = user.DeviceId
                            };


                            await client.PostAsync(CONSTANTS.POST_DEVICEID_URL, new FormUrlEncodedContent(model.ToDict()));

                            prefEditor.PutString(CONSTANTS.AUTH_HEADER, resultObject.Access_Token);
                            prefEditor.PutString(CONSTANTS.USERID, user.Id);
                            prefEditor.PutString(CONSTANTS.USERNAME, user.Username);
                            prefEditor.PutString(CONSTANTS.EMAIL, user.Email);
                            prefEditor.PutInt(CONSTANTS.REGIONID, user.RegionId);
                            prefEditor.PutString(CONSTANTS.DEVICEID, user.DeviceId);
                            prefEditor.PutString(CONSTANTS.FULLNAME, user.Fullname);
                            prefEditor.Apply();

                            //move to discount page
                            Intent intent = new Intent(this, typeof(NavigationDrawerActivity));
                            intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
                            StartActivity(intent);
                        }
                        else
                        {
                            Toast.MakeText(this, result.ReasonPhrase, ToastLength.Short).Show();
                        }
                    }
                    catch (Exception ex)
                    {
                        string debugBreak = ex.ToString();
                        Toast.MakeText(this, debugBreak, ToastLength.Short).Show();
                    }
                }
                else
                {
                    Toast.MakeText(this, "Invalid inputs", ToastLength.Short).Show();
                }
            }
            else
            {
                //Snackbar snackBar = Snackbar.Make((Button)sender, "No nternet Connection", Snackbar.LengthIndefinite);
                //Show the snackbar
                // snackBar.Show();
            }
        }
Exemplo n.º 3
0
        private async Task LogoutAsync()
        {
            try
            {
                using (var client = new HttpClient()){
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + prefs.GetString(CONSTANTS.AUTH_HEADER, ""));

                    var model = new DeviceIdModel
                    {
                        UserId   = prefs.GetString(CONSTANTS.USERID, ""),
                        DeviceId = ""
                    };
                    //TODO:delete from device id table
                    await client.PostAsync(CONSTANTS.POST_DEVICEID_URL, new FormUrlEncodedContent(model.ToDict()));

                    var result = await client.PostAsync(Constants.LOGOUTUSER_URL, null);

                    if (result.IsSuccessStatusCode)
                    {
                        prefEditor.Remove(CONSTANTS.AUTH_HEADER);
                        prefEditor.Remove(CONSTANTS.DEVICEID);
                        prefEditor.Remove(CONSTANTS.USERID);
                        prefEditor.Remove(CONSTANTS.USERNAME);
                        prefEditor.Remove(CONSTANTS.EMAIL);
                        prefEditor.Remove(CONSTANTS.FULLNAME);
                        prefEditor.Remove(CONSTANTS.RECEIVE_NOTIFICATION_BOOLEAN);


                        prefEditor.Apply();

                        Intent intent = new Intent(this, typeof(MainActivity));
                        intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
                        StartActivity(intent);
                    }
                    else
                    {
                        Toast.MakeText(this, "Logout unsuccessful", ToastLength.Short).Show();
                    }
                }
            }
            catch (Exception exp)
            {
                Toast.MakeText(this, exp.Message, ToastLength.Short).Show();
            }
        }