在做注冊界面的時候,出現了兩個錯誤,讓我糾結得想死,幸好最後都解決了,只能怪自己對MVC的 Membership了解得不深,尤其是有關Web.Config的配置問題。
問題一:Membership.IsValid返回為 false
這個問題一開始讓我很無語,因為在之前也有做過注冊界面,但並不會出現這樣的問題,代碼 如下:
[HttpPost] public ActionResult Register(RegisterModel model) { if(ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */); return RedirectToAction("LogOn", "Account"); } else { ModelState.AddModelError("test", ErrorCodeToString(createStatus)); return RedirectToAction("Index", "Home"); } } // If we got this far, something failed, redisplay form return View(model); }