Skip to content

Commit

Permalink
Create README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
farhadzm authored Apr 11, 2024
1 parent 1f51b80 commit 0e14625
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
You can generate `Totp` code based on time, `UserSecurityStamp` and `UserIdentifier`
**Usage**
```CSharp
public async ValueTask<int> GetTwoFactorTotpCode(string email)
{
var user = await GetUserByEmail(email);

var modifier = GetModifier(user, TotpEnum.TwoFactor);

var totpCode = TotpService.GenerateCode(user.SecurityStamp.ToString(), modifier);

return totpCode;
}

public async ValueTask<bool> ValidateTwoFactorTotpCode(string email, int totpCode)
{
var user = await GetUserByEmail(email);

string modifier = GetModifier(user, TotpEnum.TwoFactor);

var isValid = TotpService.ValidateCode(
user.SecurityStamp.ToString(),
totpCode,
modifier,
_userOption.TwoFactorTotpExpiration);

return isValid;
}

private static string GetModifier(User user, TotpEnum totpEnum)
{
return $"Totp:{user.Id}:{totpEnum}";
}
```
TotpEnum:
```CSharp
public enum TotpEnum : byte
{
TwoFactor = 1,
Activation = 2,
Deactivation = 4
}

```

0 comments on commit 0e14625

Please sign in to comment.