0

I am trying to integrate Twilio with my VS2015 site, its clear to me that the example given on Twilio was for a straight MVC project and has an AccountController.cs where this code block goes (step 5/6) Twilio Two-Factor Authentication

//
    // POST: /Account/SendCode
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> SendCode(SendCodeViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View();
        }

        // Generate the token and send it
        if (!await SignInManager.SendTwoFactorCodeAsync(model.SelectedProvider))
        {
            return View("Error");
        }
        return RedirectToAction("VerifyCode",
                                new { Provider = model.SelectedProvider,
                                      ReturnUrl = model.ReturnUrl,
                                      RememberMe = model.RememberMe });
    }

My project has no such controllers and I'm getting an error with their implementation of this

private readonly ITwilioMessageSender _messageSender;

    public SmsService() : this(new TwilioMessageSender()) { }

    public SmsService(ITwilioMessageSender messageSender)
    {
        _messageSender = messageSender;
    }

CS0051 Inconsistent accessibility: parameter type 'ITwilioMessageSender' is less accessible than method 'SmsService.SmsService(ITwilioMessageSender)'

The error points to:

public SmsService(ITwilioMessageSender messageSender)
    {
        _messageSender = messageSender;
    }

In all, 1 error which I cannot manage to fix, and 1 issue that which I dont know where this step 5/6 code block needs to go. if thats a webform it needs to go in, it wasn't added in this project by default but I dont know. If someone understands this better I'd really appreciate some assistance.

Autonomic
  • 150
  • 4
  • 15
  • Jeremy, David from Twilio here. AccountController.cs is part of the sample application for that tutorial. You can download the entire application here: https://github.com/TwilioDevEd/sms2fa-csharp – dprothero Jul 24 '17 at 15:31
  • If all you really need is how to send an SMS, this bare-bones example will get you going: https://www.twilio.com/docs/libraries/csharp?code-sample=code-cnet-helper-library-async-example&code-language=cs&code-sdk-version=default I realize that sample is showing a Console program, but the same code will work in ASP.NET. You just want to use the Async version for best performance. – dprothero Jul 24 '17 at 15:36
  • Finally, for a truly hardened two-factor implementation, you may want to consider using Authy. Here's a tutorial: https://www.twilio.com/docs/tutorials/account-verification-csharp-mvc – dprothero Jul 24 '17 at 15:39
  • Hi David, the example Twilio provides is rather a one-off implementation and over-engineered in my opinion. It doesn't give confidence when the examples given throw errors (CS0051) nconsistent accessibility: which by that point I hadn't even run into the road block that forced me go a different direction. I did figure out how to finalize the implementation, but that (CS0051) error couldn't be remediated so thats an issue with the tutorial code. – Autonomic Jul 25 '17 at 02:10
  • You're absolutely right about the error not lending confidence. I went to try to correct the error but I do not receive the error when I clone the sample app and build using VS 2015 (14.0.25431.01 Update 3, .NET Framework 4.6.1). Any thoughts on how I might reproduce the issue? I'd like to make sure we nail down all the edge cases. – dprothero Jul 25 '17 at 15:16
  • Create a new default website with the Identity Model base. The first bit of code to add to IdentityModel.cs (my 2nd code block above), you'll need to create an interface and add the libraries, after that, you'll see the error. – Autonomic Jul 26 '17 at 00:29
  • The sample was intended to be cloned or downloaded rather than recreated. I can see we need to do a better job of communicating that on the doc. I imagine the issue you ran into was missing the `public` keyword on the interface declaration. – dprothero Jul 27 '17 at 04:04

1 Answers1

0

I ended up following this example, but you will need to download the older version of Twilio.4.7.2 as the newer version does not support client.SendMessage() method. And there is not a complete code example for VS2015 yet that works with all the new stuff. Honestly, I tried to make the newer version work but it wouldn't. This older version works for sure and I've recieved text messages from my site. Best practice, add your account info in your web.config

<appSettings>
   <add key="webpages:Version" value="3.0.0.0" />
   ...

   <add key="ACCOUNT_SID" value="<YOUR SID>" />
   <add key="AUTH_TOKEN" value="<YOUR LIVE TOKEN>"/>
  <add key="CLIENT_PHONE" value="<YOUR ACCNT PHONE>" />
</appSettings>

reference your account variables inside App_Start > IdentityConfig.cs. Find the class SmsService and under the Task SendAsynch

public class SmsService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // add your Twilio code here
        string ACCOUNT_SID = ConfigurationManager.AppSettings["ACCOUNT_SID"];
        string AUTH_TOKEN = ConfigurationManager.AppSettings["AUTH_TOKEN"];
        string CLIENT_PHONE = ConfigurationManager.AppSettings["CLIENT_PHONE"];

        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

        client.SendMessage(CLIENT_PHONE, message.Destination, message.Body);
        return Task.FromResult(0);
    }
}

Twillio gives you two tokens when you register... and doesn't tell you that one of them can only be used from within the Twilio Account REST API console. Trial accounts can only send SMS to the phone number you used to sign up. So if you're not getting an SMS to that phone number when you test, swap out your tokens.

This was tested with VS2015 Default website application with the Identity Model framework. I certainly hope this helps someone else so they don't need to spend 3 hrs wasting their time with Twilio and STFW.

Autonomic
  • 150
  • 4
  • 15