Пример #1
0
        public ActionResult Register(RegisteredUser newUser)
        {
            //when user registers in checks model requirements to ensure valid input
            if (ModelState.IsValid)
            {
                CaptchaHelper captchaHelper = new CaptchaHelper();
                string captchaResponse = captchaHelper.CheckRecaptcha();
                ViewBag.CaptchaResponse = captchaResponse;

                // add user to database, lock account until email confirmation
                var userStore = new UserStore<IdentityUser>();
                UserManager<IdentityUser> manager = new UserManager<IdentityUser>(userStore)
                {
                    //set account to lock after consecutive failed login attempts
                    UserLockoutEnabledByDefault = true,
                    DefaultAccountLockoutTimeSpan = new TimeSpan(0, 10, 0),
                    MaxFailedAccessAttemptsBeforeLockout = 3
                };

                var identityUser = new IdentityUser()
                {
                    UserName = newUser.UserName,
                    Email = newUser.Email
                };
                IdentityResult result = manager.Create(identityUser, newUser.Password);

                if (result.Succeeded)
                {
                    samUserRegEntities context = new samUserRegEntities();
                    AspNetUser user = context.AspNetUsers
                        .Where(u => u.UserName == newUser.UserName).FirstOrDefault();
                    AspNetRole role = context.AspNetRoles
                        .Where(r => r.Name == "registered").FirstOrDefault();

                    user.AspNetRoles.Add(role);
                    context.SaveChanges();

                    //creates token to be passed to mail helper to allow email confirmation
                    CreateToken ct = new CreateToken();
                    CreateTokenProvider(manager, EMAIL_CONFIRMATION);

                    var code = manager.GenerateEmailConfirmationToken(identityUser.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Home",
                                                    new { userId = identityUser.Id, code = code },
                                                        protocol: Request.Url.Scheme);
                    //send callbackURL to email helper
                    MailHelper mailer = new MailHelper();
                    string email = "Please confirm your account by clicking this link: <a href=\""
                                    + callbackUrl + "\">Confirm Registration</a>";
                    string subject = "Please confirm your email";
                    //try
                    //{
                        mailer.EmailFromArvixe(email, identityUser.Email, subject);
                        ViewBag.FakeConfirmation =
                            "An account confirmation has been sent to your email, please confirm before attempting to login";
                    //}
                    //catch (System.Exception ex)
                    //{
                    //    ViewBag.FakeConfirmation = ex.Message;
                    //}

                }
            }
            return View();
        }
Пример #2
0
        public ActionResult Register(RegisteredUser newUser)
        {
            //when user registers in checks model requirements to ensure valid input
            if (ModelState.IsValid)
            {
                CaptchaHelper captchaHelper   = new CaptchaHelper();
                string        captchaResponse = captchaHelper.CheckRecaptcha();
                ViewBag.CaptchaResponse = captchaResponse;

                // add user to database, lock account until email confirmation
                var userStore = new UserStore <IdentityUser>();
                UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore)
                {
                    //set account to lock after consecutive failed login attempts
                    UserLockoutEnabledByDefault          = true,
                    DefaultAccountLockoutTimeSpan        = new TimeSpan(0, 10, 0),
                    MaxFailedAccessAttemptsBeforeLockout = 3
                };

                var identityUser = new IdentityUser()
                {
                    UserName = newUser.UserName,
                    Email    = newUser.Email
                };
                IdentityResult result = manager.Create(identityUser, newUser.Password);

                if (result.Succeeded)
                {
                    samUserRegEntities context = new samUserRegEntities();
                    AspNetUser         user    = context.AspNetUsers
                                                 .Where(u => u.UserName == newUser.UserName).FirstOrDefault();
                    AspNetRole role = context.AspNetRoles
                                      .Where(r => r.Name == "registered").FirstOrDefault();

                    user.AspNetRoles.Add(role);
                    context.SaveChanges();

                    //creates token to be passed to mail helper to allow email confirmation
                    CreateToken ct = new CreateToken();
                    CreateTokenProvider(manager, EMAIL_CONFIRMATION);


                    var code        = manager.GenerateEmailConfirmationToken(identityUser.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Home",
                                                 new { userId = identityUser.Id, code = code },
                                                 protocol: Request.Url.Scheme);
                    //send callbackURL to email helper
                    MailHelper mailer = new MailHelper();
                    string     email  = "Please confirm your account by clicking this link: <a href=\""
                                        + callbackUrl + "\">Confirm Registration</a>";
                    string subject = "Please confirm your email";
                    //try
                    //{
                    mailer.EmailFromArvixe(email, identityUser.Email, subject);
                    ViewBag.FakeConfirmation =
                        "An account confirmation has been sent to your email, please confirm before attempting to login";
                    //}
                    //catch (System.Exception ex)
                    //{
                    //    ViewBag.FakeConfirmation = ex.Message;
                    //}
                }
            }
            return(View());
        }