public async Task <ActionResult> Login(LoginViewModel loginViewModel) { var nvc = new List <KeyValuePair <string, string> >(); nvc.Add(new KeyValuePair <string, string>("grant_type", "password")); nvc.Add(new KeyValuePair <string, string>("username", loginViewModel.UserName)); nvc.Add(new KeyValuePair <string, string>("password", loginViewModel.Password)); var client = new HttpClient(); var req = new HttpRequestMessage(HttpMethod.Post, "https://localhost:44398/api/token") { Content = new FormUrlEncodedContent(nvc) }; var res = await client.SendAsync(req); if (res.StatusCode == HttpStatusCode.OK) { string responseBody = await res.Content.ReadAsStringAsync(); UserAuth usr = JsonConvert.DeserializeObject <UserAuth>(responseBody); if (usr.userName == "admin") { Cookiez.AddCookie(usr.userName, "Admin", Response); return(RedirectToAction("Order", "AdminOrder")); } Cookiez.AddCookie(usr.userName, "User", Response); return(RedirectToAction("Index", "Home")); } ModelState.AddModelError("SignUp Error", "Invalid data"); return(View()); }
public ActionResult Logout() { Cookiez.RemoveCookie(HttpContext.ApplicationInstance.Response); return(RedirectToAction("Index", "Home")); }