public ActionResult Index([Bind(Prefix = "id")] string username)
        {
            _dbContext = new ApplicationDbContext();
            //Update the User Profile Details
            UserAccountService userAccountService = new UserAccountService(_dbContext);

            UserProfileVM userProfileVM = null;

            if (!string.IsNullOrEmpty(username))
            {
                userProfileVM = userAccountService.GetUserProfile(username);
            }

            string currentUserId = "";

            if (User.Identity.IsAuthenticated)
            {
                currentUserId = User.Identity.GetUserId();
            }

            if (userProfileVM == null && User.Identity.IsAuthenticated)
            {
                userProfileVM = userAccountService.GetUserProfileById(currentUserId);
            }

            if (userProfileVM == null)
            {
                return(HttpNotFound());
            }

            //Check whether the User is seeing his/her own Profile
            //If it is his/her ownn profile, return Edit Profile Page.
            //else return the Public Profile View
            ViewBag.HasUserFollowed = false;
            if (currentUserId.Equals(userProfileVM.Id))
            {
                ViewBag.YearsOfBirth  = UtilityExtension.GetYearsList();
                ViewBag.LanguagesList = UtilityExtension.GetLanguagesList();

                CurrencyService currencyService = new CurrencyService(_dbContext);
                ViewBag.CurrencyList = currencyService.GetAllCurrencies();

                userProfileVM.ProfileImageData = userAccountService.GetUserProfileImageData(userProfileVM.Id);

                return(View("UpdateProfile", userProfileVM));
            }
            else
            {
                if (User.Identity.IsAuthenticated)
                {
                    ViewBag.HasUserFollowed = userAccountService.GetUserFollower(currentUserId, userProfileVM.Id) != null;
                }
                return(View("PublicProfile", userProfileVM));
            }
        }
        public ActionResult Step2()
        {
            ViewBag.DisableEmailTextbox   = true;
            ViewBag.ShowResendEmailButton = false;
            _dbContext = new ApplicationDbContext();

            //Get Current User's Profile
            string             currentUserId      = User.Identity.GetUserId();
            UserAccountService userAccountService = new UserAccountService(_dbContext);
            UserProfileVM      userProfileVM      = userAccountService.GetUserProfileById(currentUserId);

            if (userProfileVM == null)
            {
                return(HttpNotFound());
            }
            if (string.IsNullOrEmpty(userProfileVM.UrlUsername))
            {
                userProfileVM.UrlUsername = User.Identity.GetExternalProviderUsername();
            }

            if (string.IsNullOrEmpty(userProfileVM.Email) || userProfileVM.EmailConfirmed == false)
            {
                ViewBag.DisableEmailTextbox = false;
            }
            if (!string.IsNullOrEmpty(userProfileVM.Email))
            {
                ViewBag.ShowResendEmailButton = true;
            }

            ViewBag.YearsOfBirth  = UtilityExtension.GetYearsList();
            ViewBag.LanguagesList = UtilityExtension.GetLanguagesList();

            CurrencyService currencyService = new CurrencyService(_dbContext);

            ViewBag.CurrencyList = currencyService.GetAllCurrencies();


            return(View(userProfileVM));
        }
        public ActionResult UpdateProfile(UserProfileVM userProfileVM)
        {
            //If User does not exists, just update the UserData
            try
            {
                _dbContext = new ApplicationDbContext();
                if (!ModelState.IsValid)
                {
                    goto FormValidationFailed_GetData;
                }

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

                //Update the User
                userProfileVM.Id = User.Identity.GetUserId();
                var userUpdateResponse = userAccountService.UpdateUserProfile(userProfileVM, false);
                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.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
                if (userUpdateResponse.SuccessCode == ResponseResultMessageCode.UserNameUpdated)
                {
                    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));
                    }

                    //Update preferred language code if changed
                    var preferredLangClaim = User.Identity.GetClaim(_ClaimTypes.PreferredLanguage);
                    if (!preferredLangClaim.Value.ToLower().Equals(userProfileVM.PreferredLanguage))
                    {
                        UserManager.RemoveClaim(userProfileVM.Id, preferredLangClaim);
                        UserManager.AddClaim(userProfileVM.Id, new Claim(_ClaimTypes.PreferredLanguage, userProfileVM.PreferredLanguage));
                        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
                #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

                //Now Check If Next button is clicked, goto Step 3
                //If Back button is clicked, goto Step 1
                return(RedirectToAction("Index", new { id = User.Identity.GetUrlUserName(), 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));
        }
        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));
        }