public async Task <ActionResult> Login(LoginViewModel model) { if (ModelState.IsValid) { const String URI_ADDRESS = "api/Accounts/Login"; try { var client = GlobalWebApiClient.GetClient(); var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("email", model.Email), new KeyValuePair <string, string>("password", model.Password) }); var response = await client.PostAsync(URI_ADDRESS, content); if (response.IsSuccessStatusCode) { var tokenResponse = await response.Content.ReadAsStringAsync(); var vmTokenResponse = new TokenResponseViewModel { AccessToken = tokenResponse }; GlobalWebApiClient.StoreToken(vmTokenResponse); if (vmTokenResponse != null) { Session["Email"] = vmTokenResponse.Username.ToString(); var clientUser = GlobalWebApiClient.GetClient(); var resultLoggedUser = clientUser.GetAsync("api/Accounts/LoggedUser").Result; var resultLoggedEmail = clientUser.GetAsync(@"api/profiles/profile/" + Session["Email"].ToString().EncodeBase64()).Result; if (!resultLoggedEmail.IsSuccessStatusCode) { if (resultLoggedUser.IsSuccessStatusCode) { if (resultLoggedUser != null) { var vmUser = await resultLoggedUser.Content.ReadAsAsync <ProfileViewModel>(); Session["UserId"] = vmUser.ProfileVmId; return(RedirectToAction("Index", "Home")); } } } else { var vmUser = await resultLoggedEmail.Content.ReadAsAsync <ProfileViewModel>(); Session["UserId"] = vmUser.ProfileVmId; return(RedirectToAction("Index", "Home")); } } return(View(model)); } return(View(model)); } catch (Exception ex) { var result = ex.Message; } } return(View(model)); }