예제 #1
0
        private async Task <HttpResponseMessage> SendCredencials(LoginInputDataModel model)
        {
            HttpClient          client   = _api.Initial();
            HttpResponseMessage response = new HttpResponseMessage();

            try
            {
                HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, $"" + client.BaseAddress + "api/Logins");

                var options = new
                {
                    Email    = model.Email,
                    Password = model.Password
                };

                var stringData = JsonConvert.SerializeObject(options);
                var content    = new StringContent(stringData, Encoding.UTF8, "application/json");
                requestMessage.Content = new StringContent(stringData, Encoding.UTF8, "application/json");
                response = await client.SendAsync(requestMessage);

                return(response);
            }
            catch (Exception ex)
            {
                response.StatusCode = HttpStatusCode.InternalServerError;
                Console.WriteLine(ex);
                return(response);
            }
        }
예제 #2
0
        public async Task <LoginOutputDataModel> CheckCredencial(LoginInputDataModel model)
        {
            var responseModel   = new LoginOutputDataModel();
            var responseMessage = await SendCredencials(model);

            if (responseMessage.IsSuccessStatusCode)
            {
                var result = responseMessage.Content.ReadAsStringAsync().Result;
                var json   = JsonConvert.DeserializeObject(result);

                responseModel.APIResponse = (bool)json;
                responseModel.Status      = true;
                return(responseModel);
            }
            else
            {
                responseModel.Status = false;
                responseModel.Error  = "Server not responding";
                return(responseModel);
            }
        }
        public async Task <IActionResult> Index(LoginViewModels model)
        {
            if (ModelState.IsValid)
            {
                var loginModelInputData = new LoginInputDataModel
                {
                    Email    = model.Input.Email,
                    Password = _hash.EncryptString(model.Input.Password)
                };

                var login = await _loginServices.CheckCredencial(loginModelInputData);

                if (!login.Status)
                {
                    model.ErrorMessage = login.Error;
                    return(View(model));
                }

                if (!login.APIResponse)
                {
                    model.ErrorMessage = "Incorrect credentials";
                    return(View(model));
                }

                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Email, model.Input.Email)
                };

                var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                var authProperties = new AuthenticationProperties();
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);

                return(RedirectToAction("Create", "Messages"));
            }
            model.ErrorMessage = "Wrong Data";
            return(View(model));
        }