private async Task <Response <string> > AddAsync(ApiResourceViewModel viewModel)
        {
            //判断
            var identityResource = await _configurationDbContext.ApiResources.FirstOrDefaultAsync(d => d.Name == viewModel.Name);

            if (identityResource != null)
            {
                return(new Response <string>(success: false, msg: "Api资源名称已存在!")
                {
                    Data = "Api资源名称已存在!"
                });
            }
            identityResource = new IdentityServer4.EntityFramework.Entities.ApiResource
            {
                Name        = viewModel.Name,
                DisplayName = viewModel.DisplayName,
                Description = viewModel.Description,
            };
            await _configurationDbContext.ApiResources.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.ApiResource apiResource)
        {
            string val;

            if (propMetadata.TryGet(apiResource, out val))
            {
                return(val);
            }
            throw new Exception("Invalid property type " + propMetadata.Type);
        }
예제 #3
0
        public async static Task Seed(ConfigurationDbContext context)
        {
            if (!context.IdentityResources.Any())
            {
                context.IdentityResources.Add(Mapper.Map <IdentityServer4.EntityFramework.Entities.IdentityResource>(new IdentityResources.Address()));
                context.IdentityResources.Add(Mapper.Map <IdentityServer4.EntityFramework.Entities.IdentityResource>(new IdentityResources.Email()));
                context.IdentityResources.Add(Mapper.Map <IdentityServer4.EntityFramework.Entities.IdentityResource>(new IdentityResources.OpenId()));
                context.IdentityResources.Add(Mapper.Map <IdentityServer4.EntityFramework.Entities.IdentityResource>(new IdentityResources.Phone()));
                context.IdentityResources.Add(Mapper.Map <IdentityServer4.EntityFramework.Entities.IdentityResource>(new IdentityResources.Profile()));
                await context.SaveChangesAsync();
            }

            if (!context.ApiResources.Any(a => a.Name == "moatgate_api"))
            {
                var moatgateApi = new IdentityServer4.EntityFramework.Entities.ApiResource
                {
                    Name        = "moatgate_api",
                    DisplayName = "MoatGate's Public API",
                    Description = "MoatGate's public API for client applications",
                    NonEditable = true,
                    Created     = DateTime.UtcNow,
                    Secrets     = new System.Collections.Generic.List <IdentityServer4.EntityFramework.Entities.ApiSecret>
                    {
                        new IdentityServer4.EntityFramework.Entities.ApiSecret
                        {
                            Created = DateTime.UtcNow,
                            Type    = "SharedSecret",
                            Value   = "moatgate_api_secret".Sha256()
                        }
                    },
                    Scopes = new System.Collections.Generic.List <IdentityServer4.EntityFramework.Entities.ApiScope>
                    {
                        new IdentityServer4.EntityFramework.Entities.ApiScope
                        {
                            Name                    = "moatgate_api_users",
                            DisplayName             = "MoatGate's Users API",
                            Description             = "MoatGate's public API for manipulation of users",
                            Required                = false,
                            Emphasize               = true,
                            ShowInDiscoveryDocument = true,
                            UserClaims              = new IdentityResources.Email().UserClaims.Union(new IdentityResources.OpenId().UserClaims)
                                                      .Select(c => new IdentityServer4.EntityFramework.Entities.ApiScopeClaim
                            {
                                Type = c
                            }).ToList()
                        }
                    }
                };
                context.ApiResources.Add(moatgateApi);
                await context.SaveChangesAsync();
            }
        }
        public Task <IdentityAdminResult <CreateResult> > CreateAsync(IEnumerable <PropertyValue> properties)
        {
            var ApiResourceNameClaim = properties.Single(x => x.Type == "ApiResourceName");

            var ApiResourceName = ApiResourceNameClaim.Value;

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

            var metadata            = GetMetadata();
            var createProps         = metadata.CreateProperties;
            var inMemoryApiResource = new IdentityServer4.EntityFramework.Entities.ApiResource
            {
                //Id = _apiResources.Count + 1,
                Name    = ApiResourceName,
                Enabled = true
            };

            foreach (var prop in otherProperties)
            {
                var propertyResult = SetProperty(createProps, inMemoryApiResource, prop.Type, prop.Value);
                if (!propertyResult.IsSuccess)
                {
                    return(Task.FromResult(new IdentityAdminResult <CreateResult>(propertyResult.Errors.ToArray())));
                }
            }
            using (var db = new ConfigurationDbContext(connection))
            {
                try
                {
                    db.ApiResources.Add(inMemoryApiResource);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return(Task.FromResult(new IdentityAdminResult <CreateResult>(ex.Message)));
                }
            }
            return(Task.FromResult(new IdentityAdminResult <CreateResult>(new CreateResult {
                Subject = inMemoryApiResource.Id.ToString()
            })));
        }
예제 #5
0
        public void missing_values_should_use_defaults()
        {
            var entity = new IdentityServer4.EntityFramework.Entities.ApiResource
            {
                Secrets = new System.Collections.Generic.List <Entities.ApiSecret>
                {
                    new Entities.ApiSecret
                    {
                    }
                }
            };

            var def = new ApiResource
            {
                ApiSecrets = { new Models.Secret("foo") }
            };

            var model = entity.ToModel();

            model.ApiSecrets.First().Type.Should().Be(def.ApiSecrets.First().Type);
        }
        protected IdentityAdminResult SetProperty(IEnumerable <PropertyMetadata> propsMeta, IdentityServer4.EntityFramework.Entities.ApiResource apiResource, string type, string value)
        {
            IdentityAdminResult result;

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

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