加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – ASP.NET标识:在Azure网站上使用GeneratePasswor

发布时间:2020-12-16 06:50:28 所属栏目:asp.Net 来源:网络整理
导读:我在Microsoft Azure上部署了我的Web应用程序.但是,当我想生成一个PasswordResetToken时: var token = await _userManager.GeneratePasswordResetTokenAsync(user.Id); 我收到以下错误: System.Security.Cryptography.CryptographicException: The data pr
我在Microsoft Azure上部署了我的Web应用程序.但是,当我想生成一个PasswordResetToken时:

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.

如何让它在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;
        }
    }      
}

}

解决方法

我根据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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读