1

编辑:原来这是由于语言中的字符和 i/u 等字符。

[HttpPost]  
        /*[Authorize(Roles = "admin")]*/
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var users = _userdb.UserProfiles.Where(x => x.UserName == model.UserName); 
                if (users.Any())
                {
                    ModelState.AddModelError("Error", "Username Already Exists.");
                    return View(model);
                }

                try
                {
                    if (model.IsAdmin == false)
                    {
                        WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new
                        {
                            CellPhone = model.CellPhone,
                            Email = model.Email,
                            SocialNo = model.SocialNo,
                            Department_Id = model.DepartmentId
                        });


                    }
                    else
                    {
                        WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new
                        {
                            CellPhone = model.CellPhone,
                            Email = model.Email,
                            SocialNo  = model.SocialNo ,
                            Department_Id = 9999
                        });

                        Roles.AddUserToRole(model.UserName, "admin"); 
                    }

                    return RedirectToAction("Departments", "Index");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

我收到此错误,这真的没有帮助。我只想注册一个用户。

为什么我会收到此错误?任何想法?

请注意,UserProfile正在获取数据而不是成员资格表。

4

0 回答 0