diff --git a/Libraries/Microsoft.Teams.Api/Auth/ClientCredentials.cs b/Libraries/Microsoft.Teams.Api/Auth/ClientCredentials.cs index 7ae6d974..89f1d42d 100644 --- a/Libraries/Microsoft.Teams.Api/Auth/ClientCredentials.cs +++ b/Libraries/Microsoft.Teams.Api/Auth/ClientCredentials.cs @@ -1,46 +1,24 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using Microsoft.Identity.Abstractions; using Microsoft.Teams.Common.Http; namespace Microsoft.Teams.Api.Auth; -public class ClientCredentials : IHttpCredentials +public class ClientCredentials(IAuthorizationHeaderProvider authorizationHeaderProvider) : IHttpCredentials { - public string ClientId { get; set; } - public string ClientSecret { get; set; } - public string? TenantId { get; set; } - - public ClientCredentials(string clientId, string clientSecret) - { - ClientId = clientId; - ClientSecret = clientSecret; - } - - public ClientCredentials(string clientId, string clientSecret, string? tenantId) - { - ClientId = clientId; - ClientSecret = clientSecret; - TenantId = tenantId; - } - public async Task Resolve(IHttpClient client, string[] scopes, CancellationToken cancellationToken = default) { - var tenantId = TenantId ?? "botframework.com"; - var request = HttpRequest.Post( - $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token" - ); - - request.Headers.Add("Content-Type", ["application/x-www-form-urlencoded"]); - request.Body = new Dictionary() + AuthorizationHeaderProviderOptions options = new(); + options.AcquireTokenOptions = new AcquireTokenOptions() { - { "grant_type", "client_credentials" }, - { "client_id", ClientId }, - { "client_secret", ClientSecret }, - { "scope", string.Join(",", scopes) } }; - - var res = await client.SendAsync(request, cancellationToken); - return res.Body; + var tokenResult = await authorizationHeaderProvider.CreateAuthorizationHeaderForAppAsync(scopes[0], options, cancellationToken); + return new TokenResponse + { + AccessToken = tokenResult.Substring("Bearer ".Length), + TokenType = "Bearer", + }; } } \ No newline at end of file diff --git a/Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs b/Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs index 1e68b1a5..09116d95 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/ActivityClient.cs @@ -6,6 +6,8 @@ using Microsoft.Teams.Api.Activities; using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; + namespace Microsoft.Teams.Api.Clients; public class ActivityClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/ApiClient.cs b/Libraries/Microsoft.Teams.Api/Clients/ApiClient.cs index f4aeae83..32254926 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/ApiClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/ApiClient.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; diff --git a/Libraries/Microsoft.Teams.Api/Clients/BotClient.cs b/Libraries/Microsoft.Teams.Api/Clients/BotClient.cs index ea7fda43..7d0830ad 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/BotClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/BotClient.cs @@ -3,6 +3,7 @@ using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class BotClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/BotSignInClient.cs b/Libraries/Microsoft.Teams.Api/Clients/BotSignInClient.cs index 52217361..0a3a94ea 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/BotSignInClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/BotSignInClient.cs @@ -3,6 +3,7 @@ using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class BotSignInClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/BotTokenClient.cs b/Libraries/Microsoft.Teams.Api/Clients/BotTokenClient.cs index 8255d89c..c9b7b39a 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/BotTokenClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/BotTokenClient.cs @@ -3,6 +3,7 @@ using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class BotTokenClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/Client.cs b/Libraries/Microsoft.Teams.Api/Clients/Client.cs index 4c145577..48616854 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/Client.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/Client.cs @@ -3,6 +3,7 @@ using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public abstract class Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/ConversationClient.cs b/Libraries/Microsoft.Teams.Api/Clients/ConversationClient.cs index 8523faae..d23b0ad7 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/ConversationClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/ConversationClient.cs @@ -6,6 +6,7 @@ using Microsoft.Teams.Api.Activities; using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class ConversationClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/MeetingClient.cs b/Libraries/Microsoft.Teams.Api/Clients/MeetingClient.cs index 9c8ef2da..807a98a4 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/MeetingClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/MeetingClient.cs @@ -6,6 +6,7 @@ using Microsoft.Teams.Api.Meetings; using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class MeetingClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/MemberClient.cs b/Libraries/Microsoft.Teams.Api/Clients/MemberClient.cs index d69c96a2..f65d06d2 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/MemberClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/MemberClient.cs @@ -3,6 +3,7 @@ using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class MemberClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/TeamClient.cs b/Libraries/Microsoft.Teams.Api/Clients/TeamClient.cs index 4ee05622..f865d650 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/TeamClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/TeamClient.cs @@ -3,6 +3,7 @@ using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class TeamClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/UserClient.cs b/Libraries/Microsoft.Teams.Api/Clients/UserClient.cs index caf49aab..ce4ff094 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/UserClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/UserClient.cs @@ -3,6 +3,7 @@ using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class UserClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Clients/UserTokenClient.cs b/Libraries/Microsoft.Teams.Api/Clients/UserTokenClient.cs index cf264d6a..321c69a0 100644 --- a/Libraries/Microsoft.Teams.Api/Clients/UserTokenClient.cs +++ b/Libraries/Microsoft.Teams.Api/Clients/UserTokenClient.cs @@ -6,6 +6,7 @@ using Microsoft.Teams.Common.Http; +using IHttpClientFactory = Microsoft.Teams.Common.Http.IHttpClientFactory; namespace Microsoft.Teams.Api.Clients; public class UserTokenClient : Client diff --git a/Libraries/Microsoft.Teams.Api/Microsoft.Teams.Api.csproj b/Libraries/Microsoft.Teams.Api/Microsoft.Teams.Api.csproj index 06bda169..fd61234d 100644 --- a/Libraries/Microsoft.Teams.Api/Microsoft.Teams.Api.csproj +++ b/Libraries/Microsoft.Teams.Api/Microsoft.Teams.Api.csproj @@ -1,4 +1,4 @@ - + @@ -23,7 +23,8 @@ - + + diff --git a/Libraries/Microsoft.Teams.Apps/App.cs b/Libraries/Microsoft.Teams.Apps/App.cs index c1943590..979541d6 100644 --- a/Libraries/Microsoft.Teams.Apps/App.cs +++ b/Libraries/Microsoft.Teams.Apps/App.cs @@ -52,11 +52,14 @@ internal string UserAgent } } - public App(AppOptions? options = null) + internal App() : this(null!, null) + { } + + public App(IHttpCredentials credentials, AppOptions? options = null) { Logger = options?.Logger ?? new ConsoleLogger(); Storage = options?.Storage ?? new LocalStorage(); - Credentials = options?.Credentials; + Credentials = credentials; Plugins = options?.Plugins ?? []; OAuth = options?.OAuth ?? new OAuthSettings(); Provider = options?.Provider; diff --git a/Libraries/Microsoft.Teams.Apps/AppBuilder.cs b/Libraries/Microsoft.Teams.Apps/AppBuilder.cs index 63e5d6f5..60f46f1f 100644 --- a/Libraries/Microsoft.Teams.Apps/AppBuilder.cs +++ b/Libraries/Microsoft.Teams.Apps/AppBuilder.cs @@ -1,16 +1,26 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using Microsoft.Extensions.DependencyInjection; using Microsoft.Teams.Apps.Plugins; +using Microsoft.Teams.Common.Http; namespace Microsoft.Teams.Apps; public partial class AppBuilder { + private readonly IServiceProvider _serviceProvider; protected AppOptions _options; + public AppBuilder(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + _options = serviceProvider.GetService(typeof(AppOptions)) as AppOptions ?? throw new InvalidOperationException("AppOptions not found in DI container"); + } + public AppBuilder(AppOptions? options = null) { + _serviceProvider = null!; _options = options ?? new AppOptions(); } @@ -100,6 +110,6 @@ public AppBuilder AddOAuth(string defaultConnectionName) public App Build() { - return new App(_options); + return new App(_serviceProvider.GetService()!, _options); } } \ No newline at end of file diff --git a/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Configuration/Microsoft.Teams.Apps.Extensions/TeamsSettings.cs b/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Configuration/Microsoft.Teams.Apps.Extensions/TeamsSettings.cs index 11a4e532..ca230139 100644 --- a/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Configuration/Microsoft.Teams.Apps.Extensions/TeamsSettings.cs +++ b/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Configuration/Microsoft.Teams.Apps.Extensions/TeamsSettings.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -using Microsoft.Teams.Api.Auth; +// using Microsoft.Teams.Api.Auth; namespace Microsoft.Teams.Apps.Extensions; @@ -20,10 +20,10 @@ public AppOptions Apply(AppOptions? options = null) { options ??= new AppOptions(); - if (ClientId is not null && ClientSecret is not null && !Empty) - { - options.Credentials = new ClientCredentials(ClientId, ClientSecret, TenantId); - } + //if (ClientId is not null && ClientSecret is not null && !Empty) + //{ + // options.Credentials = new ClientCredentials(ClientId, ClientSecret, TenantId); + //} return options; } diff --git a/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/HostApplicationBuilder.cs b/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/HostApplicationBuilder.cs index 01f28920..164abb31 100644 --- a/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/HostApplicationBuilder.cs +++ b/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/HostApplicationBuilder.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Teams.Api.Auth; +// using Microsoft.Teams.Api.Auth; using Microsoft.Teams.Apps.Plugins; using Microsoft.Teams.Common.Logging; using Microsoft.Teams.Extensions.Logging; @@ -32,22 +32,22 @@ public static IHostApplicationBuilder AddTeamsCore(this IHostApplicationBuilder var loggingSettings = builder.Configuration.GetTeamsLogging(); // client credentials - if (options.Credentials is null && settings.ClientId is not null && settings.ClientSecret is not null && !settings.Empty) - { - options.Credentials = new ClientCredentials( - settings.ClientId, - settings.ClientSecret, - settings.TenantId - ); - } + //if (options.Credentials is null && settings.ClientId is not null && settings.ClientSecret is not null && !settings.Empty) + //{ + // options.Credentials = new ClientCredentials( + // settings.ClientId, + // settings.ClientSecret, + // settings.TenantId + // ); + //} options.Logger ??= new ConsoleLogger(loggingSettings); - var app = new App(options); - + //var app = new App(options); + builder.Services.AddSingleton(); builder.Services.AddSingleton(settings); builder.Services.AddSingleton(loggingSettings); - builder.Logging.AddTeams(app.Logger); - builder.Services.AddTeams(app); + //builder.Logging.AddTeams(app.Logger); + //builder.Services.AddTeams(app); return builder; } @@ -57,14 +57,14 @@ public static IHostApplicationBuilder AddTeamsCore(this IHostApplicationBuilder var loggingSettings = builder.Configuration.GetTeamsLogging(); // client credentials - if (settings.ClientId is not null && settings.ClientSecret is not null && !settings.Empty) - { - appBuilder = appBuilder.AddCredentials(new ClientCredentials( - settings.ClientId, - settings.ClientSecret, - settings.TenantId - )); - } + //if (settings.ClientId is not null && settings.ClientSecret is not null && !settings.Empty) + //{ + // appBuilder = appBuilder.AddCredentials(new ClientCredentials( + // settings.ClientId, + // settings.ClientSecret, + // settings.TenantId + // )); + //} var app = appBuilder.Build(); diff --git a/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/ServiceCollection.cs b/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/ServiceCollection.cs index 14c276f2..66e59f33 100644 --- a/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/ServiceCollection.cs +++ b/Libraries/Microsoft.Teams.Extensions/Microsoft.Teams.Extensions.Hosting/Microsoft.Teams.Apps.Extensions/ServiceCollection.cs @@ -37,20 +37,20 @@ public static IServiceCollection AddTeams(this IServiceCollection collection) return collection; } - public static IServiceCollection AddTeams(this IServiceCollection collection, AppOptions options) - { - var app = new App(options); - var log = new TeamsLogger(app.Logger); - - collection.AddSingleton(app.Logger); - collection.AddSingleton(app.Storage); - collection.AddSingleton(_ => new LoggerFactory([new TeamsLoggerProvider(log)])); - collection.AddSingleton(log); - collection.AddSingleton(app); - collection.AddHostedService(); - collection.AddSingleton(); - return collection; - } + //public static IServiceCollection AddTeams(this IServiceCollection collection, AppOptions options) + //{ + // var app = new App(options); + // var log = new TeamsLogger(app.Logger); + + // collection.AddSingleton(app.Logger); + // collection.AddSingleton(app.Storage); + // collection.AddSingleton(_ => new LoggerFactory([new TeamsLoggerProvider(log)])); + // collection.AddSingleton(log); + // collection.AddSingleton(app); + // collection.AddHostedService(); + // collection.AddSingleton(); + // return collection; + //} public static IServiceCollection AddTeams(this IServiceCollection collection, AppBuilder builder) { diff --git a/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.DevTools/Microsoft.Teams.Plugins.AspNetCore.DevTools.csproj b/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.DevTools/Microsoft.Teams.Plugins.AspNetCore.DevTools.csproj index 3863699f..5b987391 100644 --- a/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.DevTools/Microsoft.Teams.Plugins.AspNetCore.DevTools.csproj +++ b/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore.DevTools/Microsoft.Teams.Plugins.AspNetCore.DevTools.csproj @@ -24,7 +24,7 @@ - + diff --git a/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/ApplicationBuilder.cs b/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/ApplicationBuilder.cs index fdb9dfb5..fde60a1e 100644 --- a/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/ApplicationBuilder.cs +++ b/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/ApplicationBuilder.cs @@ -8,6 +8,7 @@ using Microsoft.Teams.Apps; using Microsoft.Teams.Apps.Annotations; using Microsoft.Teams.Apps.Plugins; +using Microsoft.Teams.Common.Http; namespace Microsoft.Teams.Plugins.AspNetCore.Extensions; @@ -22,7 +23,7 @@ public static partial class ApplicationBuilderExtensions public static App UseTeams(this IApplicationBuilder builder, bool routing = true) { var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly(); - var app = builder.ApplicationServices.GetService() ?? new App(builder.ApplicationServices.GetService()); + var app = builder.ApplicationServices.GetService() ?? new App(builder.ApplicationServices.GetService()!,builder.ApplicationServices.GetService()); var plugins = builder.ApplicationServices.GetServices(); var types = assembly.GetTypes(); diff --git a/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/HostApplicationBuilder.cs b/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/HostApplicationBuilder.cs index 760d94da..bf23d2e7 100644 --- a/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/HostApplicationBuilder.cs +++ b/Libraries/Microsoft.Teams.Plugins/Microsoft.Teams.Plugins.AspNetCore/Extensions/HostApplicationBuilder.cs @@ -6,8 +6,13 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Identity.Abstractions; +using Microsoft.Identity.Web; +using Microsoft.Identity.Web.TokenCacheProviders.InMemory; +using Microsoft.Teams.Api.Auth; using Microsoft.Teams.Apps; using Microsoft.Teams.Apps.Extensions; +using Microsoft.Teams.Common.Http; namespace Microsoft.Teams.Plugins.AspNetCore.Extensions; @@ -19,9 +24,20 @@ public static class HostApplicationBuilderExtensions /// /// set to false to disable the plugins default http controller /// set to true to disable token authentication - public static IHostApplicationBuilder AddTeams(this IHostApplicationBuilder builder, bool routing = true, bool skipAuth = false) + public static IHostApplicationBuilder AddTeams(this IHostApplicationBuilder builder, bool routing = true, bool skipAuth = false, string authSectionName = "Teams") { - builder.AddTeamsCore(); + builder.Services.AddHttpClient(); + builder.Services.AddTokenAcquisition(); + builder.Services.AddInMemoryTokenCaches(); + + builder.Services.AddScoped(); + builder.Services.AddSingleton(); + builder.Services.Configure(builder.Configuration.GetSection(authSectionName)); +#pragma warning disable ASP0000 // Use 'new(...)' + AppBuilder appBuilder = new AppBuilder(builder.Services.BuildServiceProvider()); +#pragma warning restore ASP0000 + + builder.AddTeamsCore(appBuilder); builder.AddTeamsPlugin(); builder.AddTeamsTokenAuthentication(skipAuth); diff --git a/Samples/Samples.Echo/Program.cs b/Samples/Samples.Echo/Program.cs index 20cdd0d4..f6f245f9 100644 --- a/Samples/Samples.Echo/Program.cs +++ b/Samples/Samples.Echo/Program.cs @@ -1,57 +1,16 @@ -using Microsoft.Teams.Api.Activities; -using Microsoft.Teams.Apps; using Microsoft.Teams.Apps.Activities; -using Microsoft.Teams.Apps.Annotations; -using Microsoft.Teams.Apps.Extensions; -using Microsoft.Teams.Apps.Plugins; -using Microsoft.Teams.Plugins.AspNetCore.DevTools.Extensions; using Microsoft.Teams.Plugins.AspNetCore.Extensions; -namespace Samples.Echo; +var builder = WebApplication.CreateBuilder(); -public static partial class Program -{ - public static void Main(string[] args) - { - var builder = WebApplication.CreateBuilder(args); - builder.Services.AddOpenApi(); - builder.Services.AddTransient(); - builder.AddTeams().AddTeamsDevTools(); - - var app = builder.Build(); - - if (app.Environment.IsDevelopment()) - { - app.MapOpenApi(); - } +builder.AddTeams(); +var app = builder.Build(); +var teamsApp = app.UseTeams(); - app.UseHttpsRedirection(); - app.UseTeams(); - app.Run(); - } - - [TeamsController] - public class Controller - { - [Activity] - public async Task OnActivity(IContext context, [Context] IContext.Next next) - { - context.Log.Info(context.AppId); - await next(); - } - - [Message] - public async Task OnMessage([Context] MessageActivity activity, [Context] IContext.Client client, [Context] Microsoft.Teams.Common.Logging.ILogger log) - { - log.Info("hit!"); - await client.Typing(); - await client.Send($"you said '{activity.Text}'"); - } +teamsApp.OnMessage(async context => +{ + await context.Typing(); + await context.Send($"you said '{context.Activity.Text}'"); +}); - [Microsoft.Teams.Apps.Events.Event("activity")] - public void OnEvent(IPlugin plugin, Microsoft.Teams.Apps.Events.Event @event) - { - Console.WriteLine("!!HIT!!"); - } - } -} \ No newline at end of file +app.Run(); diff --git a/Samples/Samples.Echo/appsettings.Development.json b/Samples/Samples.Echo/appsettings.Development.json index e1c7c6d9..5ddaaa00 100644 --- a/Samples/Samples.Echo/appsettings.Development.json +++ b/Samples/Samples.Echo/appsettings.Development.json @@ -10,7 +10,14 @@ } }, "Teams": { + "Instance": "https://login.microsoftonline.com/", + "TenantId": "", "ClientId": "", - "ClientSecret": "" + "ClientCredentials": [ + { + "SourceType": "ClientSecret", + "ClientSecret": "" + } + ] } } diff --git a/Samples/Samples.Echo/appsettings.json b/Samples/Samples.Echo/appsettings.json index bc6cda26..9bd983d9 100644 --- a/Samples/Samples.Echo/appsettings.json +++ b/Samples/Samples.Echo/appsettings.json @@ -10,8 +10,15 @@ } }, "Teams": { + "Instance": "https://login.microsoftonline.com/", + "TenantId": "", "ClientId": "", - "ClientSecret": "" + "ClientCredentials": [ + { + "SourceType": "ClientSecret", + "ClientSecret": "" + } + ] }, "AllowedHosts": "*" } diff --git a/Tests/Microsoft.Teams.Apps.Tests/AppTests.cs b/Tests/Microsoft.Teams.Apps.Tests/AppTests.cs index b7b02e9a..d1188185 100644 --- a/Tests/Microsoft.Teams.Apps.Tests/AppTests.cs +++ b/Tests/Microsoft.Teams.Apps.Tests/AppTests.cs @@ -24,7 +24,7 @@ public async Task Test_App_Start_GetBotToken_Success() { Credentials = credentials.Object, }; - var app = new App(options); + var app = new App(credentials.Object,options); var api = new Mock(_serviceUrl, CancellationToken.None) { CallBase = true }; api.Setup(a => a.Bots.Token.GetAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new TokenResponse() { AccessToken = _unexpiredJwt, TokenType = "bot" }); @@ -51,7 +51,7 @@ public async Task Test_App_Start_GetBotToken_Failure() Credentials = credentials.Object, Logger = logger.Object, }; - var app = new App(options); + var app = new App(credentials.Object, options); var api = new Mock(_serviceUrl, CancellationToken.None) { CallBase = true }; api.Setup(a => a.Bots.Token.GetAsync(It.IsAny(), It.IsAny())) .ThrowsAsync(exception); @@ -73,7 +73,7 @@ public async Task Test_App_Start_DoesNot_GetBotToken_WhenNoCredentials() { Credentials = null, }; - var app = new App(options); + var app = new App(null!, options); var api = new Mock(_serviceUrl, CancellationToken.None) { CallBase = true }; api.Setup(a => a.Bots.Token.GetAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new TokenResponse() { AccessToken = _unexpiredJwt, TokenType = "bot" }); @@ -98,7 +98,7 @@ public void Test_App_Client_TokenFactory_GetsToken_IfNotExists() Client = client.Object, Credentials = credentials.Object, }; - var app = new App(options); + var app = new App(credentials.Object, options); var api = new Mock(_serviceUrl, CancellationToken.None) { CallBase = true }; api.Setup(a => a.Bots.Token.GetAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new TokenResponse() { AccessToken = _unexpiredJwt, TokenType = "bot" }); @@ -124,7 +124,7 @@ public void Test_App_Client_TokenFactory_GetsToken_IfExpired() Client = client.Object, Credentials = credentials.Object, }; - var app = new App(options); + var app = new App(credentials.Object, options); app.Token = new JsonWebToken(_expiredJwt); credentials.Setup(c => c.Resolve(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(new TokenResponse() { AccessToken = _unexpiredJwt, TokenType = "bot" }); @@ -149,7 +149,7 @@ public void Test_App_Client_TokenFactory_DoesNotGetToken_IfValid() Client = client.Object, Credentials = credentials.Object, }; - var app = new App(options); + var app = new App(credentials.Object, options); app.Token = new JsonWebToken(_unexpiredJwt); credentials.Setup(c => c.Resolve(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(new TokenResponse() { AccessToken = _unexpiredJwt, TokenType = "bot" }); @@ -177,7 +177,7 @@ public void Test_App_Client_TokenFactory_DoesNotGetToken_IfNoCredentials() Client = client.Object, Credentials = null, }; - var app = new App(options); + var app = new App(null!, options); var api = new Mock(_serviceUrl, CancellationToken.None) { CallBase = true }; api.Setup(a => a.Bots.Token.GetAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new TokenResponse() { AccessToken = _unexpiredJwt, TokenType = "bot" }); @@ -209,7 +209,7 @@ public void Test_App_Client_CustomTokenFactory() Client = client.Object, Credentials = null, }; - var app = new App(options); + var app = new App(null!, options); // act client.Object.Options.TokenFactory();