Ejemplo n.º 1
0
        public IActionResult Create(int apiResourceId)
        {
            var vm = new ApiSecretInputViewModel {
                ApiResourceId = apiResourceId, Expiration = DateTime.Today.AddYears(1)
            };

            return(View(vm));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(ApiSecretInputViewModel vm)
        {
            if (vm == null)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                var apiResource = await _dbContext.ApiResources.FindAsync(vm.ApiResourceId);

                if (apiResource == null)
                {
                    return(BadRequest());
                }

                var apiSecret = new ApiSecret
                {
                    ApiResource = apiResource,
                    Description = vm.Description,
                    Value       = vm.Value.Sha256(),
                    Expiration  = vm.Expiration
                };
                if (!string.IsNullOrEmpty(vm.Type))
                {
                    apiSecret.Type = vm.Type;
                }

                _dbContext.Set <ApiSecret>().Add(apiSecret);
                try
                {
                    await _dbContext.SaveChangesAsync();

                    _logger.LogInformation($"API secret Id {apiSecret.Id} created by {User?.Identity?.Name}.");
                    return(RedirectToAction("Edit", "ApiResources", new { id = vm.ApiResourceId }));
                }
                catch (DbException ex)
                {
                    _logger.LogError(ex.GetBaseException()?.Message ?? ex.Message);
                    throw;
                }
            }
            return(View(vm));
        }