0

enter image description hereI have an issue, i have register and login app using asp.net mvc. But the problem most of my users are get register via db table under users. Yet if i want to sign in as that user id, im getting an error from my controller method under Index(LoginViewModel model) "Email is not verified. Please check your email and verify account". If i go and check i dont get any notification, but when i do a query on database table i see the list of those where not approved by the Admin under pending users. I need some help to fix this issue. I am using Individual User account on the Database side to do, meaning they table names comes when created with these lists of table fields.

// Controller 

  // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Index(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);

            }

            var emailExist = await UserManager.FindByEmailAsync(model.Email);
            if(emailExist != null)
            {
                if(emailExist.EmailConfirmed == false)
                {
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(emailExist.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = emailExist.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(emailExist.Id, "Confirm your account", ConfirmAccountMailBody(callbackUrl));

                    TempData["ErrorMessage"] = "Email id is not verified. Please check your email and verify account !!!";
                    ViewBag.JavaScriptFunction = "ShowErrorPopup();";
                    return View(model);
                }
            }
            else
            {
                TempData["ErrorMessage"] = "Email is not registered !!!";
                ViewBag.JavaScriptFunction = "ShowErrorPopup();";
                return View(model);
            }

            var loggedinUser = await UserManager.FindAsync(model.Email, model.Password);
            if(loggedinUser !=null)
            {
                await UserManager.UpdateSecurityStampAsync(loggedinUser.Id);
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
                case SignInStatus.Failure:
                default:
                    TempData["ErrorMessage"] = "Email or Password is Incorrect";
                    ViewBag.JavaScriptFunction = "ShowErrorPopup();";
                    return View(model);
            }
        }

       // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {   
                if(model.Password !=model.ConfirmPassword)
                {
                    TempData["ErrorMessage"] = "New Password must be different from Old Password!!!";
                    ViewBag.JavaScriptFunction = "ShowErrorPopup();";
                    return View(model);
                }
                var user = new ApplicationUser
                {
                    UserName = model.Email,
                    Email = model.Email,
                    FirstName = model.FirstName
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if(result.Succeeded)
                {
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", ConfirmAccountMailBody(callbackUrl));

                    model.UserRoles = "PendingUsers";

                    await this.UserManager.AddToRolesAsync(user.Id, model.UserRoles);
                    return View();
                }
                foreach(var error in result.Errors)
                {
                    TempData["ErrorMessage"] = error;
                }

            }

            ViewBag.JavaScriptFunction = "ShowErrorPopup();";
            return View(model);
        }
Tera2020
  • 33
  • 2

0 Answers0