2017-03-09 30 views
0

我正在使用.net 4.5.2和sendgrid。我已经使用下面的链接作为指导,而不是使用sendgrid v2我正在使用sendgrid v3。.net网络应用程序的授权部分可在确认电子邮件被点击前访问

​​

电子邮件确认可与发送到注册用户的电子邮件地址的链接。点击该链接时,AspNetUsers中的“电子邮件确认”字段从false变为true。

但是,当用户第一次提交的报名表 - ,然后点击确认电子邮件之前 - 他们成为记录到系统中。不知怎的,_LoginPartial正在被调用,因为用户的电子邮件地址和注销最终位于导航栏的顶部。

所以只要注册之后,但电子邮件确认被点击之前想着有点ActionController的中登录的动作显然是被调用后。这不是在微软的文档,我不认为。

但任何建议来解决这将是伟大的。我可以检查AspNetUser表的EmailConfirmation == false。但是有没有一个合适的地方可以做到这一点?

我检查了这个职位Prevent login when EmailConfirmed is false和注释掉默认的登录操作代码,并用此代替下方,但它似乎并没有发挥作用。

if (ModelState.IsValid) 
      { 
       var user = await UserManager.FindByNameAsync(model.Email); 
       if (user == null) 
       { 
        ModelState.AddModelError("", "Invalid login attempt."); 
        return View(model); 
       } 
       //Add this to check if the email was confirmed. 
       if (!await UserManager.IsEmailConfirmedAsync(user.Id)) 
       { 
        ModelState.AddModelError("", "You need to confirm your email."); 
        return View(model); 
       } 
       if (await UserManager.IsLockedOutAsync(user.Id)) 
       { 
        return View("Lockout"); 
       } 
       if (await UserManager.CheckPasswordAsync(user, model.Password)) 
       { 
        // Uncomment to enable lockout when password login fails 
        //await UserManager.ResetAccessFailedCountAsync(user.Id); 
        return await LoginCommon(user, model.RememberMe, returnUrl); 
       } 
       else 
       { 
        // Uncomment to enable lockout when password login fails 
        //await UserManager.AccessFailedAsync(user.Id); 
        ModelState.AddModelError("", "Invalid login attempt."); 
        return View(model); 
       } 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 

register操作:

public async Task<ActionResult> Register(RegisterViewModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       var user = new ApplicationUser {UserName = model.Email, Email = model.Email }; 
       var result = await UserManager.CreateAsync(user, model.Password); 
       if (result.Succeeded) 
       { 
        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
        // Send an email with this link 

        /*These bottom three lines were commented out */ 
        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", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\"></a>"); 
        return RedirectToAction("ConfirmRegistration"); 
       } 
       AddErrors(result); 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 
     } 

登录操作:

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
{ 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 

     // 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: 
       ModelState.AddModelError("", "Invalid login attempt."); 
       return View(model); 
     } 
    } 

回答

0

在你Register行动,评论/删除行:

await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

这将禁用自动登录注册istering。然后,在您的Login操作中,在初始ModelState.IsValid检查后添加以下代码,以检查用户的电子邮件是否已得到确认:

var user = await UserManager.FindByEmailAsync(model.Email); 
if (user != null && !await UserManager.IsEmailConfirmedAsync(user.Id)) 
{ 
    ModelState.AddModelError("", "Please confirm your email address before signing in."); 
    return View(model); 
} 
相关问题