我想在以下代码中添加一个if语句来拒绝尚未确认其电子邮件地址的用户 - 类似这样的伪代码:
if EmailConfirmed = false Then RedirectToURL and user = ""在
Protected Sub LogIn(sender As Object, e As EventArgs) If IsValid Then ' Validate the user password Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)() Dim signinManager = Context.GetOwinContext().GetUserManager(Of ApplicationSignInManager)() ' This doen't count login failures towards account lockout ' To enable password failures to trigger lockout, change to shouldLockout := True Dim result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout:=True) Select Case result Case SignInStatus.Success IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response) Exit Select Case SignInStatus.LockedOut Response.Redirect("/Account/Lockout") Exit Select Case SignInStatus.RequiresVerification Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", Request.QueryString("ReturnUrl"), RememberMe.Checked), True) Exit Select Case Else FailureText.Text = "Invalid login attempt" ErrorMessage.Visible = True Exit Select End Select End If End Sub End Class我已经看到它在C#中完成,但我无法准确复制或转换它。 感谢您的任何帮助。
UPDATE
当我使用代码@HumbleBeginnings链接并转换为VB我得到:
Dim user = Await.UserManager.FindByNameAsync(model.Email) If user IsNot Nothing Then If Not Await UserManager.IsEmailConfirmedAsync(user.Id) Then ViewBag.errorMessage = "You must have a confirmed email to log on." Return View("Error") End If但是说await只能用于Async方法,考虑用Async修饰符更改此方法并将其返回类型更改为任务
I want to add an if statement in the following code to deny users who haven't confirmed their email addresses - something like this pseudo code:
if EmailConfirmed = false Then RedirectToURL and user = ""in
Protected Sub LogIn(sender As Object, e As EventArgs) If IsValid Then ' Validate the user password Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)() Dim signinManager = Context.GetOwinContext().GetUserManager(Of ApplicationSignInManager)() ' This doen't count login failures towards account lockout ' To enable password failures to trigger lockout, change to shouldLockout := True Dim result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout:=True) Select Case result Case SignInStatus.Success IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response) Exit Select Case SignInStatus.LockedOut Response.Redirect("/Account/Lockout") Exit Select Case SignInStatus.RequiresVerification Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", Request.QueryString("ReturnUrl"), RememberMe.Checked), True) Exit Select Case Else FailureText.Text = "Invalid login attempt" ErrorMessage.Visible = True Exit Select End Select End If End Sub End ClassI've seen it done in C#, but I was unable to duplicate or convert it accurately. Thank you for any help.
UPDATE
When I use the code there @HumbleBeginnings link and convert to VB I get:
Dim user = Await.UserManager.FindByNameAsync(model.Email) If user IsNot Nothing Then If Not Await UserManager.IsEmailConfirmedAsync(user.Id) Then ViewBag.errorMessage = "You must have a confirmed email to log on." Return View("Error") End IfBut is says that await can only be used with Async method, consider changing this method with Async modifier and changing it's return type to task
最满意答案
查看此链接,特别是“登录前需要确认电子邮件”标题下的部分。 账户/注册控制器中有一条注释掉的线似乎可以解决问题。 这是在C#但它应该让你接近。 希望这可以帮助。
This was how I ended solving this issue:
Protected Async Sub LogIn(sender As Object, e As EventArgs) If IsValid Then ' Validate the user password Dim manager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)() Dim signinManager = Context.GetOwinContext().GetUserManager(Of ApplicationSignInManager)() ' This doen't count login failures towards account lockout ' To enable password failures to trigger lockout, change to shouldLockout := True Dim result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout:=True) Dim user = Await manager.FindByNameAsync(Email.Text) If user Is Nothing Then ModelState.AddModelError("", "Invalid login attempt.") Else End If 'Add this to check if the email was confirmed. If Not Await manager.IsEmailConfirmedAsync(user.Id) Then ConfirmationExpected.Text = "Your email address has not been confirmed <br/> <br/>Please Check your email (including spam folder) <br/>OR Click 'Register' to have a new confirmation email generated." ConfirmationRequired.Visible = True Else Select Case result Case SignInStatus.Success IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response) Exit Select Case SignInStatus.LockedOut Response.Redirect("/Account/Lockout") Exit Select Case SignInStatus.RequiresVerification Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}", Request.QueryString("ReturnUrl"), RememberMe.Checked), True) Exit Select Case Else FailureText.Text = "Invalid login attempt" ErrorMessage.Visible = True Exit Select End Select End If End If End SubThis was the added code:
Dim user = Await manager.FindByNameAsync(Email.Text) If user Is Nothing Then ModelState.AddModelError("", "Invalid login attempt.") Else End If 'Add this to check if the email was confirmed. If Not Await manager.IsEmailConfirmedAsync(user.Id) Then ConfirmationExpected.Text = "Your email address has not been confirmed <br/> <br/>Please Check your email (including spam folder) <br/>OR Click 'Register' to have a new confirmation email generated." ConfirmationRequired.Visible = True Else更多推荐
发布评论