Exemple #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                UserName = Request.Form[nameof(UserName)];
                Password = Request.Form[nameof(Password)];

                try
                {
                    var client   = clientFactory.CreateClient("MarinaApi");
                    var response = await client.PostAsJsonAsync("authorization/login", new { UserName, Password });

                    if (response.IsSuccessStatusCode)
                    {
                        var json = await response.Content.ReadAsStringAsync();

                        var authorization = JsonConvert.DeserializeObject <AuthDto>(json);
                        sessionHelper.SignIn(authorization.Token, authorization.UserId, UserName);
                        return(RedirectToPage("./Lease"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Invalid login attempt."); //TODO: make this display
                        return(Page());
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

            return(Page());
        }