locked
How do I generate a password token to reset a user password RRS feed

  • Question

  • User297437924 posted

    I wrote the following code, some of it pasted from a MSDN page:

    Public Class ApplicationUserManager
    Inherits UserManager(Of ApplicationUser)
    Public Sub New()
    MyBase.New(New UserStore(Of ApplicationUser)(New ApplicationDbContext()))
    
    End Sub
    End Class

    then I did the following, which is an attempt to generate a token that I can email to a user who has forgotten his password:



    Private Shared Provider As DpapiDataProtectionProvider = New DpapiDataProtectionProvider("rateForSuccess")
    Private Shared userManager As New ApplicationUserManager

    Public Shared Function GenerateResetPassword(ByVal UserID As String) As String Dim code As String If Provider IsNot Nothing Then Dim oTokenProvider As Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider(Of ApplicationUser) oTokenProvider = New DataProtectorTokenProvider(Of ApplicationUser)(Provider.Create("ASP.NET Identity")) oTokenProvider.TokenLifespan = TimeSpan.FromDays(1) userManager.UserTokenProvider = oTokenProvider End If code = userManager.GeneratePasswordResetToken(UserID) Return code End Function

    This gives the following error (see below).   The error does not occur when I run the program in Visual Studio on my local PC, but does occur when I ftp all my files to a remote server and try it there.   Any help is appreciated.

    The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Security.Cryptography.CryptographicException: The data protection operation was unsuccessful. This may have been caused by not having the user profile loaded for the current thread's user context, which may be the case when the thread is impersonating.

    Monday, August 6, 2018 8:46 PM

Answers

  • User283571144 posted

    Hi ReteFor,

    According to your description, I suggest you could follow below steps to enable the userprofile.

    • Open your IIS Manager
    • Find out what AppPool your application is using by selecting your App, right-click on it, and Select Manage Application -> Advanced Settings.
    • After that, on the top left hand side, select Applications Pools, and go ahead and select the App Pool used by your app.
    • Right-click on it, and select Advanced Settings, Go to the Process Model Section and Find the "Load User Profile" Option and set it to true.

    Image:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 7, 2018 6:49 AM