Exemplo n.º 1
0
        private async void Login()
        {
            var userModel = new UserModel
            {
                Email    = Email,
                Password = Password
            };
            var validator        = new UserModelValidator();
            var validationResult = validator.Validate(userModel);

            if (!validationResult.IsValid)
            {
                ValidateMessage = validationResult.Errors[0].ErrorMessage;
                return;
            }

            var        cookieContainer = new CookieContainer();
            HttpClient httpClient      = new HttpClient(new HttpClientHandler()
            {
                CookieContainer = cookieContainer
            });
            var initResponse = await httpClient.GetAsync(Contants.LOGIN_URL);

            var antiForgeryValues = await CookieHelper.ExtractAntiForgeryValues(initResponse);

            var postRequest = new HttpRequestMessage(HttpMethod.Post, Contants.LOGIN_URL);

            postRequest.Headers.Add("Cookie", new CookieHeaderValue(CookieHelper.AntiForgeryCookieName, antiForgeryValues.cookieValue).ToString());
            var modelData = new Dictionary <string, string>
            {
                { CookieHelper.AntiForgeryFieldName, antiForgeryValues.fieldValue },
                { "Username", this.Email },
                { "Password", this.Password }
            };

            postRequest.Content = new FormUrlEncodedContent(modelData);

            await httpClient.SendAsync(postRequest);

            var cookies = cookieContainer.GetCookies(new System.Uri(Contants.LOGIN_URL));

            if (cookies.Count <= 1)
            {
                App.Current.MainPage.DisplayAlert("Login Failed", "Invalid Email or Password. Please try again!", "OK");
                return;
            }

            UserInfo.CookieContainer = cookieContainer;
            await App.Current.MainPage.Navigation.PushModalAsync(new MainPageView());
        }