ASP.NET标识:在Azure网站上使用GeneratePasswordResetToken

时间:2014-09-09 08:41:17

标签: asp.net-mvc azure asp.net-identity forgot-password

我在Microsoft Azure上部署了我的Web应用程序。但是当我想用:

生成一个PasswordResetToken时
var token = await _userManager.GeneratePasswordResetTokenAsync(user.Id);

我收到以下错误:

  

System.Security.Cryptography.CryptographicException:数据保护操作失败。这可能是由于没有为当前线程的用户上下文加载用户配置文件引起的,这可能是线程模拟时的情况。

如何让它在Azure上运行?

或者是否有其他方法可以在不知道旧密码的情况下重置密码?

这是我的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;
        }
    }      
}

}

2 个答案:

答案 0 :(得分:5)

我根据trailmax的答案为我自己的问题找到了一个有效的解决方案。

我使用TotpSecurityStampBasedTokenProvider代替EmailTokenProvider

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

有关TotpSecurityStampBasedTokenProvider的更多信息: http://msdn.microsoft.com/en-us/library/dn613297(v=vs.108).aspx

答案 1 :(得分:1)

在UserManager中使用EmailTokenProvider

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

blogged about it recently