Esempio n. 1
0
        public JsonWebTokenDto GenerateJsonWebToken(object jwtTokenData)
        {
            IdentityOptions _options = new IdentityOptions();

            var claims = new[]
            {
                new Claim(_options.ClaimsIdentity.UserIdClaimType, jwtTokenData.ToString()),
                new Claim(ClaimTypes.UserData, JsonConvert.SerializeObject(jwtTokenData, new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }), null)
            };

            var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSettings.JwtSecretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(
                issuer: AppSettings.JwtIssuer,
                audience: AppSettings.JwtAudience,
                claims: claims,
                expires: DateTime.Now.AddDays(7),
                signingCredentials: creds);

            string          jwtToken     = new JwtSecurityTokenHandler().WriteToken(token);
            JsonWebTokenDto jsonWebToken = new JsonWebTokenDto(jwtToken);

            return(jsonWebToken);
        }
Esempio n. 2
0
        public async Task Register_CallWithMockedIAuthentication_ReturnsOkObjectResult()
        {
            // Arrange
            var mock            = new Mock <IAuthentication>();
            var registrationDto = new RegistrationDto()
            {
                Username             = "******",
                Email                = "*****@*****.**",
                Password             = "******",
                PasswordConfirmation = "password",
                Locale               = "en-US",
                Timezone             = "Asia/Calcutta",
            };

            var jsonWebToken = new JsonWebTokenDto("expected_token");

            mock.Setup(auth => auth.RegisterUserAsync(registrationDto)).ReturnsAsync(jsonWebToken);
            var authController = new AuthController(mock.Object);

            // Act
            var result = await authController.Register(registrationDto);

            var response     = (OkObjectResult)result;
            var objectResult = (ResponseDto <JsonWebTokenDto>)response.Value;

            // Assert
            Assert.Equal(jsonWebToken.Token, objectResult.Data.Token);
        }
Esempio n. 3
0
        /// <summary>
        /// Create the main object to return
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        private JsonWebTokenDto CreateJwt(User user)
        {
            var jwt = new JsonWebTokenDto
            {
                AccessToken  = GenerateAccessToken(user),
                RefreshToken = GenerateRefreshToken(),
                ExpiresIn    = DateTime.UtcNow.AddMinutes(_expirationTokenIn)
            };

            return(jwt);
        }
Esempio n. 4
0
        public async Task Login_CallWithMockedIAuthentication_ReturnsOkObjectResult()
        {
            // Arrange
            var mock     = new Mock <IAuthentication>();
            var loginDto = new LoginDto()
            {
                Email    = "*****@*****.**",
                Password = "******",
            };

            var jsonWebToken = new JsonWebTokenDto("expected_token");

            mock.Setup(auth => auth.LoginUserAsync(loginDto)).ReturnsAsync(jsonWebToken);
            var authController = new AuthController(mock.Object);

            // Act
            var result = await authController.Login(loginDto);

            var response     = (OkObjectResult)result;
            var objectResult = (ResponseDto <JsonWebTokenDto>)response.Value;

            // Assert
            Assert.Equal(jsonWebToken.Token, objectResult.Data.Token);
        }