Ejemplo n.º 1
0
        public async Task <IActionResult> OnGetAsync()
        {
            if (Id.HasValue)
            {
                var identityResource = await configurationDbContext.IdentityResources.FirstOrDefaultAsync(e => e.Id == Id).ConfigureAwait(false);

                if (identityResource == null)
                {
                    return(NotFound());
                }

                ViewModel = new CreateOrUpdateIdentityResourceViewModel
                {
                    Id          = identityResource.Id,
                    Description = identityResource.Description,
                    DisplayName = identityResource.DisplayName,
                    Name        = identityResource.Name
                };
            }
            else
            {
                ViewModel = new CreateOrUpdateIdentityResourceViewModel();
            }
            return(Page());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// POST 创建或者更新
        /// </summary>
        /// <param name="ViewModel"></param>
        /// <returns></returns>
        public async Task <IActionResult> OnPostAsync(CreateOrUpdateIdentityResourceViewModel ViewModel)
        {
            if (ViewModel is null)
            {
                throw new ArgumentNullException(nameof(ViewModel));
            }

            if (!TryValidateModel(ViewModel))
            {
                return(BadRequest(ModelState));
            }

            if (ViewModel.Id.HasValue)
            {
                await UpdateIdentityResourceAsync(ViewModel).ConfigureAwait(false);
            }
            else
            {
                await CreateIdentityResourceAsync(ViewModel).ConfigureAwait(false);
            }

            try
            {
                await configurationDbContext.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (DbUpdateException ex)
            {
                ModelState.AddModelError(string.Empty, ex.InnerException?.Message ?? ex.Message);
                return(BadRequest(ModelState));
            }

            return(new OkResult());
        }
Ejemplo n.º 3
0
        private async Task CreateIdentityResourceAsync(CreateOrUpdateIdentityResourceViewModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var resource = new IdentityServer4.EntityFramework.Entities.IdentityResource
            {
                Name                    = model.Name,
                Description             = model.Description,
                DisplayName             = model.DisplayName,
                ShowInDiscoveryDocument = true,
                Enabled                 = true,
            };

            await configurationDbContext.IdentityResources.AddAsync(resource).ConfigureAwait(false);
        }
Ejemplo n.º 4
0
        private async Task UpdateIdentityResourceAsync(CreateOrUpdateIdentityResourceViewModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (!model.Id.HasValue)
            {
                throw new ArgumentNullException(nameof(model.Id));
            }

            var resource = await configurationDbContext.IdentityResources.FirstOrDefaultAsync(e => e.Id == model.Id.Value).ConfigureAwait(false);

            if (resource == null)
            {
                throw new Exception("身份资源不存在");
            }

            resource.Name        = model.Name;
            resource.Description = model.Description;
            resource.DisplayName = model.DisplayName;
        }