prevent users without confirmed email from logging in ASP.Net MVC with Identity 2

前端 未结 6 1158
遥遥无期
遥遥无期 2020-12-29 08:28

In microsoft Identity 2 there is ability to users can confirm there email addresses I downloaded Identity 2 sample project from here in this project there isn\'t any differ

6条回答
  •  旧巷少年郎
    2020-12-29 08:41

    Maybe its a little late but I hope it may help others.

    Add this

    var userid = UserManager.FindByEmail(model.Email).Id;
            if (!UserManager.IsEmailConfirmed(userid))
            {
                return View("EmailNotConfirmed");
            }
    

    before

    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
    

    The first block of code just checks if the email in the model exists in the database and gets it's id to check if it is not confirmed and if so returns a view to the user wich says so and if it is confirmed just lets the user sign in.

    And delete your changes to the result switch like this

    switch (result)
            {
                case SignInStatus.Success:
                        return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl });
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return View(model);
            }
    

提交回复
热议问题