public ActionResult Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // Attempt to register the user
            var roles = (SimpleRoleProvider)Roles.Provider;
            if (!roles.RoleExists(SystemRole.Admin))
            {
                roles.CreateRole(SystemRole.Admin);
            }
            if (!roles.RoleExists(SystemRole.Partner))
            {
                roles.CreateRole(SystemRole.Partner);
            }
            if (!roles.RoleExists(SystemRole.User))
            {
                roles.CreateRole(SystemRole.User);
            }

            model.UserName = model.UserName.Trim();
            model.Password = model.Password.Trim();
            model.ConfirmPassword = model.ConfirmPassword.Trim();
            model.CompanyName = model.CompanyName == null ? "" : model.CompanyName.Trim();

            // Register user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                WebSecurity.Login(model.UserName, model.Password);
                roles.AddUsersToRoles(new[] {model.UserName}, new[] {SystemRole.User});
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                return View(model);
            }

            // Generate verification code 
            var verificationCode = Guid.NewGuid();
            var profile = _ctx.DUserProfiles.FirstOrDefault(p => p.UserName == model.UserName);
            profile.VerificationCode = verificationCode;
            profile.CompanyName = model.CompanyName;
            profile.FirstName = model.FirstName;
            profile.MiddleName = model.MiddleName;
            profile.LastName = model.LastName;
            profile.WorkEmail = model.WorkEmail;
            profile.WorkPhone = model.WorkPhone;
            profile.CellPhone = model.CellPhone;
            profile.AcademicDegree = model.AcademicDegree;
            profile.Position = model.Position;
            profile.Country = model.Country;
            profile.City = model.City;
            profile.Info = model.Info;
            _ctx.SaveChanges();

            SendEmailToUser(model, verificationCode);
            SendEmailToManager(model);

            return RedirectToAction("Profile", "Account", new { justRegistered = true });
        }
 public ActionResult Register()
 {
     var m = new RegisterModel();
     return View(m);
 }
 public ActionResult RegisterComplete()
 {
     var m = new RegisterModel();
     m.UserName = User.Identity.Name;
     return View(m);
 }
        private static void ResendVerificationToUser(RegisterModel model, Guid verificationCode)
        {
            try
            {
                var url = String.Format("{0}/account/verify/{1}",
                    WebConfigurationManager.AppSettings["AppHost"],
                    verificationCode);
                var anchor = String.Format("<a href=\"{0}\" target=\"_blank\">{0}</a>", url);
                using (var mail = new MailMessage())
                {
                    mail.From = new MailAddress(WebConfigurationManager.AppSettings["SmtpSenderMail"], WebConfigurationManager.AppSettings["SmtpSenderName"]);
                    mail.To.Add(new MailAddress(model.UserName));
                    mail.Subject = "Offwind verification";

                    var text = new StringBuilder();
                    text.AppendFormat("Welcome to Offwind!<br /><br />");
                    text.AppendFormat("Your account: {0}<br />", model.UserName);
                    text.AppendFormat("Verification code: {0}<br />", verificationCode);
                    text.AppendFormat("You can simply follow the link: {0}<br /><br />", anchor);
                    text.AppendFormat("Best regards,<br />");
                    text.AppendFormat("Offwind group");
                    mail.Body = text.ToString();
                    mail.IsBodyHtml = true;

                    var smtpClient = new SmtpClient()
                    {
                        Host = WebConfigurationManager.AppSettings["SmtpHost"],
                        Port = Convert.ToInt32(WebConfigurationManager.AppSettings["SmtpHostPort"]),
                        EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["SmtpEnableSSL"]),
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials =
                            Convert.ToBoolean(WebConfigurationManager.AppSettings["SmtpUseDefaultCredentialas"]),
                        Credentials = new NetworkCredential()
                        {
                            UserName = WebConfigurationManager.AppSettings["SmtpSenderMail"],
                            Password = WebConfigurationManager.AppSettings["SmtpSenderPswd"]
                        }
                    };
                    smtpClient.Send(mail);
                }
            }
            catch (Exception)
            {
                // TODO: Add logging
                //throw;
            }
        }
        private void SendEmailToManager(RegisterModel model)
        {
            try
            {
                var url = String.Format("{0}/account/profile?userName={1}", WebConfigurationManager.AppSettings["AppHost"], Server.UrlEncode(model.UserName));
                var href = String.Format("<a href=\"{0}\">{1} {2}</a>", url, model.FirstName, model.LastName);
                using (var mail = new MailMessage())
                {
                    mail.From = new MailAddress(WebConfigurationManager.AppSettings["SmtpSenderMail"], WebConfigurationManager.AppSettings["SmtpSenderName"]);
                    mail.To.Add(new MailAddress(WebConfigurationManager.AppSettings["ManagerEmail1"]));
                    mail.CC.Add(new MailAddress(WebConfigurationManager.AppSettings["ManagerEmail2"]));
                    mail.CC.Add(new MailAddress(WebConfigurationManager.AppSettings["ManagerEmail3"]));
                    mail.Subject = "Offwind new user [" + model.UserName + "]";

                    var text = new StringBuilder();
                    text.AppendFormat("Notification about new user registered<br /><br />");
                    text.AppendFormat("View user profile {0}", href);
                    mail.Body = text.ToString();
                    mail.IsBodyHtml = true;

                    var smtpClient = new SmtpClient()
                    {
                        Host = WebConfigurationManager.AppSettings["SmtpHost"],
                        Port = Convert.ToInt32(WebConfigurationManager.AppSettings["SmtpHostPort"]),
                        EnableSsl = Convert.ToBoolean(WebConfigurationManager.AppSettings["SmtpEnableSSL"]),
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        UseDefaultCredentials =
                            Convert.ToBoolean(WebConfigurationManager.AppSettings["SmtpUseDefaultCredentialas"]),
                        Credentials = new NetworkCredential()
                        {
                            UserName = WebConfigurationManager.AppSettings["SmtpSenderMail"],
                            Password = WebConfigurationManager.AppSettings["SmtpSenderPswd"]
                        }
                    };
                    smtpClient.Send(mail);
                }
            }
            catch (Exception)
            {
                // TODO: Add logging
                //throw;
            }
        }
        public ActionResult ResendVerificationCode()
        {
            var verificationCode = Guid.NewGuid();
            var profile = _ctx.DUserProfiles.FirstOrDefault(p => p.UserName == User.Identity.Name);
            profile.VerificationCode = verificationCode;
            _ctx.SaveChanges();

            var model = new RegisterModel();
            model.UserName = User.Identity.Name;
            ResendVerificationToUser(model, verificationCode);
            return RedirectToAction("Profile", new { verificationResent = true });
        }