Пример #1
0
        /// <summary>
        /// 普通读者用户资料注册管理
        /// </summary>
        /// <param name="jsonRegisetrInformation"></param>
        /// <returns></returns>
        public async Task <IActionResult> Register(string jsonRegisterInformation)
        {
            var validateMessage = new ValidateMessage();
            var loUser          = Newtonsoft.Json.JsonConvert.DeserializeObject <LogonInformation>(jsonRegisterInformation);//获取前端数据放到用户视图模型里边

            if (loUser.ConfirmPassword != loUser.Password)
            {
                validateMessage.IsOK = false;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "Succeed",
                    Message        = "密码和确认密码不一致",
                });
                return(Json(validateMessage));
            }
            if (ModelState.IsValid)
            {
                var isNewUser = await _userManager.FindByNameAsync(loUser.UserName);

                if (isNewUser != null)
                {
                    validateMessage.IsOK = false;
                    validateMessage.ValidateMessageItems.Add(
                        new ValidateMessageItem
                    {
                        IsPropertyName = false,
                        MessageName    = "Succeed",
                        Message        = "登录名重复",
                    });
                    return(Json(validateMessage));
                }
                var user = new ApplicationUser()
                {
                    Name               = loUser.UserName,
                    UserName           = loUser.UserName,
                    TwoFactorEnabled   = true,
                    Email              = loUser.Email,
                    NormalizedUserName = loUser.UserName,
                    SecurityStamp      = Guid.NewGuid().ToString(),
                    Avatar             = await _businessImageExtension.GetAll().Where(x => x.Name == "头像" && x.IsSystem).FirstOrDefaultAsync(),
                    RegisterTime       = DateTime.Now,
                    Power              = AnJurisdiction.Ordinary,
                };
                _userExtension.Add(user);
                user.PasswordHash = new PasswordHasher <ApplicationUser>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(user, loUser.Password);
                await _userExtension.SaveAsyn();

                validateMessage.IsOK = true;
                validateMessage.ValidateMessageItems.Add(
                    new ValidateMessageItem
                {
                    IsPropertyName = false,
                    MessageName    = "Succeed",
                    Message        = "注册成功,请登录"
                });

                return(Json(validateMessage));
            }
            else
            {
                validateMessage.IsOK = false;
                var errCollection = from errKey in ModelState.Keys
                                    from errMessage in ModelState[errKey].Errors
                                    where ModelState[errKey].Errors.Count > 0
                                    select(new { errKey, errMessage.ErrorMessage });

                foreach (var errItem in errCollection)
                {
                    var vmItem = new ValidateMessageItem()
                    {
                        IsPropertyName = true,
                        MessageName    = errItem.errKey,
                        Message        = errItem.ErrorMessage
                    };
                    validateMessage.ValidateMessageItems.Add(vmItem);
                }
                return(Json(validateMessage));
            }
        }