Beispiel #1
0
        /// <summary>
        /// 临时密钥登录
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <bool> CheckTempPasswordLoginAsync(UserItemDto user, string password)
        {
            var code = await _simpleKeyValueService.CheckAndGet(StsConstants.TempPasswordContainerName,
                                                                user.Id.ToString(), TimeSpan.FromSeconds(30)); //临时密码30秒钟有效

            if (code != password)
            {
                return(false);
            }

            await _simpleKeyValueService.Remove(StsConstants.TempPasswordContainerName, user.Id.ToString());

            return(true);
        }
        public async Task <bool> CheckAsync(string phoneNumber, string code)
        {
            var original = await _simpleKeyValueService.CheckAndGet(
                SimpleKeyValueServiceContainerName, phoneNumber, TimeSpan.FromMinutes(5));

            return(original == code);
        }
        public async Task <RemotingResult> ScanJoinAsync(string code, Guid employeeId, string employeeName)
        {
            if (code == null)
            {
                throw new ArgumentNullException(nameof(code));
            }
            if (employeeId == Guid.Empty)
            {
                throw new ArgumentException("must not be empty", nameof(employeeId));
            }

            var splited = code.Split(',');

            if (splited.Length != 2)
            {
                return(RemotingResult.Fail(1));
            }
            if (!Guid.TryParse(splited[0], out Guid id))
            {
                return(RemotingResult.Fail(1));
            }
            if (!Guid.TryParse(splited[1], out Guid _))
            {
                return(RemotingResult.Fail(1));
            }

            var originalFingerprint = await _simpleKeyValueService.CheckAndGet(SKVContainer_ScanJoinCode,
                                                                               id.ToString("N"), TimeSpan.FromDays(1));

            if (splited[1] != originalFingerprint)
            {
                return(RemotingResult.Fail(1));
            }

            using (var db = new ServiceDbContext(_dbOptions))
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    try
                    {
                        var entity = await db.Groups.Include(o => o.Members)
                                     .Where(o => o.Type == GroupType.CustomChat)
                                     .FirstOrDefaultAsync(o => o.Id == id);

                        if (entity == null)
                        {
                            return(RemotingResult.Fail(2));
                        }

                        if (entity.Members.Exists(o => o.EmployeeId == employeeId))
                        {
                            return(RemotingResult.Fail(3));
                        }

                        entity.Members.Add(new GroupMember
                        {
                            EmployeeId = employeeId,
                            GroupId    = id,
                            Joined     = DateTimeOffset.UtcNow,
                        });

                        await db.SaveChangesAsync();

                        await _busMs.Publish(new GroupMembersUpdated { Id = entity.Id });

                        await _busMs.Publish(new GroupNotified
                        {
                            Id       = entity.Id,
                            Notifies = new List <EventNotifyDto>
                            {
                                new EventNotifyDto
                                {
                                    Target         = NotifyTargetType.Conversation,
                                    TargetId       = entity.Id.ToString(),
                                    Created        = DateTimeOffset.UtcNow,
                                    Text           = employeeName,
                                    TargetCategory = (int)GroupEventNotifyType.ParticipantAdded
                                }
                            }
                        });

                        tx.Commit();
                        return(RemotingResult.Success());
                    }
                    catch (Exception ex)
                    {
                        tx.Rollback();
                        _logger.Error(ex, "UpdateAsync Transaction rollback");
                        return(RemotingResult.Fail());
                    }
                }
            }
        }