In Part 3 of this tutorial series, we will create an SMS provider for Microsoft Identity that comes shipped with in the .NET framework. It will take about 15 minutes.

Prerequisites Prerequisites

Working knowledge of C# and REST APIs Visual Studio 2013 or later A Sinch account

Setup Setup

Create a new a project and name it LoginSample Select MVC project with basic authentication



Enable SMS for two-factor authentication with Sinch Enable SMS for two-factor authentication with Sinch

In the package manager console, enter: Install-Package Sinch.SMS Open IdentityConfig.cs in the App_Start folder and find the SMSService class. Add the Sinch implementation to it like this:

public Task SendAsync(IdentityMessage message) { Sinch.SMS.Client client = new Client("key", "secret"); return client.SendSMS(message.Destination, message.Body); }

Remember to replace the key and secret with your own info from the dashboard.

Update the profile page to show number

In a production scenario, you would likely ask for a phone number during the registration process. In this case, we will just add it to the profile page.

Open Views:raw-latex:`Manage`:raw-latex:`Index`.cshtml and find the PhoneNumber section. Uncomment it:

<dt>Phone Number:</dt> <dd> @(Model.PhoneNumber ?? "None") [ @if (Model.PhoneNumber != null) { @Html.ActionLink("Change", "AddPhoneNumber") @: | @Html.ActionLink("Remove", "RemovePhoneNumber") } else { @Html.ActionLink("Add", "AddPhoneNumber") } ] </dd>

Also uncomment the two-factor authentication part:

@if (Model.TwoFactor) { using (Html.BeginForm("DisableTwoFactorAuthentication", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() <text>Enabled <input type="submit" value="Disable" class="btn btn-link" /> </text> } } else { using (Html.BeginForm("EnableTwoFactorAuthentication", "Manage", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() <text>Disabled <input type="submit" value="Enable" class="btn btn-link" /> </text> } }

Testing the app Testing the app

Hit F5 and run the app. If you haven’t already registered an account, click on the username in the top right corner. This will take you to the page where you can manage your profile.

Click Add Phone Number; remember to enter it in the international format (i.e 1 5612600684) Enter the code you received in an SMS Click enable two-factor authentication Log off and log back in; you should now see it below:



Click Next and enter the code in the following window.



Now you see how easy it is to enable two-factor authentication on an ASP.NET Identity application using Sinch.

Edit on GitHub