forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Startup.cs
115 lines (104 loc) · 4.78 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// <copyright file="Startup.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
namespace TokenApp
{
using System.IO;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using TokenApp.Bots;
using TokenApp.Repository;
using TokenApp.Service;
/// <summary>
/// Register services in DI container, and set up middle-wares in the pipeline.
/// </summary>
public class Startup
{
/// <summary>
/// Initializes a new instance of the <see cref="Startup"/> class.
/// </summary>
/// <param name="configuration">IConfiguration instance.</param>
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
/// <summary>
/// Gets the IConfiguration instance.
/// </summary>
public IConfiguration Configuration { get; }
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services">IServiceCollection instance.</param>
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.Authority = "https://login.microsoftonline.com/common";
options.Audience = this.Configuration["AzureAd:ApplicationIdURI"];
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
};
});
services.AddControllersWithViews();
services.AddControllers().AddNewtonsoftJson();
services.AddSingleton<AppCredentials, MicrosoftAppCredentials>(
m => new MicrosoftAppCredentials(this.Configuration["MicrosoftAppId"], this.Configuration["MicrosoftAppPassword"]));
// Create the Bot Framework Adapter with error handling enabled.
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
services.AddSingleton<ITenantInfoRepository, InMemoryTenantInfoRepository>();
services.AddSingleton<IMeetingService, MeetingService>();
services.AddSingleton<IMeetingTokenRepository, InMemoryMeetingTokenRepository>();
services.AddOptions<MeetingServiceOptions>().Configure<IConfiguration>((options, configuration) =>
{
// For simplicity, in this sample we set the Teams app ID in the manifest to the app ID of the bot
options.TeamsAppId = configuration.GetValue<string>("MicrosoftAppId");
options.ContentBubbleUrl = configuration.GetValue<string>("ContentBubbleUrl");
});
services.AddHttpClient();
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, TokenBot>();
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app">IApplicationBuilder instance, which is a class that provides the mechanisms to configure an application's request pipeline.</param>
/// <param name="env">IHostingEnvironment instance, which provides information about the web hosting environment an application is running in.</param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDefaultFiles(new DefaultFilesOptions
{
DefaultFileNames = { "index.html" },
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "App/dist")),
})
.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, "App/dist")),
})
.UseWebSockets()
.UseRouting()
.UseAuthentication()
.UseAuthorization()
.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}