locked
Generate token for password reset (Asp.net VB.net Webforms) RRS feed

  • Question

  • User1567609808 posted

    I'm creating a website as simple as possible.

    I am Using the VS2017 community. I create a site like this:

    New> Project> Web> Previous Versions> ASP.NET Web Forms Site.

    Everything works fine but when I try to generate a token for password reset an error occurs.

    Dim token As String = Await manager.GeneratePasswordResetTokenAsync (user.Id)

    It seems that something is missing. Maybe "manager.UserTokenProvider"

    How do I implement a tokenProvider in this simplified template?

    Thank you very much

    Tuesday, July 3, 2018 2:44 AM

Answers

  • User283571144 posted

    Hi VB_Blue,

    According to your description, I suggest you could set the UserManager's UserTokenProvider to DataProtectorTokenProvider.

    Then it will work well.

    More details, you could refer to below codes:

    manager.aspx:

        Protected Sub ChangePassword_Click(sender As Object, e As EventArgs)
            If IsValid Then
                Dim manager = New UserManager()
                Dim dataProtectionProvider = New DpapiDataProtectionProvider("ASP.NET Identity")
                manager.UserTokenProvider = New DataProtectorTokenProvider(Of ApplicationUser)(dataProtectionProvider.Create("ASP.NET Identity"))
                Dim token As String = manager.GeneratePasswordResetToken(User.Identity.GetUserId())
                Dim result As IdentityResult = manager.ChangePassword(User.Identity.GetUserId(), CurrentPassword.Text, NewPassword.Text)
                If result.Succeeded Then
                    Dim userInfo = manager.FindById(User.Identity.GetUserId())
                    IdentityHelper.SignIn(manager, userInfo, isPersistent:=False)
                    Response.Redirect("~/Account/Manage?m=ChangePwdSuccess")
                Else
                    AddErrors(result)
                End If
            End If
        End Sub

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 3, 2018 6:05 AM

All replies

  • User283571144 posted

    Hi VB_Blue,

    According to your description, I suggest you could set the UserManager's UserTokenProvider to DataProtectorTokenProvider.

    Then it will work well.

    More details, you could refer to below codes:

    manager.aspx:

        Protected Sub ChangePassword_Click(sender As Object, e As EventArgs)
            If IsValid Then
                Dim manager = New UserManager()
                Dim dataProtectionProvider = New DpapiDataProtectionProvider("ASP.NET Identity")
                manager.UserTokenProvider = New DataProtectorTokenProvider(Of ApplicationUser)(dataProtectionProvider.Create("ASP.NET Identity"))
                Dim token As String = manager.GeneratePasswordResetToken(User.Identity.GetUserId())
                Dim result As IdentityResult = manager.ChangePassword(User.Identity.GetUserId(), CurrentPassword.Text, NewPassword.Text)
                If result.Succeeded Then
                    Dim userInfo = manager.FindById(User.Identity.GetUserId())
                    IdentityHelper.SignIn(manager, userInfo, isPersistent:=False)
                    Response.Redirect("~/Account/Manage?m=ChangePwdSuccess")
                Else
                    AddErrors(result)
                End If
            End If
        End Sub

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 3, 2018 6:05 AM
  • User1567609808 posted

    Perfect. Thank you.

    Tuesday, July 3, 2018 7:52 AM