看起来你没有得到任何答案。
除非我不完全理解您想要做什么,否则无需修改/扩展/自定义默认 SimpleMembership 以提供电子邮件注册机制,或在注册期间分配默认角色,因为所有这些都可以在 AccountController 中完成。
例如,这是我正在使用的注册方法:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid) //TODO Change this to use a worker to send emails.
{
// Check if email exists already before creating new user
using (UsersContext db = new UsersContext())
{
UserProfile email = db.UserProfiles.FirstOrDefault(u => u.Email.ToLower() == model.Email.ToLower());
UserProfile uName =
db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
// Attempt to register the user
try
{
if (email == null && uName == null && this.IsCaptchaVerify("Captcha is not valid"))
{
bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
string confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new
{
FirstName = model.FirstName,
LastName = model.LastName,
Email = model.Email
},
requireEmailConfirmation);
if (requireEmailConfirmation)
{
EmailViewModel eml = new EmailViewModel
{
ToEmail = model.Email,
Subject = "Confirmez votre inscription",
FirstName = model.FirstName,
LastName = model.LastName,
Body = confirmationToken
};
UserMailer.ConfirmRegistration(eml).SendAsync();
Response.Redirect("~/Account/Thanks");
}
else
{
WebSecurity.Login(model.UserName, model.Password);
Response.Redirect("~/");
}
}
else
{
if (email != null)
ModelState.AddModelError("Email", "Email address already exists. Please enter a different email address.");
if (uName != null)
ModelState.AddModelError("UserName", "User Name already exists. Please enter a different user name.");
if (!this.IsCaptchaVerify("Captcha is not valid"))
TempData["ErrorMessage"] = "Captcha is not valid";
}
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
这里没有分配默认角色,但是一旦验证了 EmailConfirmation,就很容易添加。
由于问题很老,我希望它对某人有所帮助!