'OWIN 2FA Sign In Succeeded by User not Authenticated
I'm trying to implement a custom Two Factor Sign in for a private website. (Previously was set for specific IPs, but client wants to view whenever and whatever device).
So, I've created a custom SMS & Email Provider, Token Provider etc - All that appears to work fine. The Token is saved in the DB (UserTokens) and I can call "TwoFactorSignInAsync" successfully and the result == "Succeeded". Now here lies my problem, desire the user being logged in. Subsequent requests do not show the user as authenticated, the "User.Identity.IsAuthenticated" property is always false. I feel like I must have missed a step but can't see to get around it.
Added into my Identity in start up
.AddTokenProvider<TwoFactorTokenProvider<UserEntity>>("OGSecurityCode");
My Class
public class TwoFactorTokenProvider<UserEntity> : DataProtectorTokenProvider<UserEntity> where UserEntity : class
{
public TwoFactorTokenProvider(IDataProtectionProvider dataProtectionProvider,
IOptions<TwoFactorTokenProviderOptions> options,
ILogger<DataProtectorTokenProvider<UserEntity>> logger)
: base(dataProtectionProvider, options, logger)
{
base.Options.Name = "OGSecurityCode";
}
public override Task<string> GenerateAsync(string purpose, UserManager<UserEntity> manager, UserEntity user)
{
Task<string> t = (Task<string>)Task.Run(async () =>
{
Random generator = new Random();
string theCode = generator.Next(100000, 999999).ToString("D6");
await manager.SetAuthenticationTokenAsync(user, manager.Options.Tokens.AuthenticatorTokenProvider, purpose, theCode);
return theCode;
});
return t;
}
public override Task<bool> CanGenerateTwoFactorTokenAsync(UserManager<UserEntity> manager, UserEntity user)
{
return Task.Run<bool>(() => { return true; });
}
public override async Task<bool> ValidateAsync(string purpose, string token, UserManager<UserEntity> manager, UserEntity user)
{
string theCode = await manager.GetAuthenticationTokenAsync(user, manager.Options.Tokens.AuthenticatorTokenProvider, purpose);
return (theCode.Equals(token, StringComparison.OrdinalIgnoreCase));
}
}
Generating code
securityKey = await _userManager.GenerateTwoFactorTokenAsync(user, "OGSecurityCode");
Sign in with code
var tfresult = await _signInManager.TwoFactorSignInAsync("OGSecurityCode", securityKey, model.RememberMe, model.RememberMachine);
if (tfresult.Succeeded)
{
//Gets here successfully }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
