我对 .Net/MVC3 比较陌生,并且正在开发使用默认成员资格提供程序的 C#、MVC3、EF4 应用程序。根据我的阅读,它应该会自动捕获重复的电子邮件,但它似乎没有这样做,我不确定为什么。我真正需要弄清楚的是在哪里查看正确的部分是否到位,以及它可能不进行验证检查的原因(它似乎做了大多数/所有其他的,比如重复用户名或无效的密码格式等,只有重复的电子邮件不会被抓住。)
自定义包括将新用户添加到特定角色,以及重定向到“第一次”欢迎页面。
这是代码:
// POST: /Account/Register
[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 */);
Roles.AddUserToRole(model.UserName, "Registered");
//return RedirectToAction("Index", "Home");
return RedirectToAction("Acceptance", "Account");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
以下是(未触及的)验证方法:
#region Status Codes
private static string ErrorCodeToString(MembershipCreateStatus createStatus)
{
// See http://go.microsoft.com/fwlink/?LinkID=177550 for
// a full list of status codes.
switch (createStatus)
{
case MembershipCreateStatus.DuplicateUserName:
return "User name already exists. Please enter a different user name.";
case MembershipCreateStatus.DuplicateEmail:
return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
等等