How to Extend / Customize MVC4 WebSecurity / SimpleMembership Internet Application

I tried to find more information on how to change / extend / customize the default membership system available in the MVC4 internet application (EF 5 Code first) in Visual Studio 2012 Express.

I would like to know how to implement email validation so that when a user registers an email, it is sent using an activation link. When they click on the link, their account is activated and they can log in using their username or email address.

I would also like to know how to implement simple roles for registered users by assigning a default role during registration.

Related questions: How do I manage profiles with SimpleMembership?

How to extend SimpleMembership authentication in ASP.NET MVC4

But I would really like to work with the existing simplemembership system.

This post is pretty close: http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/

I saw this post too: 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

This is the closest I've found so far: http://weblogs.asp.net/thangchung/archive/2012/11/15/customize-the-simplemembership-in-asp-net-mvc-4-0.aspx

This is also useful, but for web pages: http://blog.osbornm.com/archive/2010/07/21/using-simplemembership-with-asp.net-webpages.aspx

I was hoping to find a more comprehensive walk along the correct path to extend it.

+3


source to share


1 answer


You don't seem to have an answer.

If I don't fully understand what you want to do, there is no need to modify / extend / customize the standard SimpleMembership to provide an email registration mechanism or assign a default role at registration time, as all of this can be done inside the AccountController.

As an example, here's the registration method I'm using:



    [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);
    }

      

There is no default role assigned here, but it would be easy to add as soon as the email confirmation is confirmed.

Since the question is quite old, I hope this helps someone!

+1


source







All Articles