示例#1
0
        private async void SendConfirmationEmail(ApplicationUser user)
        {
            var code        = Manager.GenerateEmailConfirmationToken(user.Id);
            var callbackUrl = Url.Action("ConfirmEmail", "Account",
                                         new { userId = user.Id, code, codeHashed = user.GeneratedGuid }, Request.Url.Scheme);
            var mailString = MailHtml.EmailConfirmHtml(user, callbackUrl);

            AppVar.Mailer.Send(user.Email, "Email Confirmation", mailString);
        }
        //[CompressFilter(Order = 1)]
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            var errors = new ErrorCollector();
            //External Validation.
            var validOtherConditions = await UserManager.ExternalUserValidation(model, _db, errors);

            if (ModelState.IsValid && validOtherConditions)
            {
                var user   = UserManager.GetUserFromViewModel(model); // get user from view model.
                var result = await Manager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    SignInProgrammatically(user, false);
                    if (model.Role == -1)
                    {
                    }
                    RoleManager.AddTempRoleInfo(user, model.Role);

                    if (AppVar.Setting.IsConfirmMailRequired && AppVar.Setting.IsFirstUserFound)
                    {
                        // mail needs to be confirmed.
                        // first user is already registered
                        #region Send an email to the user about mail confirmation

                        var code        = Manager.GenerateEmailConfirmationToken(user.Id);
                        var callbackUrl = Url.Action("ConfirmEmail", "Account",
                                                     new { userId = user.Id, code, codeHashed = user.GeneratedGuid }, Request.Url.Scheme);
                        var mailString = MailHtml.EmailConfirmHtml(user, callbackUrl);
                        AppVar.Mailer.Send(user.Email, "Email Confirmation", mailString);

                        #endregion

                        #region Sign out because registration is not complete

                        return(SignOutProgrammatically());

                        #endregion
                    }
                    if (!AppVar.Setting.IsFirstUserFound)
                    {
                        // first user is not registered yet
                        #region Send an email to the user about mail confirmation

                        var code        = Manager.GenerateEmailConfirmationToken(user.Id);
                        var callbackUrl = Url.Action("ConfirmEmail", "Account",
                                                     new { userId = user.Id, code, codeHashed = user.GeneratedGuid }, Request.Url.Scheme);
                        var mailString = MailHtml.EmailConfirmHtml(user, callbackUrl);
                        AppVar.Mailer.Send(user.Email, "Email Confirmation", mailString);

                        #endregion
                    }
                    CallCompleteRegistration(user.UserID);
                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            ViewBag.Roles = new SelectList(RoleManager.GetRoles(), "Id", "Name");
            return(View(model));
        }