public IActionResult Authenticate([FromBody] UserDto userDto)
        {
            var response = new UserDto();

            var serviceResponse = _userRepository.Authenticate(userDto.Username, GenericService.Encrypt(userDto.Password, _appSettings.Value.KeyForEncrypting));

            if (serviceResponse == null)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            // authentication successful so generate jwt token
            var tokenHandler = new JwtSecurityTokenHandler();

            var key = Encoding.ASCII.GetBytes(_appSettings.Value.Secret);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, serviceResponse.UniqueId.ToString())
                }),
                Expires            = DateTime.UtcNow.AddDays(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            serviceResponse.Token = tokenHandler.WriteToken(token);

            response = _mapper.Map <UserDto>(serviceResponse);

            return(Ok(response));
        }
Beispiel #2
0
        public void EncryptWithEmptyPropertiesOK()
        {
            // all of these objects should have null properties, this should not cause secret to blow up
            var secret = Guid.NewGuid().ToString("n");

            try
            {
                var generic = new GenericService();
                generic.Configuration["test"] = string.Empty;
                generic.Encrypt(secret);
                generic.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("generic failed with empty values");
            }

            try
            {
                var file = new FileService
                {
                    Path = string.Empty,
                };
                file.Encrypt(secret);
                file.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("file failed with empty values");
            }

            try
            {
                var luis = new LuisService
                {
                    SubscriptionKey = string.Empty,
                };
                luis.Encrypt(secret);
                luis.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("luis failed with empty values");
            }

            try
            {
                var dispatch = new DispatchService
                {
                    SubscriptionKey = string.Empty,
                };
                dispatch.Encrypt(secret);
                dispatch.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("dispatch failed with empty values");
            }

            try
            {
                var insights = new AppInsightsService
                {
                    InstrumentationKey = string.Empty,
                };
                insights.Encrypt(secret);
                insights.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("insights failed with empty values");
            }

            try
            {
                var bot = new BotService();
                bot.Encrypt(secret);
                bot.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("bot failed with empty values");
            }

            try
            {
                var cosmos = new CosmosDbService
                {
                    Key = string.Empty,
                };
                cosmos.Encrypt(secret);
                cosmos.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("cosmos failed with empty values");
            }

            try
            {
                var qna = new QnAMakerService
                {
                    SubscriptionKey = string.Empty,
                };
                qna.Encrypt(secret);
                qna.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("qna failed with empty values");
            }

            try
            {
                var blob = new BlobStorageService
                {
                    ConnectionString = string.Empty,
                };
                blob.Encrypt(secret);
                blob.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("blob failed with empty values");
            }

            try
            {
                var endpoint = new EndpointService
                {
                    AppPassword = string.Empty,
                };
                endpoint.Encrypt(secret);
                endpoint.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("endpoint failed with empty values");
            }
        }
Beispiel #3
0
        public void EncryptWithNullPropertiesOK()
        {
            // all of these objects should have null properties, this should not cause secret to blow up
            var secret = Guid.NewGuid().ToString("n");

            try
            {
                var generic = new GenericService();
                generic.Encrypt(secret);
                generic.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("generic failed with empty values");
            }

            try
            {
                var file = new FileService();
                file.Encrypt(secret);
                file.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("file failed with empty values");
            }

            try
            {
                var luis = new LuisService();
                luis.Encrypt(secret);
                luis.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("luis failed with empty values");
            }

            try
            {
                var dispatch = new DispatchService();
                dispatch.Encrypt(secret);
                dispatch.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("dispatch failed with empty values");
            }

            try
            {
                var insights = new AppInsightsService();
                insights.Encrypt(secret);
                insights.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("insights failed with empty values");
            }

            try
            {
                var bot = new BotService();
                bot.Encrypt(secret);
                bot.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("bot failed with empty values");
            }

            try
            {
                var cosmos = new CosmosDbService();
                cosmos.Encrypt(secret);
                cosmos.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("cosmos failed with empty values");
            }

            try
            {
                var qna = new QnAMakerService();
                qna.Encrypt(secret);
                qna.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("qna failed with empty values");
            }

            try
            {
                var blob = new BlobStorageService();
                blob.Encrypt(secret);
                blob.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("blob failed with empty values");
            }

            try
            {
                var endpoint = new EndpointService();
                endpoint.Encrypt(secret);
                endpoint.Decrypt(secret);
            }
            catch (Exception)
            {
                Assert.Fail("endpoint failed with empty values");
            }
        }