title: ASP.NET Identity: use GeneratePasswordResetToken on Azure website
body: <p>I have my web application deployed on Microsoft Azure. However when I want to generate a PasswordResetToken with: </p>
  
  <pre><code>var token = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>How do I get this to work on Azure?</p>
  
  <p>Or is there an other way to reset a password without knowing the old password?</p>
  
  <p>This is my UserManager class. Mabey there is an error in it.</p>
  
  <pre><code>public class ApplicationUserManager : UserManager&lt;ApplicationIdentityUser&gt;
  {
      private static IUnitOfWork _unitOfWork;
      private readonly IRepository&lt;ApplicationIdentityUser&gt; _userRepository;
  
  
      public ApplicationUserManager(IUserStore&lt;ApplicationIdentityUser&gt; store, IRepository&lt;ApplicationIdentityUser&gt; userRepository)
          : base(store)
      {
          if (userRepository == null) throw new ArgumentNullException("userRepository");
  
          _userRepository = userRepository;
  
          if (bool.Parse(ConfigurationManager.AppSettings["RunningInAzure"]))
              UserTokenProvider = new EmailTokenProvider&lt;ApplicationIdentityUser, string&gt;();
          else
          {
              var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("TopRijden");
              UserTokenProvider = new DataProtectorTokenProvider&lt;ApplicationIdentityUser, string&gt;(provider.Create("Password Reset"));
          }
      }
  
  
      public static ApplicationUserManager Create(IdentityFactoryOptions&lt;ApplicationUserManager&gt; options, IOwinContext context)
      {
          if (options == null) throw new ArgumentNullException("options");
          if (context == null) throw new ArgumentNullException("context");
  
          try
          {
              _unitOfWork = ObjectFactory.GetInstance&lt;IUnitOfWork&gt;();
              var userRepository = ObjectFactory.GetInstance&lt;IRepository&lt;ApplicationIdentityUser&gt;&gt;();
  
              var manager = new ApplicationUserManager(new UserStore&lt;ApplicationIdentityUser&gt;(_unitOfWork.Session), userRepository);
  
              // Configure validation logic for usernames
              manager.UserValidator = new UserValidator&lt;ApplicationIdentityUser&gt;(manager)
              {
                  AllowOnlyAlphanumericUserNames = false,
                  RequireUniqueEmail = true
              };
  
              // Configure validation logic for passwords
              manager.PasswordValidator = new PasswordValidator
              {
                  RequiredLength = 6,
                  RequireNonLetterOrDigit = true,
                  RequireDigit = true,
                  RequireLowercase = true,
                  RequireUppercase = true,
              };
  
              // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
              // You can write your own provider and plug in here.
              manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider&lt;ApplicationIdentityUser&gt;
              {
                  MessageFormat = "Your security code is: {0}"
              });
  
              manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider&lt;ApplicationIdentityUser&gt;
              {
                  Subject = "Security Code",
                  BodyFormat = "Your security code is: {0}"
              });
  
              var dataProtectionProvider = options.DataProtectionProvider;
              if (dataProtectionProvider != null)
              {
                  manager.UserTokenProvider = new DataProtectorTokenProvider&lt;ApplicationIdentityUser&gt;(dataProtectionProvider.Create("ASP.NET Identity"));
              }
  
              return manager;
          }
          catch (Exception ex)
          {
              ex.Process(MethodBase.GetCurrentMethod().DeclaringType, MethodBase.GetCurrentMethod().Name);
  
              return null;
          }
      }      
  }
  </code></pre>
  
  <p>}</p>