Sam
Sam

Reputation: 10113

How to set up two-factor authentication with ASP.net Identity 2.0?

So I must be missing something super simple or I'm not fully understanding how two-factor authentication is supposed to work for ASP.net Identity 2.0.

My understanding is that two-factor authentication is supposed to work like GoDaddy or Google; when you attempt to log in from a computer without a valid second factor cookie, an email or SMS is sent with an auth code and you are presented with a second form to enter your auth code in order to complete the sign in process.

All of the code appears to be present in a new MVC 5 project, except 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("[email protected]", message.Destination, message.Subject, message.Body);
        smtpClient.Send(mail);
        return Task.FromResult(0);
    }
}

However, when I log in, no email is sent and no auth code form is displayed.

I went in to the Manage View (Views => Manage => Index) and uncommented the TwoFactor section. I logged back in, went to the manage screen, and enabled two-factor authentication for the account, but it didn't make a bit of difference.

Thoughts on what am I missing?


Edit

Ok, so it appears the crux of my problem may be related to registration confirmation. The two factor authentication only appears to work when the email has been confirmed. Otherwise the code does not send. So you either need to enable the email confirmation in the Registration, or set EmailConfirmed = true when you register the user.

Upvotes: 3

Views: 1741

Answers (1)

Sam
Sam

Reputation: 10113

The issue was that the email has to be marked as confirmed for two-factor authentication to work.

Upvotes: 1

Related Questions