microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
kavin/conversation-fix

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Apps/TeamsBotApplication.HostingExtensions.cs

107lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using Microsoft.AspNetCore.Routing;
5using Microsoft.Extensions.Configuration;
6using Microsoft.Extensions.DependencyInjection;
7using Microsoft.Extensions.Logging;
8using Microsoft.Extensions.Options;
9using Microsoft.Identity.Abstractions;
10using Microsoft.Teams.Bot.Core.Hosting;
11
12namespace Microsoft.Teams.Bot.Apps;
13
14/// <summary>
15/// Extension methods for <see cref="TeamsBotApplication"/>.
16/// </summary>
17public static class TeamsBotApplicationHostingExtensions
18{
19 /// <summary>
20 /// Registers Teams bot application services with the specified service collection.
21 /// </summary>
22 /// <remarks>This method provides a simplified way to configure Teams bot support by encapsulating the
23 /// necessary service registrations and configuration binding.</remarks>
24 /// <param name="services">The service collection to which Teams bot application services will be added. Cannot be null.</param>
25 /// <param name="sectionName">The name of the configuration section containing Azure Active Directory settings. Defaults to "AzureAd" if not
26 /// specified.</param>
27 /// <returns>The service collection with Teams bot application services registered.</returns>
28 public static IServiceCollection AddTeams(this IServiceCollection services, string sectionName = "AzureAd")
29 => AddTeamsBotApplication(services, sectionName);
30
31 /// <summary>
32 /// Adds the Default TeamsBotApplication
33 /// </summary>
34 /// <param name="services"></param>
35 /// <param name="sectionName"></param>
36 /// <returns></returns>
37 public static IServiceCollection AddTeamsBotApplication(this IServiceCollection services, string sectionName = "AzureAd")
38 {
39 return AddTeamsBotApplication<TeamsBotApplication>(services, sectionName);
40 }
41
42 /// <summary>
43 /// Adds a custom TeamsBotApplication
44 /// </summary>
45 /// <param name="services">The WebApplicationBuilder instance.</param>
46 /// <param name="sectionName">The configuration section name for AzureAd settings. Default is "AzureAd".</param>
47 /// <returns>The updated WebApplicationBuilder instance.</returns>
48 public static IServiceCollection AddTeamsBotApplication<TApp>(this IServiceCollection services, string sectionName = "AzureAd") where TApp : TeamsBotApplication
49 {
50 // Register options to defer configuration reading until ServiceProvider is built
51 services.AddOptions<BotClientOptions>()
52 .Configure<IConfiguration>((options, configuration) =>
53 {
54 options.Scope = "https://api.botframework.com/.default";
55 if (!string.IsNullOrEmpty(configuration[$"{sectionName}:Scope"]))
56 options.Scope = configuration[$"{sectionName}:Scope"]!;
57 if (!string.IsNullOrEmpty(configuration["Scope"]))
58 options.Scope = configuration["Scope"]!;
59 options.SectionName = sectionName;
60 });
61
62 services.AddHttpClient<TeamsApiClient>(TeamsApiClient.TeamsHttpClientName)
63 .AddHttpMessageHandler(sp =>
64 {
65 BotClientOptions options = sp.GetRequiredService<IOptions<BotClientOptions>>().Value;
66 return new BotAuthenticationHandler(
67 sp.GetRequiredService<IAuthorizationHeaderProvider>(),
68 sp.GetRequiredService<ILogger<BotAuthenticationHandler>>(),
69 options.Scope,
70 sp.GetService<IOptions<ManagedIdentityOptions>>());
71 });
72
73 services.AddBotApplication<TApp>();
74 return services;
75 }
76
77 /// <summary>
78 /// Configures the TeamsBotApp
79 /// </summary>
80 /// <typeparam name="TApp"></typeparam>
81 /// <param name="endpoints"></param>
82 /// <param name="routePath"></param>
83 /// <returns></returns>
84 public static TApp UseTeamsBotApplication<TApp>(this IEndpointRouteBuilder endpoints,
85 string routePath = "api/messages")
86 where TApp : TeamsBotApplication
87 => endpoints.UseBotApplication<TApp>(routePath);
88
89 /// <summary>
90 /// Configures the default TeamsBotApplication
91 /// </summary>
92 /// <param name="endpoints"></param>
93 /// <param name="routePath"></param>
94 /// <returns></returns>
95 public static TeamsBotApplication UseTeamsBotApplication(this IEndpointRouteBuilder endpoints,
96 string routePath = "api/messages")
97 => endpoints.UseBotApplication<TeamsBotApplication>(routePath);
98
99 /// <summary>
100 /// Alias for backward compat
101 /// </summary>
102 /// <param name="endpoints"></param>
103 /// <param name="routePath"></param>
104 /// <returns></returns>
105 public static TeamsBotApplication UseTeams(this IEndpointRouteBuilder endpoints,string routePath = "api/messages")
106 => endpoints.UseBotApplication<TeamsBotApplication>(routePath);
107}
108