Exemple #1
0
        /// <summary>
        /// 检查是否更新NpsChannels
        /// </summary>
        /// <param name="npsAppSecret">NpsAppSecret</param>
        /// <returns>返回NpsAppSecret</returns>
        private async Task <NpsAppSecret> UpdateNpsChannelsIfCheckNotNullAsync(NpsAppSecret npsAppSecret)
        {
            Check.NotNull(npsAppSecret, nameof(npsAppSecret));
            Check.NotNull(npsAppSecret.NpsClient, nameof(npsAppSecret.NpsClient));
            Check.NotNull(npsAppSecret.NpsClient.NpsChannels, nameof(npsAppSecret.NpsClient.NpsChannels));

            _logger.LogInformation($"检查是否更新NpsChannels,设备唯一识别编码:{npsAppSecret.DeviceUniqueId}");
            var channelListOutput = await GetRemoteChannelOutputAsync(npsAppSecret.NpsClient.RemoteClientId);

            if (channelListOutput == null || channelListOutput.Datas == null || channelListOutput.Datas.Count == 0)
            {
                return(npsAppSecret);
            }

            //先查询,再更新,否则报异常
            _npsChannelRepository.Attach(npsAppSecret.NpsClient.NpsChannels);
            for (int index = 0; index < npsAppSecret.NpsClient.NpsChannels.Count; index++)
            {
                var npsChannel    = npsAppSecret.NpsClient.NpsChannels[index];
                var remoteChannel = channelListOutput.Datas.FirstOrDefault(x => x.Target.TargetAddress == npsChannel.DeviceAddress);
                if (remoteChannel != null)
                {//将远程隧道信息回写至本地Nps隧道表
                    npsChannel.RemoteChannelId = remoteChannel.Id;
                    npsChannel.Status          = remoteChannel.Status;
                    npsChannel.RunStatus       = remoteChannel.RunStatus;
                }
            }
            await _npsChannelRepository.UpdateAsync(npsAppSecret.NpsClient.NpsChannels);

            return(npsAppSecret);
        }
        private async Task <Tokens> CreateTokenAsync(User user)
        {
            List <Claim> claims = new List <Claim>()
            {
                new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.UserName ?? ""),
                new Claim(ClaimTypes.Email, user.Email ?? ""),
                new Claim(ClaimTypes.GivenName, user.NikeName ?? ""),
                new Claim(ClaimTypes.MobilePhone, user.Mobile ?? "")
            };
            //添加角色信息
            //TODO

            string token = _jsonWebTokenService.Encode(claims);

            string refreshToken = GenerateToken();

            user.LastLoginTime = DateTime.Now;
            user.RefreshToken  = refreshToken;
            await _userRepository.UpdateAsync(user);

            var jwtToken = _jsonWebTokenService.Decode(token);

            return(new Tokens(token, refreshToken, jwtToken["exp"]?.ToString()));
        }
Exemple #3
0
        /// <summary>
        /// 检查是否更新NpsAppSecret、NpsServer、NpsClient
        /// </summary>
        /// <param name="npsAppSecret">NpsAppSecret</param>
        /// <returns>返回NpsAppSecret</returns>
        private async Task <NpsAppSecret> UpdateNpsClientOrNpsServerIfCheckNotNullAsync(NpsAppSecret npsAppSecret)
        {
            Check.NotNull(npsAppSecret, nameof(npsAppSecret));
            Check.NotNull(npsAppSecret.NpsClient, nameof(npsAppSecret.NpsClient));

            _logger.LogInformation($"检查是否更新NpsAppSecret、NpsServer、NpsClient,设备唯一识别编码:{npsAppSecret.DeviceUniqueId}");
            if (npsAppSecret.NpsClient.RemoteClientId == 0 || npsAppSecret.NpsServer == null || npsAppSecret.NpsServerId == 0)
            {
                var clientListOutput = await GetRemoteClientOutputAsync(npsAppSecret.AppSecret);

                if (clientListOutput == null || clientListOutput.Datas == null || clientListOutput.Datas.Count == 0)
                {
                    return(npsAppSecret);
                }

                //若本地数据库Nps客户端表中无远程Nps客户端Id,则将远程客户端信息回写
                if (npsAppSecret.NpsClient.RemoteClientId == 0)
                {
                    var remoteNpsClient = clientListOutput.Datas[0];

                    _npsClientRepository.Attach(npsAppSecret.NpsClient);
                    npsAppSecret.NpsClient.RemoteClientId     = remoteNpsClient.Id;
                    npsAppSecret.NpsClient.Status             = remoteNpsClient.Status;
                    npsAppSecret.NpsClient.IsConnect          = remoteNpsClient.IsConnect;
                    npsAppSecret.NpsClient.LastConnectAddress = remoteNpsClient.LastConnectAddress;
                    await _npsClientRepository.UpdateAsync(npsAppSecret.NpsClient);
                }

                //若该设备无对应的服务器信息,则将服务器写入本地数据库
                if (npsAppSecret.NpsServer == null)
                {
                    //先根据IP地址查询服务器是否存在
                    npsAppSecret.NpsServer = await _npsServerRepository.Where(x => x.ServerIPAddress == clientListOutput.ServerIPAddress).ToOneAsync();

                    if (npsAppSecret.NpsServer == null)
                    {
                        npsAppSecret.NpsServer = await _npsServerRepository.InsertAsync(new NpsServer
                        {
                            ServerIPAddress   = clientListOutput.ServerIPAddress,
                            ClientConnectPort = clientListOutput.ClientConnectPort.ToInt32OrDefault(0),
                            ProtocolType      = _protocolType
                        });
                    }
                }

                //若该设备无对应的服务器信息
                if (npsAppSecret.NpsServerId == 0)
                {
                    if (npsAppSecret?.NpsServer?.Id > 0)
                    {
                        //将服务器信息与设备应用密钥关联
                        _npsAppSecretRepository.Attach(npsAppSecret);
                        npsAppSecret.NpsServerId = npsAppSecret.NpsServer.Id;
                        await _npsAppSecretRepository.UpdateAsync(npsAppSecret);
                    }
                }
            }

            return(npsAppSecret);
        }