Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("File,Id,EmailSubject,EmailText,DaysBeforeEmail, Group")] SettingsViewModel settingsViewModel)
        {
            Settings settings = _mapper.Map <Settings>(settingsViewModel);

            PopulateModelWithSelectedValues(settingsViewModel.Group, settings);
            var hasSettingsCreated = _context.Settings.Any(x => x.Group.Id.ToString() == settingsViewModel.Group);

            if (hasSettingsCreated)
            {
                ModelState.AddModelError("Group", "Setting already created with this group. Please select another group.");
            }
            if (ModelState.IsValid)
            {
                _context.Settings.Add(settings);
                await _context.SaveChangesAsync();

                TempData["Msg"] = "Settings for " + settings.Group.Name + " sucessfully added";
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                //ValidationResult result = new ValidationResult("Please select a user.");
                LoadFieldsViewModel(settingsViewModel);
                return(View(settingsViewModel));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Application application)
        {
            if (ModelState.IsValid)
            {
                _context.Add(application);
                await _context.SaveChangesAsync();

                TempData["Msg"] = application.Name + " sucessfully created.";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(application));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Server server)
        {
            if (ModelState.IsValid)
            {
                _context.Add(server);
                await _context.SaveChangesAsync();

                TempData["Msg"] = server.Name + " sucessfully added";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(server));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,Name,Email,PhoneExtension,IsAdmin")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                TempData["Msg"] = user.Name + " sucessfully added";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
        public async Task <IActionResult> Create([Bind("Id,Name, UserIds")] GroupViewModel groupViewModel)
        {
            if (ModelState.IsValid)
            {
                Group group = _mapper.Map <Group>(groupViewModel);
                PopulateModelWithSelectedValues(groupViewModel.UserIds, group);
                _context.Add(@group);
                await _context.SaveChangesAsync();

                TempData["Msg"] = groupViewModel.Name + " sucessfully added";
                return(RedirectToAction(nameof(Index)));
            }
            LoadFieldsViewModel(groupViewModel);
            return(View(groupViewModel));
        }
        public async Task <IActionResult> Create(IFormFile file, [Bind("Id,File,Guid,Extension,Name,Password,GroupIds,ApplicationIds,ServerIds,SubjectName,SerialNumber,Issuer,Location,Purpose,Environment,From,To,InstallationLink,OtherInfo,Active")] CertificateViewModel certificateViewModel)
        {
            if (ModelState.IsValid)
            {
                var filePath = "";
                try
                {
                    var outputFolder = _configuration.GetSection("AppSettings")["CertificatesPath"];

                    // Extract file name from whatever was posted by browser
                    var fileName      = Guid.NewGuid().ToString();
                    var fileExtension = System.IO.Path.GetExtension(file.FileName);
                    filePath = System.IO.Path.GetFullPath(outputFolder + fileName + fileExtension);

                    if (fileExtension != ".p12" && fileExtension != ".pfx" && fileExtension != ".cer")
                    {
                        throw new Exception("Unsupported file");
                    }

                    // If file with same name exists delete it
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }

                    // Create new local file and copy contents of uploaded file
                    using (var localFile = System.IO.File.OpenWrite(filePath))
                        using (var uploadedFile = file.OpenReadStream())
                            uploadedFile.CopyTo(localFile);

                    switch (fileExtension)
                    {
                    case ".p12":
                    case ".pfx":
                        X509Certificate2 certificatep12 = new X509Certificate2(@filePath, certificateViewModel.Password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
                        certificateViewModel.From         = Convert.ToDateTime(certificatep12.GetEffectiveDateString());
                        certificateViewModel.To           = Convert.ToDateTime(certificatep12.GetExpirationDateString());
                        certificateViewModel.SerialNumber = certificatep12.SerialNumber;
                        certificateViewModel.SubjectName  = certificatep12.Subject;
                        certificateViewModel.Issuer       = certificatep12.Issuer;
                        break;

                    case ".cer":
                        // Load the certificate into an X509Certificate object.
                        X509Certificate certificateCer = X509Certificate.CreateFromCertFile(filePath);
                        certificateViewModel.From         = Convert.ToDateTime(certificateCer.GetEffectiveDateString());
                        certificateViewModel.To           = Convert.ToDateTime(certificateCer.GetExpirationDateString());
                        certificateViewModel.SerialNumber = certificateCer.GetSerialNumberString();
                        certificateViewModel.SubjectName  = certificateCer.Subject;
                        certificateViewModel.Issuer       = certificateCer.Issuer;
                        break;

                    default:
                        throw new Exception("Unsupported file");
                    }
                    certificateViewModel.Guid      = fileName;
                    certificateViewModel.Extension = fileExtension;

                    Certificate certificate = _mapper.Map <Certificate>(certificateViewModel);
                    PopulateModelWithSelectedValues(certificateViewModel.ApplicationIds, certificateViewModel.ServerIds, certificateViewModel.GroupIds, certificate);
                    _context.Certificate.Add(certificate);
                    await _context.SaveChangesAsync();

                    TempData["Msg"] = certificateViewModel.Name + " sucessfully added";
                }
                catch (Exception ex)
                {
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }

                    if (ex.Message.ToLower().Contains("password"))
                    {
                        TempData["Msg"] = "Error: Invalid certificate password";
                    }
                    else
                    {
                        TempData["Msg"] = "Error: " + ex.Message;
                    }
                    return(RedirectToAction(nameof(Create)));
                }

                return(RedirectToAction(nameof(Index)));
            }
            LoadFieldsViewModel(certificateViewModel);
            return(View(certificateViewModel));
        }