コード例 #1
0
        public async Task <ActionResult> SendConfirmEmail(string email)
        {
            string currentUserId = User.Identity.GetUserId();

            //Before creating a new token, we need to invalidate the old token.
            //So, update the security stamp for the user.
            UserManager.UpdateSecurityStamp(currentUserId);

            string code = await UserManager.GenerateEmailConfirmationTokenAsync(currentUserId);

            var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = currentUserId, code = code }, protocol: Request.Url.Scheme);

            string langCode = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
            bool   isSent   = new EmailHelperService().SendEmailConfirmationTokenMail(email, callbackUrl, Strings.ConfirmationEmailSubject, langCode);

            if (Request.IsAjaxRequest())
            {
                return(Json(new ResponseResult <string>
                {
                    Data = callbackUrl,
                    Success = true,
                    Message = "Email Confirmation Link is sent successfully."
                }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(View());
            }
        }
コード例 #2
0
        public async Task <ActionResult> Step2(UserProfileVM userProfileVM, string SubmitAction = "")
        {
            //If User does no exists, just update the UserData
            try
            {
                ViewBag.DisableEmailTextbox   = true;
                ViewBag.ShowResendEmailButton = false;
                _dbContext = new ApplicationDbContext();
                bool skipValidation = false;

                //If Next Button is clicked, perform the Form Validation, otherwise do not perform Validations
                if (!SubmitAction.ToLower().Equals("back"))
                {
                    if (!ModelState.IsValid)
                    {
                        goto FormValidationFailed_GetData;
                    }
                }
                else
                {
                    skipValidation = true;
                }

                //Update the User Profile Details
                UserAccountService userAccountService = new UserAccountService(_dbContext);

                //Check If the Username is unique
                userProfileVM.Id = User.Identity.GetUserId();

                //Update the User
                var userUpdateResponse = userAccountService.UpdateUserProfile(userProfileVM, skipValidation);
                if (userUpdateResponse.Success == false)
                {
                    if (userUpdateResponse.MessageCode == ResponseResultMessageCode.EmailExists)
                    {
                        ModelState.AddModelError("Email", "A user already exists with the same email address. Please choose a different one.");
                        ViewBag.DisableEmailTextbox = false;
                    }

                    /*else if (userUpdateResponse.MessageCode == ResponseResultMessageCode.EmailNotConfirmed)
                     * {
                     *  ModelState.AddModelError("Email", "You have not confirm your email address. Please confirm your email address to continue. Click on the \"Send Confirmation\" to send the confirmation link again.");
                     *  ViewBag.DisableEmailTextbox = false;
                     * }*/
                    else if (userUpdateResponse.MessageCode == ResponseResultMessageCode.UserNameExists)
                    {
                        ModelState.AddModelError("UrlUsername", "A user already exists with the same username. Please choose a different one.");
                    }
                    else
                    {
                        ModelState.AddModelError("", ResponseResultMessageCode.GetMessageFromCode(userUpdateResponse.MessageCode));
                    }
                    goto FormValidationFailed_GetData;
                }

                //If User is Updated Successfully, Change UrlUserName Claim Also
                var owinContext = HttpContext.GetOwinContext();
                var UserManager = owinContext.GetUserManager <ApplicationUserManager>();

                //New Claims List, for current Identity
                List <Claim> newClaimsList = new List <Claim>();

                Claim UserNameClaim = User.Identity.GetClaim(_ClaimTypes.UrlUserName);
                if (UserNameClaim != null)
                {
                    //If the Username is changed, only then update the Claim
                    if (!UserNameClaim.Value.ToLower().Equals(userProfileVM.UrlUsername.ToLower()))
                    {
                        UserManager.RemoveClaim(userProfileVM.Id, UserNameClaim);
                        UserManager.AddClaim(userProfileVM.Id, new Claim(_ClaimTypes.UrlUserName, userProfileVM.UrlUsername));

                        newClaimsList.Add(new Claim(_ClaimTypes.UrlUserName, userProfileVM.UrlUsername));
                    }
                }
                else
                {
                    UserManager.AddClaim(userProfileVM.Id, new Claim(_ClaimTypes.UrlUserName, userProfileVM.UrlUsername));

                    newClaimsList.Add(new Claim(_ClaimTypes.UrlUserName, userProfileVM.UrlUsername));
                }


                //If User is Updated then the Wizard is Completed, Change HasCompletedProfileWizard Claim Also
                Claim HasCompletedProfileWizardClaim = User.Identity.GetClaim(_ClaimTypes.HasCompletedProfileWizard);
                if (HasCompletedProfileWizardClaim != null)
                {
                    UserManager.RemoveClaim(userProfileVM.Id, HasCompletedProfileWizardClaim);
                }

                UserManager.AddClaim(userProfileVM.Id, new Claim(_ClaimTypes.HasCompletedProfileWizard, true.ToString()));
                UserManager.AddClaim(userProfileVM.Id, new Claim(_ClaimTypes.PreferredLanguage, userProfileVM.PreferredLanguage));

                newClaimsList.Add(new Claim(_ClaimTypes.HasCompletedProfileWizard, true.ToString()));
                newClaimsList.Add(new Claim(_ClaimTypes.PreferredLanguage, userProfileVM.PreferredLanguage));


                //Update Current Identity Claim and Login User Again
                if (newClaimsList.Count > 0)
                {
                    User.Identity.AddOrUpdateClaims(newClaimsList, owinContext.Authentication);
                }

                //User update was successfull
                //Now Check If Next button is clicked, goto Step 3
                //If Back button is clicked, goto Step 1
                if (SubmitAction.ToLower().Equals("back"))
                {
                    return(RedirectToAction("Step1"));
                }
                else
                {
                    //Before Redirecting to Step 3, send out an email confirmation link mail.
                    if (userProfileVM.EmailConfirmed == false)
                    {
                        string code = await UserManager.GenerateEmailConfirmationTokenAsync(userProfileVM.Id);

                        var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userProfileVM.Id, code = code }, protocol: Request.Url.Scheme);

                        string langCode = Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
                        bool   isSent   = new EmailHelperService().SendEmailConfirmationTokenMail(userProfileVM.Email, callbackUrl, Strings.ConfirmationEmailSubject, langCode);

                        TempData["ResponseResult"] = new ResponseResult <object>
                        {
                            Message = Strings.Step3_EmailConfirmationLinkText,
                            Success = true
                        };
                    }


                    #region Set user preferred language
                    string userPreferredLanguageCode = "en";
                    if (User != null && User.Identity != null && User.Identity.GetClaim("PreferredLanguage") != null)
                    {
                        userPreferredLanguageCode = User.Identity.GetClaim("PreferredLanguage").Value;
                    }
                    #endregion

                    return(RedirectToAction("Step3", "Profile", new { lang = userPreferredLanguageCode }));
                }
            }
            catch (Exception err)
            {
                ModelState.AddModelError("", err);
                goto FormValidationFailed_GetData;
            }

            //Goto Statement If form validation is failed, goto View but first get the required data for View
FormValidationFailed_GetData:
            ViewBag.YearsOfBirth  = UtilityExtension.GetYearsList();
            ViewBag.LanguagesList = UtilityExtension.GetLanguagesList();

            CurrencyService currencyService = new CurrencyService(_dbContext);
            ViewBag.CurrencyList = currencyService.GetAllCurrencies();
            return(View(userProfileVM));
        }