示例#1
0
        public async Task <string> GenerateApiKeyAsync()
        {
            try
            {
                Guid apiKey     = Guid.NewGuid();
                var  salt       = _hashService.GetSalt();
                var  apiKeyHash = _hashService.CalculateHash(salt, apiKey.ToString());

                var authorizationKey = await _databaseRepository.GetApiKeyAsync();

                if (authorizationKey == null)
                {
                    throw new BadRequestException("Authorization key not setted");
                }
                else
                {
                    authorizationKey.SetKey(salt, apiKeyHash);
                }

                await _databaseRepository.SetApiKeyAsync(authorizationKey);

                return(apiKey.ToString());
            }
            catch (Exception exception)
            {
                Log.Error(exception.Message);
                throw new BadRequestException("Error during generation new Api key");
            }
        }