You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+8
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,11 @@
1
+
## 6.0.0 (2022-08-05)
2
+
- Big breaking change to the `[ValidateRequest]` attribute. The attribute no longer accepts parameters nor properties. Instead, you have to configure the request validation as documented in the readme.
3
+
- You can now add the Twilio REST client to ASP.NET Core's dependency injection container, using the `.AddTwilioClient` method. This Twilio client will use an `HttpClient` provided by an HTTP client factory. See readme for more details.
4
+
- We no longer try to match the Twilio SDK version number, and instead go by our own versioning to better communicate breaking changes vs minor changes.
5
+
- The projects are now built and packages are now pushed using GitHub Actions instead of AppVeyor.
6
+
- The projects are now built deterministically and support source link for better debugging.
7
+
- More samples have been added to the readme.
8
+
1
9
## 5.77.0 (2022-07-19)
2
10
- Twilio.AspNet.Core and Twilio.AspNet.Common now use .NET Standard 2.0 and dropped older .NET Standard versions.
3
11
- Microsoft.AspNetCore.Mvc.Core dependency has been updated to a version that is not vulnerable. For newer versions of .NET, a framework dependency is used instead.
Copy file name to clipboardExpand all lines: README.md
+240-14
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,7 @@
4
4
5
5
**The Twilio helper library for ASP.NET (Twilio.AspNet), helps you integrate the official [Twilio SDK for C# and .NET](https://github.com/twilio/twilio-csharp) into your ASP.NET applications.** The library supports ASP.NET MVC on .NET Framework and ASP.NET Core.
6
6
7
-
You only need this library if you wish to respond to Twilio webhooks for
8
-
voice calls and SMS messages. If you only need to use the Twilio REST API's,
9
-
then you only need the [Twilio SDK for C# and .NET](https://github.com/twilio/twilio-csharp).
7
+
This library helps you respond to webhooks, adds the Twilio client to the dependency injection container, and validate HTTP request originate from Twilio.
In traditional MVC controllers, the `SmsRequest`, `VoiceRequest`, and other typed request object would be bound, but Minimal APIs does not support the same model binding.
146
139
147
140
Instead, you can bind individual parameters for HTTP GET requests using the `FromQuery` attribute. When you don't specify the [FromQuery](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis#parameter-binding) attribute, multiple sources will be considered to bind from in addition to the query string parameters. For HTTP POST requests you can grab the form and then retrieve individual parameters by string index.
@@ -155,7 +148,8 @@ Here's the list of classes:
155
148
-`StatusCallbackRequest`: Holds data for tracking the status of an outbound Twilio Voice Call
156
149
-`VoiceRequest`: Holds data for incoming Voice Calls
157
150
158
-
Note: Only MVC Controllers and Razor Pages supports model binding to typed .NET objects. In Minimal APIs and other scenario's, you'll have to write code to extract the parameters yourself.
151
+
> **Note**
152
+
> Only MVC Controllers and Razor Pages support model binding to typed .NET objects. In Minimal APIs and other scenarios, you'll have to write code to extract the parameters yourself.
159
153
160
154
The following sample shows how to accept inbound SMS, respond, and track the status of the SMS response.
161
155
@@ -177,7 +171,7 @@ public class SmsController : TwilioController
177
171
{
178
172
varmessagingResponse=newMessagingResponse();
179
173
messagingResponse.Message(
180
-
body: $"Hey there {request.From}! How 'bout those Seahawks?",
174
+
body: $"Ahoy {request.From}!",
181
175
action: newUri("/Sms/StatusCallback"),
182
176
method: Twilio.Http.HttpMethod.Post
183
177
);
@@ -193,3 +187,235 @@ public class SmsController : TwilioController
193
187
194
188
As shown in the sample above, you can add an `SmsRequest` as a parameter, and MVC will bind the object for you.
195
189
The code then responds with an SMS with the `status` and `method` parameter. When the status of the SMS changes, Twilio will send an HTTP POST request to `StatusCallback` action. You can add an `SmsStatusCallbackRequest` as a parameter, and MVC will bind the object for you.
190
+
191
+
### Add the Twilio client to the ASP.NET Core dependency injection container
192
+
193
+
In ASP.NET Core, you can add the Twilio REST API client to ASP.NET Core's service using the `.AddTwilioClient` method, like this:
194
+
195
+
```csharp
196
+
usingTwilio.AspNet.Core;
197
+
198
+
varbuilder=WebApplication.CreateBuilder(args);
199
+
200
+
builder.Services.AddTwilioClient()
201
+
```
202
+
203
+
Now you can request `ITwilioRestClient` and `TwilioRestClient` via dependency injection.
204
+
205
+
You can configure the Twilio client using the following configuration:
-`Twilio:Client:AuthToken` falls back on `Twilio:AuthToken`. You only need to configure one of them.
227
+
-`Twilio:Client:CredentialType` has the following valid values: `Unspecified`, `AuthToken`, or `ApiKey`
228
+
-`Twilio:Client:CredentialType` is optional and defaults to `Unspecified`. If `Unspecified`, whether you configured an API key or an Auth Token will be detected.
229
+
230
+
If you do not wish to configure the Twilio client using .NET configuration, you can do so manually:
> Do not hard-code your **Auth Token** or **API key secret** into code and do not check them into source control.
253
+
> We recommend using the [Secrets Manager](https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets) for local development.
254
+
> Alternatively, you can use environment variables, a vault service, or other more secure techniques.
255
+
256
+
#### Use your own HTTP client
257
+
258
+
By default when you call `.AddTwilioClient`, an HTTP client factory is configured that is used to provide an `HttpClient` to the Twilio REST client. If you'd like to provide your own HTTP client, you can do so by providing a callback like this:
Webhooks require your endpoint to be publicly available, but this also introduces the risk that bad actors could find your webhook URL and try to abuse it.
272
+
273
+
Luckily, you can verify that an HTTP request originated from Twilio.
274
+
The `Twilio.AspNet` library provides an attribute that will validate the request for you in MVC.
275
+
The implementation differs between the `Twilio.AspNet.Core` and `Twilio.AspNet.Mvc` library.
276
+
277
+
#### Validate requests in ASP.NET Core MVC
278
+
279
+
Add the `.AddTwilioRequestValidation` method at startup:
280
+
281
+
```csharp
282
+
usingTwilio.AspNet.Core;
283
+
284
+
varbuilder=WebApplication.CreateBuilder(args);
285
+
286
+
builder.Services.AddTwilioRequestValidation();
287
+
```
288
+
289
+
Then configure the request validation:
290
+
291
+
```json
292
+
{
293
+
"Twilio": {
294
+
"AuthToken": "[YOUR_AUTH_TOKEN]",
295
+
"RequestValidation": {
296
+
"AuthToken": "[YOUR_AUTH_TOKEN]",
297
+
"AllowLocal": true,
298
+
"BaseUrlOverride": "https://??????.ngrok.io"
299
+
}
300
+
}
301
+
}
302
+
```
303
+
304
+
A couple of notes about the configuration:
305
+
-`Twilio:RequestValidation:AuthToken` falls back on `Twilio:AuthToken`. You only need to configure one of them.
306
+
-`AllowLocal` will skip validation when the HTTP request originated from localhost.
307
+
- Use `BaseUrlOverride` in case your app is behind a reverse proxy or a tunnel like ngrok. The path of the current request will be appended to the `BaseUrlOverride` for request validation.
308
+
309
+
You can also manually configure the request validation:
If you configure request validation using both ways, app setting will overwrite the `twilio/requestValidation` configuration element.
387
+
388
+
A couple of notes about the configuration:
389
+
-`allowLocal` will skip validation when the HTTP request originated from localhost.
390
+
- Use `baseUrlOverride` in case you are in front of a reverse proxy or a tunnel like ngrok. The path of the current request will be appended to the `baseUrlOverride` for request validation.
391
+
392
+
> **Warning**
393
+
> Do not hard-code your **Auth Token** into code and do not check them into source control.
394
+
> Use the `UserSecretsConfigBuilder` for local development or [one of the other configuration builders](https://docs.microsoft.com/en-us/aspnet/config-builder).
395
+
> Alternatively, you should encrypt the configuration sections containing secrets like the Auth Token.
396
+
397
+
Now that request validation has been configured, use the `[ValidateRequest]` attribute.
398
+
You can apply the attribute globally, to MVC areas, controllers, and actions.
399
+
Here's an example where the attribute is applied to the `Index` action:
400
+
401
+
```csharp
402
+
usingTwilio.AspNet.Common;
403
+
usingTwilio.AspNet.Mvc;
404
+
usingTwilio.TwiML;
405
+
406
+
publicclassSmsController : TwilioController
407
+
{
408
+
[ValidateRequest]
409
+
publicTwiMLResultIndex(SmsRequestrequest)
410
+
{
411
+
varresponse=newMessagingResponse();
412
+
response.Message("Ahoy!");
413
+
returnTwiML(response);
414
+
}
415
+
}
416
+
```
417
+
418
+
#### Validate requests outside of MVC
419
+
420
+
The `[ValidateRequest]` attribute only works for MVC. If you need to validate requests outside of MVC, you can use the `RequestValidationHelper` class provided by `Twilio.AspNet`.
421
+
Alternatively, the `RequestValidator` class from the [Twilio SDK](https://github.com/twilio/twilio-csharp) can also help you with this.
0 commit comments