Пример #1
0
        public async Task<ActionResult> LogIn(UsersLogInModel model)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }

            using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(Constants.ApiBaseUri);
                httpClient.SetBasicAuthentication(model.Username, model.Password);

                var response = await httpClient.GetAsync("token");

                if (response.IsSuccessStatusCode)
                {
                    var tokenResponse = await response.Content.ReadAsStringAsync();
                    var json = JObject.Parse(tokenResponse);
                    var token = json["access_token"].ToString();

                    Session[Constants.SessionTokenKey] = token;
                    
                    FormsAuthentication.SetAuthCookie(model.Username, createPersistentCookie: true);
                    return Redirect("~/");
                }
                else // could check specific error code here
                {
                    ModelState.AddModelError("", "The username and password provided do not match any accounts on record.");
                    return View();
                }
            }
        }