Esempio n. 1
0
        /// <summary>
        /// 获取客户与资源
        /// </summary>
        /// <returns></returns>
        private List <Client> GetAllClients()
        {
            //获取客户与资源关系
            var apiClientToResourceList = _sysApiResourceToClientRepository.GetApiClientAndResource().ToList();
            //
            List <Client> clients = new List <Client>();

            //
            if (apiClientToResourceList == null && !apiClientToResourceList.Any())
            {
                return(clients);
            }
            //获取有效的客户以及相关资源关系对象
            apiClientToResourceList = apiClientToResourceList.Where(w => w.IsActiveClient.Value && w.IsActiveResource.Value).ToList();
            //构造客户端对象
            foreach (var clientItem in apiClientToResourceList)
            {
                Client client = new Client();
                //客户ID
                client.ClientId   = clientItem.ClientId;
                client.ClientName = "客户端名称(" + clientItem.ClientId + ")";
                //设置秘钥类型 可以在 ISecretValidator 实现具体解密方式
                client.ClientSecrets = new[] {
                    new Secret()
                    {
                        //Value = client.ClientSecrets.Sha256(),
                        Value = clientItem.ClientSecrets,
                        Type  = IdentityServerConstants.SecretTypes.SharedSecret
                    }
                };

                //身份认证服务器常量
                client.AllowedScopes = new List <string>
                {
                    IdentityServerConstants.StandardScopes.OpenId,
                    IdentityServerConstants.StandardScopes.Profile,
                    //IdentityServerConstants.StandardScopes.Email,
                    //IdentityServerConstants.StandardScopes.Address,
                    //IdentityServerConstants.StandardScopes.Phone,
                    //IdentityServerConstants.StandardScopes.OfflineAccess,
                    clientItem.ResourceName
                };
                //授权token有效期 (单位/小时 转换 单位/秒)
                client.AccessTokenLifetime = clientItem.AccessTokenLifetime.Value * 60 * 60;
                //产生刷新令牌
                client.AllowOfflineAccess = clientItem.AllowOfflineAccess.Value;
                //设置刷新令牌有效时间(单位/小时 转换 单位/秒)
                client.SlidingRefreshTokenLifetime = clientItem.SlidingRefreshTokenLifetime.Value * 60 * 60;
                //设置刷新令牌将在固定的时间点过期
                client.RefreshTokenExpiration = TokenExpiration.Sliding;

                //支持客户端验证同时支持客户端与密码验证
                //ToDo 此处针对自定义 GrantType 可以进行扩展并添加到授权类型中
                //ToDo 默认添加自定义 GrantType 方式 示例:new[] { "demo_validation" }
                //ToDo 默认针对每个资源相关的客户添加单点登录授权 GrantType.Implicit,
                client.AllowedGrantTypes = new[] { GrantType.ClientCredentials, GrantType.ResourceOwnerPassword, GrantType.Implicit, "demo_validation" };

                //ToDo 设置单点登录相关的客户端回发以及注销地址
                client.RedirectUris           = new[] { "http://localhost:4838/signin-oidc", "http://localhost:44077/signin-oidc" };
                client.PostLogoutRedirectUris = new[] { "http://localhost:4838/signout-callback-oidc", "http://localhost:4838/signout-callback-oidc" };
                client.RequireConsent         = false;

                clients.Add(client);
            }
            return(clients);
        }