Esempio n. 1
0
        /// <summary>
        /// 新增授权信息
        /// </summary>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        public virtual async Task StoreAsync(PersistedGrant token)
        {
            var existing = await Context.Query <Entities.PersistedGrant>().FirstOrDefaultAsync(x => x.Key == token.Key);

            if (existing == null)
            {
                Logger.LogDebug("{persistedGrantKey} not found in database", token.Key);

                var persistedGrant = token.ToEntity();
                await Context.Command <Entities.PersistedGrant>().AddAsync(persistedGrant);
            }
            else
            {
                Logger.LogDebug("{persistedGrantKey} found in database", token.Key);
                //更新实体传输
                token.UpdateEntity(existing);
                //执行命令
                await Context.Command <Entities.PersistedGrant>().UpdateAsync(x => x.Key == token.Key, new Dictionary <string, object>()
                {
                    { "Type", token.Type },
                    { "SubjectId", token.SubjectId },
                    { "ClientId", token.ClientId },
                    { "CreationTime", token.CreationTime },
                    { "Expiration", token.Expiration },
                    { "Data", token.Data }
                });
            }
        }
Esempio n. 2
0
        public async Task <bool> AddUpdClient(ClientModel model)
        {
            //参数校检
            model.IsNotNull();
            //实体转换
            var entity = model.ToEntity();
            //定义返回值
            var returnValue = true;

            //更新密钥
            if (entity.ClientSecrets?.FirstOrDefault() == null)
            {
                entity.ClientSecrets = null;
            }
            entity.ClientSecrets?.ForEach(a =>
            {
                //将密码的明文存储到描述字段中
                a.Description = a.Value;
                a.Value       = a.Value.Sha256Encrypt();
            });
            //验证新增修改
            if (model.Id.IsNullOrEmpty())
            {
                await mongoRepository.Command <Client>().AddAsync(entity);
            }
            else
            {
                returnValue = await mongoRepository.Command <Client>().UpdateAsync(a => a.Id == entity.Id, new Dictionary <string, object>
                {
                    { "Updated", DateTime.UtcNow },
                    { "ClientId", entity.ClientId },
                    { "ClientName", entity.ClientName },
                    { "Description", entity.Description },
                    { "ClientSecrets", entity.ClientSecrets },
                    { "IdentityTokenLifetime", entity.IdentityTokenLifetime },
                    { "AccessTokenLifetime", entity.AccessTokenLifetime },
                    { "AllowedGrantTypes", entity.AllowedGrantTypes },
                    { "AllowedScopes", entity.AllowedScopes },
                    { "RedirectUris", entity.RedirectUris },
                    { "PostLogoutRedirectUris", entity.PostLogoutRedirectUris },
                    { "AllowedCorsOrigins", entity.AllowedCorsOrigins },
                    { "AllowAccessTokensViaBrowser", entity.AllowAccessTokensViaBrowser }
                });
            }
            return(returnValue);
        }
Esempio n. 3
0
        /// <summary>
        /// 新增编辑资源s
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <bool> AddUpdResources(ResourcesModel model)
        {
            //参数校检
            model.IsNotNull();
            //实体转换
            var entity = model.ToEntity();

            //将密钥明文存储 到描述中
            entity.Secrets?.ForEach(a =>
            {
                //将密码的明文存储到描述字段中
                a.Description = a.Value;
                a.Value       = a.Value.Sha256Encrypt();
            });
            //定义一个返回值
            var returnValue = true;

            //验证新增修改
            if (model.Id.IsNullOrEmpty())
            {
                //默认启用
                entity.Enabled = true;
                await mongoRepository.Command <ApiResource>().AddAsync(entity);
            }
            else
            {
                returnValue = await mongoRepository.Command <ApiResource>().UpdateAsync(a => a.Id == entity.Id, new Dictionary <string, object>
                {
                    { "Updated", DateTime.UtcNow },
                    { "Name", entity.Name },
                    { "Description", entity.Description },
                    { "DisplayName", entity.DisplayName },
                    { "Enabled", entity.Enabled },
                    { "Secrets", entity.Secrets },
                    { "Scopes", entity.Scopes },
                });
            }

            return(returnValue);
        }
Esempio n. 4
0
        public async Task <string> Test()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            await mongoRepository.Command <Client>().AddAsync(new Client
            {
                //d = SnowFlakeHelper.NewID()
            });

            await mongoRepository.ChangeDataBase("tests");

            var res = await mongoRepository.Query <Client>().ToListAsync(a => true);

            stopwatch.Stop();

            return(stopwatch.ElapsedMilliseconds.ToString());
        }
Esempio n. 5
0
        public async Task TestData()
        {
            //api资源
            Id4.Entities.ApiResource apiResource = new Id4.Entities.ApiResource
            {
                Enabled     = true,
                Name        = "api",
                DisplayName = "api",
                Description = "api",
                Scopes      = new List <ApiScope>()
                {
                    new Id4.Entities.ApiScope()
                    {
                        Name                    = "api",
                        DisplayName             = "api",
                        Description             = "api",
                        Required                = true,
                        Emphasize               = false,
                        ShowInDiscoveryDocument = true
                    }
                },
                Secrets    = null,
                Properties = null,
                UserClaims = null
            };


            await mongoRepository.Command <Id4.Entities.ApiResource>().AddAsync(apiResource);

            //客户端信息
            var client = new Id4.Entities.Client
            {
                Enabled             = true,
                ClientId            = "test",
                ProtocolType        = IdentityServerConstants.ProtocolTypes.OpenIdConnect,
                RequireClientSecret = true,
                ClientName          = "testname",
                RequireConsent      = true,
                EnableLocalLogin    = true,
                AccessTokenLifetime = 7200,

                //对象
                ClientSecrets = new List <ClientSecret>()
                {
                    new ClientSecret
                    {
                        Expiration = DateTime.Now.AddYears(1),
                        Type       = IdentityServerConstants.SecretTypes.SharedSecret,
                        Value      = "123456".Sha256(),
                    }
                },
                AllowedGrantTypes = new List <ClientGrantType>()
                {
                    new ClientGrantType {
                        GrantType = GrantType.ClientCredentials,
                    }
                },
                AllowedScopes = new List <ClientScope>()
                {
                    new ClientScope {
                        Scope = "api"
                    }
                },
                RedirectUris           = null,
                PostLogoutRedirectUris = null,
                Claims                       = null,
                AllowedCorsOrigins           = null,
                Properties                   = null,
                IdentityProviderRestrictions = null,
            };
            await mongoRepository.Command <Id4.Entities.Client>().AddAsync(client);

            //新增设备
            await mongoRepository.Command <DeviceFlowCodes>().AddAsync(new DeviceFlowCodes
            {
            });

            //授权信息i
            await mongoRepository.Command <Id4.Entities.PersistedGrant>().AddAsync(new Id4.Entities.PersistedGrant
            {
            });

            //认证资源
            await mongoRepository.Command <Id4.Entities.IdentityResource>().AddAsync(new Id4.Entities.IdentityResource
            {
            });
        }
Esempio n. 6
0
 /// <summary>
 /// Stores the device authorization request.
 /// </summary>
 /// <param name="deviceCode">The device code.</param>
 /// <param name="userCode">The user code.</param>
 /// <param name="data">The data.</param>
 /// <returns></returns>
 public virtual async Task StoreDeviceAuthorizationAsync(string deviceCode, string userCode, DeviceCode data)
 {
     await Context.Command <Entities.DeviceFlowCodes>().AddAsync(ToEntity(data, deviceCode, userCode));
 }
Esempio n. 7
0
 public async Task BsonDocument()
 {
     await mongoRepository.Command <BsonDocument>().AddAsync(new BsonDocument
     {
         { "name", "hai" },
         { "age", "nian" }
     });
 }