示例#1
0
 public ActionResult Create(RegisterModel user, string[] selectedroles)
 {
     if (ModelState.IsValid)
     {
         try
         {
             AccountServices.CreateAccount(user.UserName, user.Password, user.Email, user.FirstName, user.LastName, user.TimeZone, user.Culture, false,selectedroles);
             return RedirectToRoute("Default", new { controller = "User", action = "Index" });
         }
         catch (MembershipCreateUserException ex)
         {
             if ((ex.StatusCode == MembershipCreateStatus.DuplicateUserName) || (ex.StatusCode == MembershipCreateStatus.InvalidUserName))
             {
                 ModelState.AddModelError("UserName", ErrorCodeToString(ex.StatusCode));
             }
             else if ((ex.StatusCode == MembershipCreateStatus.DuplicateEmail) || (ex.StatusCode == MembershipCreateStatus.InvalidEmail))
             {
                 ModelState.AddModelError("Email", ErrorCodeToString(ex.StatusCode));
             }
             else if (ex.StatusCode == MembershipCreateStatus.InvalidPassword)
             {
                 ModelState.AddModelError("Password", ErrorCodeToString(ex.StatusCode));
             }
             else
             {
                 ModelState.AddModelError("", ErrorCodeToString(ex.StatusCode));
             }
         }
         catch (Exception ex)
         {
             throw new Exception("Se ha producido un error en el envío del mail. Intentelo de nuevo en unos minutos.");
         }
     }
     return View(user);
 }
示例#2
0
        public ActionResult Register(RegisterModel model)
        {
            string recaptchaprivatekey = BgResources.Recaptcha_PrivateKeyHttp;
            try
            {
                if (!ReCaptcha.Validate(privateKey: recaptchaprivatekey))
                {
                    ModelState.AddModelError("recaptcha", Resources.AppMessages.Error_Recaptcha);
                }
            }
            catch (Exception)
            {

                ModelState.AddModelError("recaptcha", Resources.AppMessages.Error_Recaptcha_Key);
            }

            if (ModelState.IsValid)
            {
                string token = null;
                try
                {
                    token = CodeFirstSecurity.CreateAccount(model.UserName, model.Password, model.Email, model.FirstName, model.LastName, model.TimeZone, model.Culture, requireConfirmationToken: true);
                    SmtpClient client = new SmtpClient { Host = BgResources.Email_Server, Port = Int32.Parse(BgResources.Email_SmtpPort), EnableSsl = BgResources.Email_SSL, Credentials = new NetworkCredential(BgResources.Email_UserName,BgResources.Email_Password) } ;
                    UserMailer.Register(token, model.Email, AccountServices.FindUser(usr => usr.Username == model.UserName)).Send(new SmtpClientWrapper { InnerSmtpClient = client });
                    ViewBag.Email = model.Email;
                    return View("CompleteRegister");
                }
                catch (MembershipCreateUserException ex)
                {
                    if ((ex.StatusCode == MembershipCreateStatus.DuplicateUserName) || (ex.StatusCode == MembershipCreateStatus.InvalidUserName))
                    {
                        ModelState.AddModelError("UserName", ErrorCodeToString(ex.StatusCode));
                    }
                    else if ((ex.StatusCode == MembershipCreateStatus.DuplicateEmail) || (ex.StatusCode == MembershipCreateStatus.InvalidEmail))
                    {
                        ModelState.AddModelError("Email", ErrorCodeToString(ex.StatusCode));
                    }
                    else if (ex.StatusCode == MembershipCreateStatus.InvalidPassword)
                    {
                        ModelState.AddModelError("Password", ErrorCodeToString(ex.StatusCode));
                    }
                    else
                    {
                        ModelState.AddModelError("", ErrorCodeToString(ex.StatusCode));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(Resources.AppMessages.Error_SendMail);
                }
            }
            return View(model);
        }