Example #1
0
        private async Task SaveIdentityResourcesAsync()
        {
            var identityResourceOpenId = new IdentityResource(Guid.NewGuid(), "openid", "OpenID", required: true);
            await _identityResourceRepository.InsertAsync(identityResourceOpenId);

            var identityResourceEmail = new IdentityResource(Guid.NewGuid(), "email", "Email", required: true);

            identityResourceEmail.AddUserClaim("email");
            identityResourceEmail.AddUserClaim("email_verified");
            await _identityResourceRepository.InsertAsync(identityResourceEmail);

            var identityResourceRole = new IdentityResource(Guid.NewGuid(), "roles", "Roles", required: true);

            identityResourceRole.AddUserClaim("role");
            await _identityResourceRepository.InsertAsync(identityResourceRole);

            var identityResourceProfile = new IdentityResource(Guid.NewGuid(), "profile", "Profile", required: true);

            identityResourceProfile.AddUserClaim("unique_name");
            await _identityResourceRepository.InsertAsync(identityResourceProfile);
        }
        protected virtual async Task UpdateApiResourceByInputAsync(IdentityResource identityResource, IdentityResourceCreateOrUpdateDto input)
        {
            if (!string.Equals(identityResource.Name, input.Name, StringComparison.InvariantCultureIgnoreCase))
            {
                identityResource.Name = input.Name;
            }
            if (!string.Equals(identityResource.Description, input.Description, StringComparison.InvariantCultureIgnoreCase))
            {
                identityResource.Description = input.Description;
            }
            if (!string.Equals(identityResource.DisplayName, input.DisplayName, StringComparison.InvariantCultureIgnoreCase))
            {
                identityResource.DisplayName = input.DisplayName;
            }
            identityResource.Emphasize = input.Emphasize;
            identityResource.Enabled   = input.Enabled;
            identityResource.Required  = input.Required;
            identityResource.ShowInDiscoveryDocument = input.ShowInDiscoveryDocument;

            if (await IsGrantAsync(AbpIdentityServerPermissions.IdentityResources.ManageClaims))
            {
                // 删除不存在的UserClaim
                identityResource.UserClaims.RemoveAll(claim => input.UserClaims.Any(inputClaim => claim.Type == inputClaim.Type));
                foreach (var inputClaim in input.UserClaims)
                {
                    var userClaim = identityResource.FindUserClaim(inputClaim.Type);
                    if (userClaim == null)
                    {
                        identityResource.AddUserClaim(inputClaim.Type);
                    }
                }
            }

            if (await IsGrantAsync(AbpIdentityServerPermissions.IdentityResources.ManageProperties))
            {
                // 删除不存在的Property
                identityResource.Properties.RemoveAll(prop => !input.Properties.Any(inputProp => prop.Key == inputProp.Key));
                foreach (var inputProp in input.Properties)
                {
                    var identityResourceProperty = identityResource.FindProperty(inputProp.Key);
                    if (identityResourceProperty == null)
                    {
                        identityResource.AddProperty(inputProp.Key, inputProp.Value);
                    }
                    else
                    {
                        identityResourceProperty.Value = inputProp.Value;
                    }
                }
            }
        }
        private void AddIdentityResources()
        {
            var identityResource = new IdentityResource(_testData.IdentityResource1Id, "NewIdentityResource1")
            {
                Description = nameof(Client.Description),
                DisplayName = nameof(IdentityResource.DisplayName)
            };

            identityResource.AddUserClaim(nameof(ApiResourceClaim.Type));

            _identityResourceRepository.Insert(identityResource);
            _identityResourceRepository.Insert(new IdentityResource(_guidGenerator.Create(), "NewIdentityResource2"));
            _identityResourceRepository.Insert(new IdentityResource(_guidGenerator.Create(), "NewIdentityResource3"));
        }
Example #4
0
        public async Task CreateIdentityResourcesAsync(string name, IEnumerable <string> claims)
        {
            var identityResource = new IdentityResource(guidGenerator.Create(), name, $"{name} Identity", required: true);

            foreach (var claim in claims)
            {
                if (identityResource.FindUserClaim(claim) == null)
                {
                    identityResource.AddUserClaim(claim);
                }
            }

            await identityResourceRepository.InsertAsync(identityResource);
        }
Example #5
0
        private async Task AddIdentityResources()
        {
            var identityResource = new IdentityResource(_testData.IdentityResource1Id, "NewIdentityResource1")
            {
                Description = nameof(Client.Description),
                DisplayName = nameof(IdentityResource.DisplayName)
            };

            identityResource.AddUserClaim(nameof(ApiResourceClaim.Type));

            await _identityResourceRepository.InsertAsync(identityResource).ConfigureAwait(false);

            await _identityResourceRepository.InsertAsync(new IdentityResource(_guidGenerator.Create(), "NewIdentityResource2")).ConfigureAwait(false);

            await _identityResourceRepository.InsertAsync(new IdentityResource(_guidGenerator.Create(), "NewIdentityResource3")).ConfigureAwait(false);
        }
Example #6
0
        public virtual async Task <IdentityResourceDto> CreateAsync(IdentityResourceCreateUpdateDto input)
        {
            var existed = await _resourceRepository.CheckNameExistAsync(input.Name);

            if (existed)
            {
                throw new UserFriendlyException(L["EntityExisted", nameof(IdentityResource),
                                                  nameof(IdentityResource.Name),
                                                  input.Name]);
            }

            var identityResource = new IdentityResource(GuidGenerator.Create(), input.Name);

            identityResource = ObjectMapper.Map(input, identityResource);
            input.UserClaims.ForEach(x => identityResource.AddUserClaim(x));

            identityResource = await _resourceRepository.InsertAsync(identityResource, true);

            return(ObjectMapper.Map <IdentityResource, IdentityResourceDto>(identityResource));
        }
Example #7
0
        public virtual async Task <IdentityResourceDto> CreateAsync(IdentityResourceCreateDto identityResourceCreate)
        {
            var identityResourceExists = await IdentityResourceRepository.CheckNameExistAsync(identityResourceCreate.Name);

            if (identityResourceExists)
            {
                throw new UserFriendlyException(L[AbpIdentityServerErrorConsts.IdentityResourceNameExisted, identityResourceCreate.Name]);
            }
            var identityResource = new IdentityResource(GuidGenerator.Create(), identityResourceCreate.Name, identityResourceCreate.DisplayName,
                                                        identityResourceCreate.Description, identityResourceCreate.Enabled, identityResourceCreate.Required, identityResourceCreate.Emphasize,
                                                        identityResourceCreate.ShowInDiscoveryDocument);

            foreach (var claim in identityResourceCreate.UserClaims)
            {
                identityResource.AddUserClaim(claim.Type);
            }
            identityResource = await IdentityResourceRepository.InsertAsync(identityResource);

            return(ObjectMapper.Map <IdentityResource, IdentityResourceDto>(identityResource));
        }