Click here to Skip to main content
15,500,869 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team

I have a mvc app uses Individual User Account and have register method with login inside my controller. The users do registered on database table, but issue im not getting a confirmation from the email. Any issue as to why and yes i did so far debug the code for both of these method and does goes through for both.

What I have tried:

C#
<pre> //
        // 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);
        }


// 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);
        }
    }
Posted
Updated 9-Mar-20 0:33am

1 solution

You do realise that
C#
await UserManager.SendEmailAsync(emailExist.Id, "Confirm your account", ConfirmAccountMailBody(callbackUrl));
will only wait until the email is sent, not wait for the user to respond to it (or even receive it)?
If you want an email confirmation, you send it, and then process with confirming it from the page the user gets to when they click the URL in teh email they receive. You can't just send it and assume the user will respond immediately!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900