Пример #1
0
        public void SignUp(User user)
        {
            if (string.IsNullOrWhiteSpace(user.Email))
            {
                throw new Exception("Email can not be empty.");
            }
            if (string.IsNullOrWhiteSpace(user.Password))
            {
                throw new Exception("Password can not be empty.");
            }
            if (string.IsNullOrWhiteSpace(user.FirstName))
            {
                throw new Exception("Firstname can not be empty.");
            }
            if (string.IsNullOrWhiteSpace(user.LastName))
            {
                throw new Exception("Lastname can not be empty.");
            }
            if (user.Gender != 'm' && user.Gender != 'f')
            {
                throw new Exception("Invalid Gender supplied");
            }

            user.Password     = _encryptionManager.Encrypt(user.Password);
            user.UserGroup_ID = API.Models.Type.User;

            using (var scope = _scopeFactory.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService <Context>();

                dbContext.Users.Add(user);
                dbContext.SaveChanges();
            }
        }
Пример #2
0
        public async Task <InstanceViewModel> AddInstance(InstanceViewModel newInstance)
        {
            if (String.IsNullOrEmpty(newInstance.InstanceName) ||
                String.IsNullOrEmpty(newInstance.ServerName) ||
                String.IsNullOrEmpty(newInstance.Login) ||
                String.IsNullOrEmpty(newInstance.Password) ||
                !newInstance.IsWindowsAuthentication.HasValue)
            {
                throw new InvalidModelException("Instance name, Server name, Login, Password, and authentication type are required.");
            }

            string encryptionKey;
            var    encryptedPassword = encryptionManager.Encrypt(out encryptionKey, newInstance.Password);

            using (var context = new MsSqlMonitorEntities())
            {
                if (null != await context.Instances.FirstOrDefaultAsync(inst => inst.ServerName.ToUpper() == newInstance.ServerName.ToUpper() &&
                                                                        inst.InstanceName.ToUpper() == newInstance.InstanceName.ToUpper()))
                {
                    throw new ObjectAlreadyExistException("Instance already exist in the system.");
                }

                Instance result = new Instance()
                {
                    ServerName     = newInstance.ServerName,
                    InstanceName   = newInstance.InstanceName,
                    Password       = encryptedPassword,
                    EncryptionKey  = encryptionKey,
                    Login          = newInstance.Login,
                    Authentication = newInstance.IsWindowsAuthentication.Value ? AuthenticationType.Windows : AuthenticationType.Sql
                };
                context.Instances.Add(result);
                await context.SaveChangesAsync();

                return(result.ToViewModel());
            }
        }
        public async Task <IFileResponse> UploadBioVideoAsync(UploadBioVideoView model)
        {
            try
            {
                var userInfo = await _appDbContext.UserInfos.Include(k => k.User).FirstOrDefaultAsync(k => k.Id == model.UserId);

                var data = new File()
                {
                    IdGuid       = Guid.NewGuid(),
                    SizeMb       = GetFileSize(model.File.Length),
                    Name         = model.File.FileName,
                    ProviderName = "SqlProvider",
                    ExtraParams  = model.ExtraParams,
                    Created      = DateTime.UtcNow,
                    MimeType     = model.File.ContentType,
                    Modified     = DateTime.UtcNow,
                    CreatedBy    = userInfo.Email,
                    ModifiedBy   = userInfo.Email
                };

                var savedEntity = (await _appDbContext.Files.AddAsync(data)).Entity;

                await _appDbContext.SaveChangesAsync();

                var profile = await _appDbContext.Profiles.FirstOrDefaultAsync(k => k.Id == model.UserId);

                profile.ExpressYourselfUrl = _encryption.Encrypt(savedEntity.Id.ToString());

                await _appDbContext.SaveChangesAsync();

                var fileDb = new FileDB()
                {
                    Id    = savedEntity.IdGuid,
                    Bytes = StreamToBytes(model.File.OpenReadStream())
                };

                await _fileDbContext.FileDB.AddAsync(fileDb);

                await _fileDbContext.SaveChangesAsync();

                return(new FileResponse(true, "Success", HttpStatusCode.Accepted));
            }
            catch (Exception e)
            {
                return(new FileResponse(e));
            }
        }
        public virtual ProtectedSecret Protect(string keyName, ClearSecret secret)
        {
            if (secret == null)
            {
                throw new ArgumentNullException(nameof(secret));
            }

            var encryptedSecret = _encryptionManager.Encrypt(keyName, SecretEncoding.GetBytes(secret.Value));

            return(new ProtectedSecret
            {
                ApplicationName = secret.ApplicationName,
                MasterKeyId = keyName,
                InitialisationVector = encryptedSecret.InitialisationVector,
                Name = secret.Name,
                ProtectedDocumentKey = encryptedSecret.EncryptedDataKey,
                ProtectedSecretValue = encryptedSecret.EncryptedData
            });
        }