Example #1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            ReturnUrl = returnUrl;

            if (ModelState.IsValid)
            {
                LoginDTO.ApartmentCode = "0000";
                var response = await loginClient.LoginAsync(LoginDTO);

                if (response == null || !response.Success)
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }

                var claims = new List <Claim>
                {
                    new Claim("token", response.Data.ToString()),
                };

                var claimsIdentity = new ClaimsIdentity(
                    claims, CookieAuthenticationDefaults.AuthenticationScheme);

                var authProperties = new AuthenticationProperties {
                    //AllowRefresh = <bool>,
                    // Refreshing the authentication session should be allowed.

                    //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                    // The time at which the authentication ticket expires. A
                    // value set here overrides the ExpireTimeSpan option of
                    // CookieAuthenticationOptions set with AddCookie.

                    //IsPersistent = true,
                    // Whether the authentication session is persisted across
                    // multiple requests. When used with cookies, controls
                    // whether the cookie's lifetime is absolute (matching the
                    // lifetime of the authentication ticket) or session-based.

                    //IssuedUtc = <DateTimeOffset>,
                    // The time at which the authentication ticket was issued.

                    //RedirectUri = <string>
                    // The full path or absolute URI to be used as an http
                    // redirect response value.
                };

                await HttpContext.SignInAsync(
                    CookieAuthenticationDefaults.AuthenticationScheme,
                    new ClaimsPrincipal(claimsIdentity),
                    authProperties);


                _logger.LogInformation(response.Data.ToString());

                return(LocalRedirect(Url.GetLocalUrl(returnUrl)));
            }

            // Something failed. Redisplay the form.
            return(Page());
        }
Example #2
0
 private async Task LoginAsync()
 {
     var loginPayload = new LoginRequest
     {
         Username = Request("Username"),
         Password = Request("Password")
     };
     await _loginClient
     .LoginAsync(loginPayload);
 }
Example #3
0
        public async Task Should_Return_OK_And_Token_When_Valid_User_Is_Trying_To_login()
        {
            var model = new
            {
                username = "******",
                password = "******"
            };

            var response = await _loginClient.LoginAsync(model);

            var responseDeserialized = await DeserializeAsync <LoginPostResponseDto>(response);

            response.EnsureSuccessStatusCode();

            responseDeserialized.Username.Should().NotBeNullOrEmpty();
            responseDeserialized.Cpf.Should().NotBeNullOrEmpty();
            responseDeserialized.Name.Should().NotBeNullOrEmpty();
            responseDeserialized.WalletId.Should().NotBeNullOrEmpty();
            responseDeserialized.Token.Should().NotBeNullOrEmpty();
        }