How to set up two factor authentication with ASP.net Identity 2.0?
So, I must be missing something super simple, or I don't quite understand how two factor authentication is supposed to work for ASP.net Identity 2.0.
I understand that 2FA should work like GoDaddy or Google; When you try to log in from a computer without a valid second factor cookie, an email or SMS is sent with an authentication code and you are presented with a second form to enter your authentication code to complete the login process.
All the code seems to be present in the new MVC 5 project, except that I had to implement the SendAsync function for the email service:
public class EmailService : IIdentityMessageService
{
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
SmtpClient smtpClient = new SmtpClient("127.0.0.1", 25);
MailMessage mail = new MailMessage("sender@domain.com", message.Destination, message.Subject, message.Body);
smtpClient.Send(mail);
return Task.FromResult(0);
}
}
However, when I login, no email is sent and the login form is not displayed.
I went into Manage View (Views => Manage => Index) and uncommented the TwoFactor section. I logged in, went to the control screen and turned on two-factor authentication for the account, but it didn't make any difference.
Thoughts on what I'm missing?
Edit
So it looks like my problem is with registration confirmation. Only two-factor authentication is checked, only when the email is confirmed. Otherwise, no code is sent. So you need to enable email confirmation in registration or set EmailConfirmed = true
when user register.
source to share