Exemplo n.º 1
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                var requireEmailConfirmation =
                    Convert.ToBoolean(ConfigurationManager.AppSettings["requireEmailConfirmation"] ?? "false");
                var token = WebSecurityService.CreateUserAndAccount(model.UserName, model.Password,
                                                                    requireConfirmationToken: requireEmailConfirmation);

                if (requireEmailConfirmation)
                {
                    // Send email to user with confirmation token
                    if (Request.Url != null)
                    {
                        string hostUrl         = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
                        string confirmationUrl = hostUrl +
                                                 VirtualPathUtility.ToAbsolute("~/Account/Confirm?confirmationCode=" +
                                                                               HttpUtility.UrlEncode(token));

                        const string fromAddress = "Your Email Address";
                        var          toAddress   = model.Email;
                        const string subject     =
                            "Thanks for registering but first you need to confirm your registration...";
                        var body =
                            string.Format(
                                "Your confirmation code is: {0}. Visit <a href=\"{1}\">{1}</a> to activate your account.",
                                token, confirmationUrl);

                        // NOTE: This is just for sample purposes
                        // It's generally a best practice to not send emails (or do anything on that could take a long time and potentially fail)
                        // on the same thread as the main site
                        // You should probably hand this off to a background MessageSender service by queueing the email, etc.
                        MessengerService.Send(fromAddress, toAddress, subject, body, true);
                    }

                    // Thank the user for registering and let them know an email is on its way
                    return(RedirectToAction("Thanks", "Account"));
                }
                // Navigate back to the homepage and exit
                WebSecurityService.Login(model.UserName, model.Password);
                return(RedirectToAction("Index", "Home"));
            }

            // If we got this far, something failed, redisplay form
            ViewBag.PasswordLength = WebSecurityService.MinPasswordLength;
            return(View(model));
        }
Exemplo n.º 2
0
 public ActionResult Register(RegisterModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             WebSecurityService.CreateUserAndAccount(model.UserName, model.Password);
             WebSecurityService.Login(model.UserName, model.Password);
             return(RedirectToAction("Index", "Album"));
         }
         catch (MembershipCreateUserException e)
         {
             ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
         }
     }
     return(View(model));
 }