Exemplo n.º 1
0
    public void ReturnRandomValue()
    {
        // Arrange & Act
        var result = Sha512Helper.GetRandomValue();

        _output.WriteLine("Generated hash: " + result);

        // Assert
        Assert.Equal(128, result.Length);
    }
Exemplo n.º 2
0
    public async Task <IActionResult> Edit(CertificateViewModel cvm, CancellationToken cancellationToken)
    {
        var _user = await _userManager.GetUserAsync(User);

        if (ModelState.IsValid)
        {
            if (cvm.File != null)
            {
                if (cvm.File.CheckFileExtension(_fileSettings.Expansion))
                {
                    ModelState.AddModelError("File", _localizer["FileExtensionError"]);
                    return(View(cvm));
                }

                using (var binaryReader = new BinaryReader(cvm.File.OpenReadStream()))
                {
                    if (cvm.File.CheckFileSize(_fileSettings.MinSize, _fileSettings.SizeLimit))
                    {
                        ModelState.AddModelError("File", _localizer["FileSizeError"]);
                        return(View(cvm));
                    }

                    var path = $"/private/{Sha512Helper.GetRandomValue()}.jpg";

                    using (var stream = System.IO.File.Create(_appEnvironment.WebRootPath + path))
                    {
                        await cvm.File.CopyToAsync(stream, cancellationToken);
                    }

                    cvm.Path = path;
                }
            }

            try
            {
                await _certificateService.UpdateCertificateAsync(cvm, _user.Id, cancellationToken);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                _logger.LogError("An error occurred while updating the certificate: " + ex.Message);

                return(RedirectToAction(nameof(Index)));
            }

            _logger.LogInformation($"Certificate {cvm.Id} changed by User {_user.Id}");

            return(RedirectToAction(nameof(Details), new { id = cvm.Id }));
        }

        return(View(cvm));
    }
Exemplo n.º 3
0
    public async Task <IActionResult> Create(CertificateViewModel cvm, CancellationToken cancellationToken)
    {
        var _user = await _userManager.GetUserAsync(User);

        if (cvm.File is null)
        {
            ModelState.AddModelError("File", _localizer["Required"]);
        }

        if (ModelState.IsValid)
        {
            if (cvm.File.CheckFileExtension(_fileSettings.Expansion))
            {
                ModelState.AddModelError("File", _localizer["FileExtensionError"]);
                return(View());
            }

            using (var binaryReader = new BinaryReader(cvm.File.OpenReadStream()))
            {
                if (cvm.File.CheckFileSize(_fileSettings.MinSize, _fileSettings.SizeLimit))
                {
                    ModelState.AddModelError("File", _localizer["FileSizeError"]);
                    return(View());
                }

                var path = $"/private/{Sha512Helper.GetRandomValue()}.jpg";

                using (var stream = System.IO.File.Create(_appEnvironment.WebRootPath + path))
                {
                    await cvm.File.CopyToAsync(stream, cancellationToken);
                }

                cvm.Path = path;
            }

            await _certificateService.CreateCertificateAsync(cvm, _user.Id, cancellationToken);

            _logger.LogInformation($"New certificate created by User {_user.Id}");

            return(RedirectToAction(nameof(Index)));
        }

        return(View());
    }