public ActionResult Validate(string userid, string code)
        {
            if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(userid))
            {
                int userId = Convert.ToInt32(userid);

                MembershipUser mu = Membership.GetUser(userId);
                if (mu != null)
                {
                    RegisteredUser ru = registeredUserRepository.GetByMembershipId(userId);

                    if (!mu.IsApproved)
                    {
                        // Make sure is using the valid code
                        if (ru != null && ru.RegistrationCode.Trim().ToLower() == code.Trim().ToLower())
                        {
                            // Approve on Membership
                            mu.IsApproved = true;
                            Membership.UpdateUser(mu);

                            // Approve on System
                            ru.Confirm();
                            ru.Closet.PrivacyLevel = PrivacyLevel.FullCloset;
                            registeredUserRepository.SaveOrUpdate(ru);
                            // Send approval mail
                            messageSenderService.SendWithTemplate("approvedmail", ru, ru.UserName, ru.EmailAddress);
                            return(RedirectToAction("Index", "Login", new { validatedUser = true }));
                        }
                    }
                    else
                    {
                        if (ru != null && ru.RegistrationCode.Trim().ToLower() == code.Trim().ToLower())
                        {
                            ru.EmailAddress = ru.NewMail;
                            mu.Email        = ru.EmailAddress;
                            ru.NewMail      = string.Empty;
                            registeredUserRepository.SaveOrUpdate(ru);
                            Membership.UpdateUser(mu);
                            // Send approval mail
                            messageSenderService.SendWithTemplate("approvedmail", ru, ru.UserName, ru.EmailAddress);
                            return(RedirectToAction("Index", "Home", new { validatedUser = true }));
                        }
                    }
                }
            }

            return(RedirectToAction("Index", new { invalidCode = true, userid = userid }));
        }
        public ActionResult Register(UserRegistration userRegistration)
        {
            if (ModelState.IsValid)
            {
                IList <UserFlavor> userFlavors    = ClosetState.UserFlavors as List <UserFlavor>;
                IList <EventType>  eventTypes     = ClosetState.EventTypes as List <EventType>;
                IList <Garment>    mygarments     = garmentRepository.GetByIds(ClosetState.AddGarments) as List <Garment>;
                IList <Garment>    mywishlist     = garmentRepository.GetByIds(ClosetState.WishGarments) as List <Garment>;
                string             channel        = ClosetState.Channel;
                string             invitationCode = ClosetState.InvitationCode;

                SecurityQuestion sq = securityQuestionRepository.Get(Convert.ToInt32(userRegistration.SecurityQuestion));

                // Create Membership User
                MembershipCreateStatus status;
                MembershipUser         mu = Membership.CreateUser(userRegistration.UserName, userRegistration.Password, userRegistration.Email, sq.Description, userRegistration.SecurityAnswer, false, out status);
                if (status != MembershipCreateStatus.Success)
                {
                    ViewData["Errors"] = new string[] { status.ToString() }
                }
                ;

                try
                {
                    bool mustConfirmMail = registerMemberService.RegisterMember(userRegistration.Email,
                                                                                userRegistration.UserName, userRegistration.FirstName, userRegistration.LastName,
                                                                                userRegistration.Password,
                                                                                new UserSize(Convert.ToInt32(userRegistration.UserSize)),
                                                                                Convert.ToInt32(mu.ProviderUserKey),
                                                                                userRegistration.ZipCode,
                                                                                userFlavors,
                                                                                eventTypes,
                                                                                mygarments,
                                                                                mywishlist,
                                                                                Url.Action("Validate", "EmailConfirmation"),
                                                                                channel,
                                                                                invitationCode);

                    // Assign User Role
                    Roles.AddUserToRole(mu.UserName, "User");

                    if (mustConfirmMail)
                    {
                        ClosetState.Clear();
                        return(RedirectToAction("Index", "EmailConfirmation", new { userid = mu.ProviderUserKey }));
                    }

                    //The user already confirmed his email, so we need to approve him and go to the login
                    // Approve on Membership
                    mu.IsApproved = true;
                    Membership.UpdateUser(mu);

                    RegisteredUser ru =
                        registeredUserRepository.GetByMembershipId(Convert.ToInt32(mu.ProviderUserKey));

                    // Approve on System
                    ru.Confirm();
                    ru.Closet.PrivacyLevel = PrivacyLevel.FullCloset;
                    registeredUserRepository.SaveOrUpdate(ru);

                    ClosetState.Clear();
                    return(RedirectToAction("Index", "Login", new { validatedUser = true }));
                }
                catch (Exception ex)
                {
                    // Try to delete the incomplete created user because something went wrong.
                    try { Membership.DeleteUser(userRegistration.UserName); }
                    catch { }

                    if (ex is InvalidInvitationCodeException)
                    {
                        ModelState.AddModelError("InvitationCode", "The code is not valid or already used.");
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            else
            {
                if (new List <ModelState>(ModelState.Values).Find(e => e.Value == null).Errors[0].ErrorMessage.StartsWith("Email"))
                {
                    ModelState.AddModelError("Email", "Email does not match.");
                }
                else
                {
                    ModelState.AddModelError("Password", "Password does not match.");
                }
            }

            GetRegistrationInfo(userRegistration);
            return(View("Index", userRegistration));
        }