Пример #1
0
        public async Task<IActionResult> Register(RegisterModel model)
        {
            var vm = new RegisterViewModel
            {
                Model = model
            };

            if (!ModelState.IsValid)
            {
                vm.ErrorMessage = Messages.ModelStateNotValid;
                return this.View(vm);
            }

            var result = await this.AuthService.Register(model);

            if (result.Success)
            {
                Core.Security.SecurityManager.WriteToken(this.HttpContext, result.Data, false);

                return this.RedirectToAction("Index", "Home");
            }
            else
            {
                vm.ErrorMessage = result.ErrorMessage;
                return this.View(vm);
            }
        }
Пример #2
0
        public async Task<Result<string>> Register(RegisterModel model)
        {
            if (!this.SiteConfiguration.Value.AllowRegister)
            {
                return Result<string>.ErrorResult("站点目前禁止注册");
            }

            using (var uw = this.CreateUnitOfWork())
            {
                if (await uw.ExistAsync<User>(t => t.UserName == model.UserName))
                {
                    return Result<string>.ErrorResult("用户名已被注册");
                }
                if (await uw.ExistAsync<User>(t => t.Email == model.Email))
                {
                    return Result<string>.ErrorResult("邮箱已被注册");
                }

                var entity = new User
                {
                    CreateDate = DateTime.Now,
                    Email = model.Email,
                    UserName = model.UserName,
                    Password = EncryptHelper.EncryptMD5(model.Password)
                };

                if (this.SiteConfiguration.Value.AdminUserList?.Contains(model.UserName, StringComparer.CurrentCultureIgnoreCase) == true)
                {
                    entity.Status = Domain.Enums.UserStatus.Active;
                }
                else if (this.SiteConfiguration.Value.VerifyRegisterUser)
                {
                    entity.Status = Domain.Enums.UserStatus.Verifying;
                }
                else
                {
                    entity.Status = Domain.Enums.UserStatus.Active;
                }

                await uw.InsertAsync(entity);

                var redis = this.RedisProvider.GetDatabase();
                redis.JsonHashSet(RedisKeys.User, entity.ID, entity);

                string token = this.GenerateAndStoreToken(entity.ID, false);

                return Result.SuccessResult(token);
            }            
        }