ASP.NET使用VB.Net在EmailConfirmed为false时阻止登录(ASP.NET Prevent login when EmailConfirmed is false using V

编程入门 行业动态 更新时间:2024-10-23 09:23:50
ASP.NET使用VB.Net在EmailConfirmed为false时阻止登录(ASP.NET Prevent login when EmailConfirmed is false using VB.Net)

我想在以下代码中添加一个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 Class

I'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 If

But 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 Sub

This 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

更多推荐

本文发布于:2023-07-29 20:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:VB   Net   ASP   NET   Prevent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!