2014-09-09 1 views
4

웹 응용 프로그램을 Microsoft Azure에 배포했습니다. 그러나 내가 가진 PasswordResetToken를 생성 할 때ASP.NET ID : Azure 웹 사이트에서 GeneratePasswordResetToken 사용

var token = await _userManager.GeneratePasswordResetTokenAsync(user.Id); 

나는 다음과 같은 오류 얻을 :

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.

가 어떻게이 푸른에 일을합니까를?

이전 비밀번호를 모른 채 비밀번호를 재설정 할 수있는 다른 방법이 있습니까?

내 UserManager 클래스입니다. Mabey 거기에 오류가 있습니다.

public class ApplicationUserManager : UserManager<ApplicationIdentityUser> 
{ 
    private static IUnitOfWork _unitOfWork; 
    private readonly IRepository<ApplicationIdentityUser> _userRepository; 


    public ApplicationUserManager(IUserStore<ApplicationIdentityUser> store, IRepository<ApplicationIdentityUser> userRepository) 
     : base(store) 
    { 
     if (userRepository == null) throw new ArgumentNullException("userRepository"); 

     _userRepository = userRepository; 

     if (bool.Parse(ConfigurationManager.AppSettings["RunningInAzure"])) 
      UserTokenProvider = new EmailTokenProvider<ApplicationIdentityUser, string>(); 
     else 
     { 
      var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("TopRijden"); 
      UserTokenProvider = new DataProtectorTokenProvider<ApplicationIdentityUser, string>(provider.Create("Password Reset")); 
     } 
    } 


    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     if (options == null) throw new ArgumentNullException("options"); 
     if (context == null) throw new ArgumentNullException("context"); 

     try 
     { 
      _unitOfWork = ObjectFactory.GetInstance<IUnitOfWork>(); 
      var userRepository = ObjectFactory.GetInstance<IRepository<ApplicationIdentityUser>>(); 

      var manager = new ApplicationUserManager(new UserStore<ApplicationIdentityUser>(_unitOfWork.Session), userRepository); 

      // Configure validation logic for usernames 
      manager.UserValidator = new UserValidator<ApplicationIdentityUser>(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<ApplicationIdentityUser> 
      { 
       MessageFormat = "Your security code is: {0}" 
      }); 

      manager.RegisterTwoFactorProvider("EmailCode", new EmailTokenProvider<ApplicationIdentityUser> 
      { 
       Subject = "Security Code", 
       BodyFormat = "Your security code is: {0}" 
      }); 

      var dataProtectionProvider = options.DataProtectionProvider; 
      if (dataProtectionProvider != null) 
      { 
       manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationIdentityUser>(dataProtectionProvider.Create("ASP.NET Identity")); 
      } 

      return manager; 
     } 
     catch (Exception ex) 
     { 
      ex.Process(MethodBase.GetCurrentMethod().DeclaringType, MethodBase.GetCurrentMethod().Name); 

      return null; 
     } 
    }  
} 

}

+0

당신은 이런 일에 대해 이야기하고 있는가? http://www.asp.net/identity/overview/features-api/account-confirmation-and-password-recovery-with-aspnet-identity – jbutler483

답변

1

사용 EmailTokenProvider 내가 blogged about it recently을했습니다

public UserManager() : base(new UserStore<ApplicationUser>(new MyDbContext())) 
{ 
    // other setup 
    this.UserTokenProvider = new EmailTokenProvider<ApplicationUser, string>(); 
} 

UserManager

있다.

+0

불행히도 작동하지 않습니다. 내 UserManager 클래스로 내 질문을 편집했습니다. Mabey에 오류가 있습니다. – DeVliegendeBeer

+0

EmailTokenProvider를 사용할 때 다음 오류도 발생합니다. System.NotSupportedException : No IUserTokenProvider가 등록되었습니다. – DeVliegendeBeer

+0

토큰 제공 업체를 등록 할 수있는 옵션이 너무 많다고 생각합니다. 사용자 관리자 생성을 단순화하고 단 2FA 제공자가 등록되어 있는지 확인하고 'UserTokenProvider'를 설정할 위치를 하나만 지정하십시오. 현재 이메일 토큰 제공자를 생성자로 설정하더라도 static'Create' 메소드로 덮어 쓸 수 있습니다. – trailmax

5

trailmax의 답을 바탕으로 내 자신의 문제에 대한 해결책을 찾았습니다. EmailTokenProvider 대신에

나는 TotpSecurityStampBasedTokenProvider에 대한 자세한 내용은 TotpSecurityStampBasedTokenProvider

public UserManager() : base(new UserStore<ApplicationUser>(new MyDbContext())) 
{ 
    // other setup 
    this.UserTokenProvider = new TotpSecurityStampBasedTokenProvider<ApplicationUser, string>(); 
} 

를 사용 http://msdn.microsoft.com/en-us/library/dn613297(v=vs.108).aspx

+0

@DeVliegendeBeer : 가능합니다. 대답을 대답으로 표시하십시오. –