[AllowAnonymous] //Permite que usuários anônimos acessem o método. Para solicitação do controller via json. //[ValidateAntiForgeryToken] //Protege a aplicação no caso de solicitações http maliciosas. public async Task <ActionResult> GeraTokenWebApi() { try { var client = WebApiHttpClient.GetClient(); HttpContent content = new StringContent( "username="******"&password="******"&grant_type=password", System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"); response = client.PostAsync("/Token", content).Result; if (response.IsSuccessStatusCode) { TokenResponse tokenResponse = await response.Content.ReadAsAsync <TokenResponse>(); WebApiHttpClient.storeToken(tokenResponse); //return RedirectToAction("UsuarioTeste", "Token"); return(Json(new { tokenResponse.AccessToken }, JsonRequestBehavior.AllowGet)); } else { return(Content("Ocorreu um erro: " + response.StatusCode)); } } catch { return(Content("Ocorreu um erro na geração do token.")); } }
public async Task <ActionResult> Login(LoginViewModel model) { if (!ModelState.IsValid) { StringBuilder strB = new StringBuilder(500); foreach (ModelState modelState in ModelState.Values) { foreach (ModelError error in modelState.Errors) { strB.Append(error.ErrorMessage + "."); } } Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; return(Content(strB.ToString())); } try { var client = WebApiHttpClient.GetClient(); string json = JsonConvert.SerializeObject(model); var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); var httpResponse = await client.PostAsync("api/auth/login", httpContent); if (httpResponse.IsSuccessStatusCode) { TokenResponse tokenResponse = await httpResponse.Content.ReadAsAsync <TokenResponse>(); WebApiHttpClient.storeToken(tokenResponse); // return Content(tokenResponse.AccessToken); return(RedirectToAction("Index", "Home")); } else { Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; MessageResponse json2 = await httpResponse.Content.ReadAsAsync <MessageResponse>(); return(Content("" + json2.message)); } } catch (Exception ee) { Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest; return(Content("Sorry, an error occured." + ee.Message)); } }
public async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return(View(model)); } try { var client = WebApiHttpClient.GetClient(); string username = model.Email; string password = model.Password; HttpContent content = new StringContent( "grant_type=password&username="******"&password="******"application/x-www-form-urlencoded"); var response = await client.PostAsync("/Token", content); if (response.IsSuccessStatusCode) { //string contentResponse = await response.Content.ReadAsStringAsync(); //return Content(contentResponse); TokenResponse tokenResponse = await response.Content.ReadAsAsync <TokenResponse>(); WebApiHttpClient.storeToken(tokenResponse); //return Content(tokenResponse.AccessToken); return(RedirectToAction("Index", "Home")); } else { return(Content("Ocorreu um erro: " + response.StatusCode)); } } catch { return(Content("Ocorreu um erro.")); } }