What is the best solution for a web login api in Xamarin Forms?

Asked

Viewed 577 times

1

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 method GET json that I can use in my web api, so for login there is no such GET json, and there’s the problem, how can I do this validation of user and password if on the website there is no function that can be used in my webservice??? Remembering that it is not possible to create the GET json for that login on the site, if there was such a possibility the problem would be solved easily!

  • you can send the Xamarin data via put ...method for example, you could send the login and password to the API to then receive the return.

  • If possible show some code, if you need an example of how to receive data json let me know that I put it here.. you need to download via Nuget the Microsoft Net package. Http and Newtonsoft.json for you to use Httpclient and Jsonconvert, I needed to do this these days and I sifted to get rs I hope I helped! hugs

  • I am creating a solution, when finish and test put here how I managed to solve!!

1 answer

0

The only viable output 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;

            }

        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.