コード例 #1
0
        private static IdentityServer4.EntityFramework.Entities.IdentityResource ConvertIdentityResource(IdentityResource model)
        {
            var result = new IdentityServer4.EntityFramework.Entities.IdentityResource
            {
                Name                    = model.Name,
                DisplayName             = model.DisplayName,
                Description             = model.Description,
                Emphasize               = model.Emphasize,
                Enabled                 = model.Enabled,
                Required                = model.Required,
                ShowInDiscoveryDocument = model.ShowInDiscoveryDocument,
                UserClaims              = new List <IdentityServer4.EntityFramework.Entities.IdentityClaim>()
            };

            var userClaims = new List <IdentityServer4.EntityFramework.Entities.IdentityClaim>();

            foreach (var claim in model.UserClaims)
            {
                userClaims.Add(new IdentityServer4.EntityFramework.Entities.IdentityClaim {
                    Type = claim
                });
            }
            result.UserClaims.AddRange(userClaims);
            return(result);
        }
コード例 #2
0
        private async Task <Response <string> > AddAsync(IdentityResourceViewModel viewModel)
        {
            //判断
            var identityResource = await _configurationDbContext.IdentityResources.FirstOrDefaultAsync(d => d.Name == viewModel.Name);

            if (identityResource != null)
            {
                return(new Response <string>(success: false, msg: "认证资源名称已存在!")
                {
                    Data = "认证资源名称已存在!"
                });
            }
            identityResource = new IdentityServer4.EntityFramework.Entities.IdentityResource
            {
                Name        = viewModel.Name,
                DisplayName = viewModel.DisplayName,
                Description = viewModel.Description,
            };
            await _configurationDbContext.IdentityResources.AddAsync(identityResource);

            var result = await _configurationDbContext.SaveChangesAsync();

            if (result < 1)
            {
                return(new Response <string>(success: false, msg: "注册失败,请重试!")
                {
                    Data = "注册失败,请重试!"
                });
            }
            return(new Response <string>(msg: "注册成功!")
            {
                Data = "注册成功!"
            });
        }
        protected string GetProperty(PropertyMetadata propMetadata, IdentityServer4.EntityFramework.Entities.IdentityResource identityResource)
        {
            string val;

            if (propMetadata.TryGet(identityResource, out val))
            {
                return(val);
            }
            throw new Exception("Invalid property type " + propMetadata.Type);
        }
        public Task <IdentityAdminResult <CreateResult> > CreateAsync(IEnumerable <PropertyValue> properties)
        {
            var IdentityResourceNameClaim = properties.Single(x => x.Type == "IdentityResourceName");

            var IdentityResourceName = IdentityResourceNameClaim.Value;


            string[] exclude         = { "IdentityResourceName" };
            var      otherProperties = properties.Where(x => !exclude.Contains(x.Type)).ToArray();

            var metadata    = GetMetadata();
            var createProps = metadata.CreateProperties;
            var inMemoryIdentityResource = new IdentityServer4.EntityFramework.Entities.IdentityResource
            {
                //Id = _identityResources.Count + 1,
                Name     = IdentityResourceName,
                Enabled  = true,
                Required = false,
                ShowInDiscoveryDocument = true
            };

            foreach (var prop in otherProperties)
            {
                var propertyResult = SetProperty(createProps, inMemoryIdentityResource, prop.Type, prop.Value);
                if (!propertyResult.IsSuccess)
                {
                    return(Task.FromResult(new IdentityAdminResult <CreateResult>(propertyResult.Errors.ToArray())));
                }
            }
            using (var db = new ConfigurationDbContext(connection))
            {
                try
                {
                    db.IdentityResources.Add(inMemoryIdentityResource);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return(Task.FromResult(new IdentityAdminResult <CreateResult>(ex.Message)));
                }
            }
            return(Task.FromResult(new IdentityAdminResult <CreateResult>(new CreateResult {
                Subject = inMemoryIdentityResource.Id.ToString()
            })));
        }
コード例 #5
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);
        }
        protected IdentityAdminResult SetProperty(IEnumerable <PropertyMetadata> propsMeta, IdentityServer4.EntityFramework.Entities.IdentityResource identityResource, string type, string value)
        {
            IdentityAdminResult result;

            if (propsMeta.TrySet(identityResource, type, value, out result))
            {
                return(result);
            }

            throw new Exception("Invalid property type " + type);
        }