如何扩展/自定义MVC4 Internet Application WebSecurity / SimpleMembership

时间:2013-01-23 16:58:17

标签: asp.net-mvc-4 security simplemembership

我一直在努力搜索有关如何修改/扩展/自定义Visual Studio 2012 Express中MVC4 Internet应用程序(EF 5 Code First)中可用的默认成员资格系统的更多信息。

我想知道如何实施电子邮件验证,以便在用户注册时通过激活链接发送电子邮件。当他们点击链接时,他们的帐户被激活,他们可以使用他们的用户名或电子邮件登录。

我还想知道如何通过在注册期间分配默认角色来为注册用户实现简单角色。

类似的问题: How do I manage profiles using SimpleMembership?

How do you extend the SimpleMembership authentication in ASP.NET MVC4

但我真的很想使用现有的简单会员系统。

这篇文章非常接近: http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

我也看过这篇文章: http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx

这是我到目前为止最接近的: http://weblogs.asp.net/thangchung/archive/2012/11/15/customize-the-simplemembership-in-asp-net-mvc-4-0.aspx

这对于WebPages也很有用: http://blog.osbornm.com/archive/2010/07/21/using-simplemembership-with-asp.net-webpages.aspx

我希望找到一个更全面的演练来扩展它。

1 个答案:

答案 0 :(得分:1)

看起来你没有得到任何答案。

除非我不完全理解您的想法,否则无需修改/扩展/自定义默认的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,就很容易添加。

由于问题已经很久了,我希望它对某人有帮助!