private async void clickBtnLogIn(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(username.Text) || string.IsNullOrEmpty(username.Text)) { Snackbar bar = Snackbar.Make(parentLayout, "Fill username", Snackbar.LengthLong); bar.SetText(Html.FromHtml("<font color=\"#000000\">Fill username</font>")); Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)bar.View; layout.SetMinimumHeight(100); layout.SetBackgroundColor(Android.Graphics.Color.White); layout.TextAlignment = TextAlignment.Center; layout.ScrollBarSize = 16; bar.SetActionTextColor(Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Red)); bar.SetDuration(Snackbar.LengthLong); bar.SetAction("Ok", (v) => { }); bar.Show(); } else if (string.IsNullOrWhiteSpace(password.Text) || string.IsNullOrEmpty(password.Text)) { Snackbar bar = Snackbar.Make(parentLayout, Html.FromHtml("<font color=\"#000000\">Fill password</font>"), Snackbar.LengthLong); Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)bar.View; layout.SetMinimumHeight(100); layout.SetBackgroundColor(Android.Graphics.Color.White); layout.TextAlignment = TextAlignment.Center; layout.ScrollBarSize = 16; bar.SetActionTextColor(Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Red)); bar.SetDuration(Snackbar.LengthLong); bar.SetAction("Ok", (v) => { }); bar.Show(); } else { var progressDialog = new ProgressDialog(this); try { progressDialog.SetIcon(2130968582); progressDialog.SetCancelable(true); progressDialog.SetMessage("Please wait!"); progressDialog.SetProgressStyle(ProgressDialogStyle.Spinner); progressDialog.Show(); var client = new HttpClient(); var keyValueLogIn = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("username", username.Text), new KeyValuePair <string, string>("password", password.Text), new KeyValuePair <string, string>("grant_type", "password") }; var request = new HttpRequestMessage(HttpMethod.Post, "UrlApiToken"); request.Content = new FormUrlEncodedContent(keyValueLogIn); var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { TokenModel tokenFromServer = JsonConvert.DeserializeObject <TokenModel>(content); var jsonContent = JsonConvert.SerializeObject(username.Text); var LogIncontent = new StringContent(jsonContent, Encoding.ASCII, "application/json"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenFromServer.access_token); response = await client.PostAsync("UrlApi" + "api/Account/userdata", LogIncontent); var userData = await response.Content.ReadAsStringAsync(); User user = JsonConvert.DeserializeObject <User>(userData); var userSession = new UserSession { FirstName = user.FirstName, LastName = user.LastName, userId = user.userId, Token = tokenFromServer.access_token, user_image = user.user_image }; con.Insert(userSession); progressDialog.Cancel(); StartActivity(typeof(MainActivity)); } else { LogInError logInError = JsonConvert.DeserializeObject <LogInError>(content); var alertLogInError = new Android.App.AlertDialog.Builder(this); alertLogInError.SetTitle(logInError.error); alertLogInError.SetMessage(logInError.error_description); username.Text = ""; password.Text = ""; Snackbar bar = Snackbar.Make(parentLayout, Html.FromHtml("<font color=\"#000000\">" + logInError.error + " - " + logInError.error_description + "</font>"), Snackbar.LengthLong); Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)bar.View; layout.SetMinimumHeight(100); layout.SetBackgroundColor(Android.Graphics.Color.White); layout.TextAlignment = TextAlignment.Center; layout.ScrollBarSize = 16; bar.SetActionTextColor(Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Red)); bar.SetDuration(Snackbar.LengthLong); bar.SetAction("Ok", (v) => { }); bar.Show(); progressDialog.Cancel(); } } catch (HttpRequestException httpEx) { Snackbar bar = Snackbar.Make(parentLayout, Html.FromHtml("<font color=\"#000000\">Please check your internet connection!</font>"), Snackbar.LengthLong); Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)bar.View; layout.SetMinimumHeight(100); layout.SetBackgroundColor(Android.Graphics.Color.White); layout.TextAlignment = TextAlignment.Center; layout.ScrollBarSize = 16; bar.SetActionTextColor(Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Red)); bar.SetDuration(Snackbar.LengthIndefinite); bar.SetAction("Ok", (v) => { }); bar.Show(); progressDialog.Cancel(); } catch (Exception ex) { Snackbar bar = Snackbar.Make(parentLayout, Html.FromHtml("<font color=\"#000000\">Error: " + ex + "</font>"), Snackbar.LengthLong); Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)bar.View; layout.SetMinimumHeight(100); layout.SetBackgroundColor(Android.Graphics.Color.White); layout.TextAlignment = TextAlignment.Center; layout.ScrollBarSize = 16; bar.SetActionTextColor(Android.Content.Res.ColorStateList.ValueOf(Android.Graphics.Color.Red)); bar.SetDuration(Snackbar.LengthIndefinite); bar.SetAction("Ok", (v) => { }); bar.Show(); progressDialog.Cancel(); } } }
public async Task <ActionResult> Index(LogIn logIn) { string SECRETKEY = ConfigurationManager.AppSettings["captchaSecret"]; string userResponse = logIn.gRecaptchaResponse; var webClient = new WebClient(); string verification = webClient.DownloadString(ConfigurationManager.AppSettings["captchaLink"] + "secret=" + SECRETKEY + "&response=" + userResponse); var verificationJson = JsonConvert.DeserializeObject <CaptchaResponse>(verification); bool rezultati = verificationJson.success; var httpClient = new HttpClient(); var jsonCaptchaLogs = new Logs { action = verificationJson.action, challenge_ts = verificationJson.challenge_ts, score = verificationJson.score, time_accessed = DateTime.Now, ip = Request.UserHostAddress, hostname = Request.UserHostName, success = verificationJson.success, url = Request.Url.ToString() }; var captchaContent = new StringContent(JsonConvert.SerializeObject(jsonCaptchaLogs), Encoding.ASCII, "application/json"); await httpClient.PostAsync(ConfigurationManager.AppSettings["url"] + "api/logs/saveRecaptchaLogs", captchaContent); if (!rezultati) { TempData["LogInError"] = "RecaptchaError"; return(RedirectToAction("Index")); } if (!ModelState.IsValid) { return(View(logIn)); } try { var credentialsToBeSend = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("username", logIn.username), new KeyValuePair <string, string>("password", logIn.password), new KeyValuePair <string, string>("grant_type", "password") }; var request = new HttpRequestMessage(HttpMethod.Post, ConfigurationManager.AppSettings["url"] + "Token"); request.Content = new FormUrlEncodedContent(credentialsToBeSend); var client = new HttpClient(); var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); if (response.StatusCode == (HttpStatusCode)429) { TempData["LogInError"] = "429"; return(RedirectToAction("Index")); } LogInError deserializeContent = JsonConvert.DeserializeObject <LogInError>(content); if (deserializeContent.error != null) { TempData["LogInError"] = deserializeContent.error; TempData["LockError"] = deserializeContent.error_description; return(RedirectToAction("Index")); } else { TokenResponseApi tokenResponse = JsonConvert.DeserializeObject <TokenResponseApi>(content); Session["Access_Token"] = tokenResponse.access_token; string username = logIn.username; var jsonContent = JsonConvert.SerializeObject(username); var LogIncontent = new StringContent(jsonContent, Encoding.ASCII, "application/json"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Session["Access_Token"].ToString()); response = await client.PostAsync(ConfigurationManager.AppSettings["url"] + "api/Account/userdata", LogIncontent); var userData = await response.Content.ReadAsStringAsync(); User user = JsonConvert.DeserializeObject <User>(userData); user.user_image = "data:Image/png;base64," + user.user_image; Session["userSession"] = user; return(RedirectToAction("Main", "Home")); } } catch (Exception ex) { TempData["LogInError"] = ex; return(RedirectToAction("Index", "LogIn")); } }
protected virtual void OnLogInError() => LogInError?.Invoke();