コード例 #1
0
        /// <summary>
        /// 用于存储或更新活动
        /// </summary>
        /// <param name="anVM"></param>
        /// <returns></returns>
        public async Task <IActionResult> Sava([Bind("ID,UserId,Name,SchoolAddress,CreateDataTime,MaxNumber,Description")] AnAssociationVM anVM)
        {
            var an = await _anAssociationRepository.GetSingleAsyn(anVM.ID);

            if (an == null)
            {
                an = new AnAssociation();
                var userclaims = User.Claims.FirstOrDefault();
                var userData   = await _userManager.FindByIdAsync(userclaims.Value.ToString());

                an.User = userData;
                var anFounder = new AnAssociationAndUser()
                {
                    User                  = userData,
                    AnAssociationId       = anVM.ID,
                    AnJurisdictionManager = AnJurisdiction.Founder
                };
                an.IsDisable = true;
                _anAssociationAndUserExtension.Add(anFounder);
            }
            anVM.MapToAn(an);
            var savaStatus = await _anAssociationRepository.AddOrEditAndSaveAsyn(an);

            if (an == null)
            {
                _anAssociationAndUserExtension.Save();
            }
            if (savaStatus)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                ModelState.AddModelError("", "数据保存出现异常,无法创建活动。");
                return(View("~/Views/GroupOrganization/AnAssociation/CreateOrEdit.cshtml", anVM));
            }
        }
コード例 #2
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));
            }
        }