public async Task <string> GenerateAsync(string purpose, UserManager <TUser, TKey> manager, TUser user)
        {
            var twoFactAuthManager = manager as IUserManagerSupportsTwoFactorAuthStore <TUser, TKey>;

            if (twoFactAuthManager == null)
            {
                throw new InvalidOperationException(Messages.IUserManagerSupportsTwoFactorAuthStoreNotImplemented);
            }
            if (!twoFactAuthManager.IsSupported())
            {
                throw new InvalidOperationException(Messages.ITwoFactorCodeStoreNotImplemented);
            }

            var stamp = await manager.GetSecurityStampAsync(user.Id);

            purpose += stamp;

            var bytes = Crypto.GenerateSaltInternal(sizeof(long));
            var val   = BitConverter.ToInt64(bytes, 0);
            var mod   = (int)Math.Pow(10, Digits);

            val %= mod;
            val  = Math.Abs(val);

            var code = val.ToString("D" + Digits);

            var hasher     = new AdaptivePasswordHasher(this.HashingIterations);
            var hashedCode = hasher.HashPassword(purpose + code);

            var data = new TwoFactorAuthData {
                HashedCode = hashedCode, DateIssued = UtcNow
            };
            await twoFactAuthManager.SetTwoFactorAuthDataAsync(user, data);

            await manager.UpdateAsync(user);

            return(code);
        }
Ejemplo n.º 2
0
        Task IUserManagerSupportsTwoFactorAuthStore <TUser, TKey> .SetTwoFactorAuthDataAsync(TUser user, TwoFactorAuthData data)
        {
            var store = this.Store as ITwoFactorCodeStore <TUser, TKey>;

            if (store == null)
            {
                throw new InvalidOperationException(Messages.ITwoFactorCodeStoreNotImplemented);
            }

            return(store.SetTwoFactorAuthDataAsync(user, data));
        }