BotSharp/src/WebStarter/Program.cs

83 lines
2.2 KiB
C#
Raw Normal View History

2023-11-24 23:26:33 +00:00
using BotSharp.Abstraction.Messaging;
2023-07-25 02:22:18 +00:00
using BotSharp.Abstraction.Users;
2023-05-27 01:58:31 +00:00
using BotSharp.Core;
2023-07-25 02:22:18 +00:00
using BotSharp.Core.Users.Services;
2023-06-11 23:46:02 +00:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
2023-05-27 01:58:31 +00:00
2023-05-26 01:36:15 +00:00
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
2023-11-24 23:26:33 +00:00
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new RichContentJsonConverter());
options.JsonSerializerOptions.Converters.Add(new TemplateMessageJsonConverter());
});
2023-06-11 23:46:02 +00:00
2023-05-26 01:36:15 +00:00
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();
2023-06-11 23:46:02 +00:00
// Add bearer authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])),
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = false,
ValidateIssuerSigningKey = true
};
});
2023-07-25 02:22:18 +00:00
builder.Services.AddScoped<IUserIdentity, UserIdentity>();
2023-05-27 01:58:31 +00:00
// Add BotSharp
2023-08-14 18:16:35 +00:00
builder.Services.AddBotSharp(builder.Configuration);
2023-06-03 02:07:30 +00:00
builder.Services.AddCors(options =>
{
options.AddPolicy("MyCorsPolicy",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
2023-05-26 01:36:15 +00:00
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
2023-06-11 23:46:02 +00:00
app.UseAuthentication();
2023-05-26 01:36:15 +00:00
app.UseAuthorization();
app.MapControllers();
2023-05-27 01:58:31 +00:00
// Use BotSharp
app.UseBotSharp();
2023-06-03 02:07:30 +00:00
#if DEBUG
app.UseCors("MyCorsPolicy");
#endif
2023-05-26 01:36:15 +00:00
app.Run();