Beispiel #1
0
        static void Main(string[] args)
        {
            var dateTime = DateTime.Now;
            var str      = EasyInviteCode.Generate("milkitic", dateTime);
            var success  = EasyInviteCode.ConvertBack(str);

            var connectRegex = new ConnectRegexObj();

            connectRegex.Match(
                "L 07/19/2020 - 17:48:37: \"CPU 0<1774><STEAM_1:0:138017978><>\" connected, address \"112.64.0.74:6785\"");
        }
Beispiel #2
0
 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
     base.OnModelCreating(modelBuilder);
     modelBuilder.Entity <User>().HasData(new User
     {
         Id          = 1,
         Password    = UserService.GetSaltedPass("r00t@bst123"),
         Username    = "******",
         Role        = UserRoles.Root,
         InviteCodes = string.Join('|', EasyInviteCode.Generate("root", DateTime.Now.AddDays(7)))
     });
 }
        public async Task AddUser(SignUpModel signUpModel)
        {
            var user = await _dbContext.Users.FirstOrDefaultAsync(k => k.Username == signUpModel.Username);

            if (user != null)
            {
                throw new Exception("用户名已存在");
            }
            if (signUpModel.Password != signUpModel.Password2)
            {
                throw new Exception("两次密码不一致");
            }

            #region Validate InviteCode

            var inviteCode = signUpModel.InviteCode;

            string   username;
            DateTime expire;
            try
            {
                (username, expire) = EasyInviteCode.ConvertBack(inviteCode);
            }
            catch (Exception ex)
            {
                throw new Exception("邀请码无效");
            }

            string inviteCodes;
            try
            {
                var inviteUser = await _dbContext.Users.FirstOrDefaultAsync(k => k.Username == username);

                if (inviteUser == null)
                {
                    throw new Exception("邀请码无效");
                }
                inviteCodes = inviteUser.InviteCodes;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            var split = inviteCodes.Split('|');
            if (split.Contains(inviteCode))
            {
                if (DateTime.Now > expire)
                {
                    throw new Exception("邀请码已过期");
                }
            }
            else
            {
                throw new Exception("邀请码无效");
            }

            #endregion

            await _dbContext.Users.AddAsync(new User
            {
                Username = signUpModel.Username,
                Password = GetSaltedPass(signUpModel.Password),
                Role     = UserRoles.User
            });

            await _dbContext.SaveChangesAsync();
        }