예제 #1
0
        public void Login_Succeeds_OkObjectResult()
        {
            // Arrange
            var model = new LoginModel {
                Email = "*****@*****.**", Password = "******"
            };

            _accountManagerService.GetTokenWithClaimsPrincipal(model).ReturnsForAnyArgs(new TokenWithClaimsPrincipal());

            // Act
            Task <IActionResult> result = _authController.Login(model);

            // Assert
            Assert.IsType <OkObjectResult>(result.Result);
        }
예제 #2
0
        public async Task GetTokenWithClaimsPrincipal_UserNotFound_ReturnsNull()
        {
            // Arrange
            var model = new LoginModel {
                Email = "*****@*****.**", Password = "******"
            };

            _userManager.FindByEmailAsync(model.Email).Returns(Task.FromResult <JobApplicationUser>(null));

            // Act
            var result = await _accountManagerService.GetTokenWithClaimsPrincipal(model).ConfigureAwait(false);

            // Assert
            Assert.Null(result);
        }
        public async Task <IActionResult> Login([FromBody] LoginModel model)
        {
            try
            {
                var tokenWithClaimsPrincipal = await _accountManagerService.GetTokenWithClaimsPrincipal(model).ConfigureAwait(false);

                if (tokenWithClaimsPrincipal == null)
                {
                    return(BadRequest("Failed to Login"));
                }

                await HttpContext.SignInAsync(tokenWithClaimsPrincipal.ClaimsPrincipal, tokenWithClaimsPrincipal.AuthenticationProperties)
                .ConfigureAwait(false);

                return(Ok(tokenWithClaimsPrincipal.JwtResponse));
            }
            catch (Exception e)
            {
                _logger.LogError(LoggingEvents.Auth, e, $"Exception when trying to login");
                return(BadRequest(e));
            }
        }