TrustServiceUrlAttribute.cs
1 using System.Collections.Generic;
2 using System.Linq;
3 using System.Threading.Tasks;
4 
5 using Microsoft.AspNetCore.Mvc.Filters;
6 using Newtonsoft.Json.Linq;
7 
8 namespace Microsoft.Bot.Connector
9 {
10  public class TrustServiceUrlAttribute : ActionFilterAttribute
11  {
12  public async override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
13  {
14  var activities = GetActivities(context);
15 
16  foreach (var activity in activities)
17  {
18  MicrosoftAppCredentials.TrustServiceUrl(activity.ServiceUrl);
19  }
20  await next();
21  }
22 
23  public static IList<Activity> GetActivities(ActionExecutingContext actionContext)
24  {
25  var activties = actionContext.ActionArguments.Select(t => t.Value).OfType<Activity>().ToList();
26  if (activties.Any())
27  {
28  return activties;
29  }
30  else
31  {
32  var objects =
33  actionContext.ActionArguments.Where(t => t.Value is JObject || t.Value is JArray)
34  .Select(t => t.Value).ToArray();
35  if (objects.Any())
36  {
37  activties = new List<Activity>();
38  foreach (var obj in objects)
39  {
40  activties.AddRange((obj is JObject) ? new Activity[] { ((JObject)obj).ToObject<Activity>() } : ((JArray)obj).ToObject<Activity[]>());
41  }
42  }
43  }
44  return activties;
45  }
46 
47  }
48 }
async override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
static IList< Activity > GetActivities(ActionExecutingContext actionContext)
static void TrustServiceUrl(string serviceUrl, DateTime expirationTime=default(DateTime))
Adds the host of service url to MicrosoftAppCredentials trusted hosts.
An Activity is the basic communication type for the Bot Framework 3.0 protocol
Definition: ActivityEx.cs:18