Beispiel #1
0
        public async Task <IActionResult> Login(LoginInputModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var response = await _accountApiClient.LoginAsync(model);

                string result = await response.Content.ReadAsStringAsync();

                switch ((int)response.StatusCode)
                {
                case (int)HttpStatusCode.OK:
                    var jwt = JsonConvert.DeserializeObject <JwtOutputModel>(result);
                    _loginManager.Token        = jwt.Token;
                    _loginManager.RefreshToken = jwt.RefreshToken;

                    return(Redirect(returnUrl ?? "/Home/Index"));

                case (int)HttpStatusCode.BadRequest:
                    var badRequest = JsonConvert.DeserializeObject <BadRequest>(result);
                    foreach (var key in badRequest.Errors.Keys)
                    {
                        foreach (var value in badRequest.Errors[key])
                        {
                            ModelState.AddModelError(key, value);
                        }
                    }
                    break;

                case (int)HttpStatusCode.Conflict:
                case (int)HttpStatusCode.InternalServerError:
                    var errors = JsonConvert.DeserializeObject <ResponseOutputModel>(result);
                    ModelState.AddModelError("", errors.Message);
                    break;

                case (int)HttpStatusCode.Unauthorized:
                    var unauthorizeErrors = JsonConvert.DeserializeObject <ResponseOutputModel>(result);
                    ModelState.AddModelError("", unauthorizeErrors.Message);
                    break;
                }
            }
            return(View(model));
        }