ASP.NET身份:在Azure网站上使用GeneratePasswordResetToken [英] ASP.NET Identity: use GeneratePasswordResetToken on Azure website

查看:319
本文介绍了ASP.NET身份:在Azure网站上使用GeneratePasswordResetToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的web应用程序部署在微软Azure。然而,当我要生成一个PasswordResetToken:

I have my web application deployed on Microsoft Azure. However when I want to generate a PasswordResetToken with:

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

我收到以下错误:

I get the following error:

System.Security.Cryptography.CryptographicException:数据保护操作是不成功的。这可能是由不具有加载的当前线程的用户上下文中的用户配置文件,当线程正在模拟其可能是这种情况造成的。

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.

我如何得到这个在Azure上工作?

How do I get this to work on Azure?

还是有一个其他的方式不知道旧密码重置密码?

Or is there an other way to reset a password without knowing the old password?

这是我的UserManager类。玛贝有一个错误在里面。

This is my UserManager class. Mabey there is an error in it.

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;
        }
    }      
}

}

推荐答案

我发现基于trailmax的答案我自己的问题的一个可行的解决方案。

I found a working solution for my own problem based on the answer of trailmax.

在EmailTokenProvider代替我用的是TotpSecurityStampBasedTokenProvider

In stead of the EmailTokenProvider I use the TotpSecurityStampBasedTokenProvider

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

有关TotpSecurityStampBasedTokenProvider的详细信息:
<一href=\"http://msdn.microsoft.com/en-us/library/dn613297(v=vs.108).aspx\">http://msdn.microsoft.com/en-us/library/dn613297(v=vs.108).aspx

For more information about TotpSecurityStampBasedTokenProvider: http://msdn.microsoft.com/en-us/library/dn613297(v=vs.108).aspx

这篇关于ASP.NET身份:在Azure网站上使用GeneratePasswordResetToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆