What is the best solution for a web login api in xamarin forms?

0

I have the following challenge to solve, I am developing an app using xamarin forms , this application will have to communicate with a website developed in PHP to consume your data, for all functions there is a GET json method that I can I can not use my web api , so for login this GET json does not exist, and there is the problem, how can I do this user validation and password if there is no function that can be used in my webservice ? ??? Remembering that it is not possible to create GET json for this login to the site, if there was this possibility the problem would be solved easily!

    
asked by anonymous 21.02.2018 / 16:45

1 answer

0

The only feasible exit was bypassing the system, hence my login page was as follows: LoginPage.xaml.cs

public partial class LoginPage : TabbedPage
    {
        public LoginPage()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);

        }

        protected async void btnLogin_Clicked(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(emailEntry.Text))
            {
                await DisplayAlert("Erro", "Digite um usuário válido", "Aceitar");
                emailEntry.Focus();
                return;
            }

            if (string.IsNullOrEmpty(senhaEntry.Text))
            {
                await DisplayAlert("Erro", "Digite uma senha", "Aceitar");
                senhaEntry.Focus();
                return;
            }

            try
            {
                waitActivityIndicator.IsRunning = true;
                var loginRequest = new Login
                {
                    UserName = emailEntry.Text,
                    Password = senhaEntry.Text,
                };

                //invocar serviço


                waitActivityIndicator.IsRunning = false;
                Device.BeginInvokeOnMainThread(() => App.Current.MainPage = new MainPageRoot());
            }
            catch (Exception ex)
            {
                waitActivityIndicator.IsRunning = false;

                await DisplayAlert("Erro", "Usuário ou Senha Incorretos", "Aceitar");
            }
        }

    }

LoginService.cs

 public async void Logar(Login login)
        {



            var jsonRequest = JsonConvert.SerializeObject(login);
            var httpContent = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
            var resp = string.Empty;

            try
            {
                var client = new HttpClient();
                client.BaseAddress = new Uri($"{BaseAddress}/_signin.php?usuario={login.UserName}&senha={login.Password}");
                var url = "";
                var result = await client.PostAsync(url, httpContent);

                if (!result.IsSuccessStatusCode)
                {
                    throw new Exception(result.RequestMessage.Content.ToString());
                }

                resp = await result.Content.ReadAsStringAsync();
                //verificar resposta do ws
                var user = JsonConvert.DeserializeObject<Colaborador>(resp);



            }
            catch (Exception)
            {
                throw;

            }

        }
    
22.02.2018 / 16:39