public async Task <IActionResult> DeleteClaimFromApiScope(int id)
        {
            var apiScope = await _configurationDbContext
                           .ApiScopes
                           .Where(q => q.Id == id)
                           .Include(q => q.UserClaims)
                           .FirstOrDefaultAsync();

            if (apiScope == null)
            {
                ModelState.AddModelError(string.Empty, "您的Claim数据无法找到");

                return(View());
            }

            var vm = new ClaimToApiScopeViewModel
            {
                ApiScopeId     = apiScope.Id,
                ApiScopeClaims = new List <ApiScopeClaim>()
            };

            if (!apiScope.UserClaims.IsNullOrEmpty())
            {
                foreach (var claim in apiScope.UserClaims)
                {
                    vm.ApiScopeClaims.Add(claim);
                }
            }

            return(View(vm));
        }
        public async Task <IActionResult> DeleteClaimFromApiScope(ClaimToApiScopeViewModel model)
        {
            var apiScope = await _configurationDbContext
                           .ApiScopes
                           .Where(q => q.Id == model.ApiScopeId)
                           .Include(q => q.UserClaims)
                           .FirstOrDefaultAsync();

            if (apiScope == null)
            {
                ModelState.AddModelError(string.Empty, "您的ApiScopeId是错误的, 无法找到");

                return(View(model));
            }

            var userClaim = apiScope
                            .UserClaims
                            .FirstOrDefault(q => q.Type == model.ClaimType);

            if (userClaim == null)
            {
                ModelState.AddModelError(string.Empty, "您的UserClaim没有任何信息, 找不到.");

                return(View(model));
            }

            apiScope.UserClaims.Remove(userClaim);

            await _configurationDbContext.SaveChangesAsync();

            return(RedirectToAction("EditApiScope", new { id = apiScope.Id }));
        }
        public async Task <IActionResult> AddClaimToApiScope(ClaimToApiScopeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var apiScope = await _configurationDbContext
                           .ApiScopes
                           .FirstOrDefaultAsync(q => q.Id == model.ApiScopeId);

            if (!apiScope.UserClaims.IsNullOrEmpty())
            {
                var i = 0;

                foreach (var claim in apiScope.UserClaims)
                {
                    if (claim.Type == model.ClaimType)
                    {
                        i += 1;
                    }
                }

                if (i > 0)
                {
                    ModelState.AddModelError(string.Empty, "您的ClaimType重复了, 请不要重复添加, 谢谢.");

                    return(View(model));
                }
            }

            apiScope.UserClaims = new List <ApiScopeClaim>
            {
                new ApiScopeClaim
                {
                    Type    = model.ClaimType,
                    Scope   = apiScope,
                    ScopeId = apiScope.Id
                }
            };

            await _configurationDbContext.SaveChangesAsync();

            return(RedirectToAction("EditApiScope", new { id = apiScope.Id }));
        }