I'm creating an application in ASP.Net core MVC that requires user registration and verifying with an OTP by only entering their mobile phones.
I'm currently using Twilio to send SMS (do not want to use Twilio Verify only Twilio SMS) and I'm creating my own OTP by randomly generating 4 digits.
So to my question: How do I use this 4 digit OTP that I get on my phone and register the mobile phone and make sure the user is logged in?
I do not want to implement 2FA at the moment because it requires username and password.
This question is similar to: 'https://stackoverflow.com/questions/43862276/register-with-phone-number-instead-of-email-using-mvc-identity' But it has gone 4 years with no answer...
I appreciate any help, tips or/and further resources, thanks in advance!
OTP and User Login/Registration are two separate process. It is the developers who decide how OTP and Login/Registration will be connected. I have implemented this requirement couple of months ago. Here is how you can do this:
When you have the user who has successfully verified an OTP, use the User ID to authenticate the user. Here is how you can implement JWT:
private string GenerateJSONWebToken(UserModel userInfo)
{
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(_config["Jwt:Issuer"],
_config["Jwt:Issuer"],
null,
expires: DateTime.Now.AddMinutes(120),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
}
That will generate a token with 120 minute validity like this:
{
"token" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJKaWduZXNoIFRyaXZlZGkiLCJlbWFpbCI6InRlc3QuYnRlc3RAZ21haWwuY29tIiwiRGF0ZU9mSm9pbmciOiIwMDAxLTAxLTAxIiwianRpIjoiYzJkNTZjNzQtZTc3Yy00ZmUxLTgyYzAtMzlhYjhmNzFmYzUzIiwiZXhwIjoxNTMyMzU2NjY5LCJpc3MiOiJUZXN0LmNvbSIsImF1ZCI6IlRlc3QuY29tIn0.8hwQ3H9V8mdNYrFZSjbCpWSyR1CNyDYHcGf6GqqCGnY"
}
Send the JSON token to the application/mobile app. As long as the application/mobile app has the token and sends it along the requests (in header) the app and user is authenticated. You have to check the token and its validity against a Database table. Here is a complete implementation of the JWT part.