using
Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
using System.Text;
using Services;
using webapp_codedocu.Data;
#region //==< Builder.Configure >==
//==< Builder >==
var builder =
WebApplication.CreateBuilder(args);
// Add services to the container.
//<
get_config >
string config_App_SignInKey =
builder.Configuration.GetSection("AppSettings").GetValue<String>("App_SignInKey")
?? throw new
InvalidOperationException("AppSignInKey missing in
Config.AppSettings");
string url_FrontEnd =
builder.Configuration.GetSection("AppSettings").GetValue<String>("Url_FrontEnd")
?? throw new
InvalidOperationException("UrlFrontEnd is missing in
Config.AppSettings");
string connectionString =
builder.Configuration.GetConnectionString("DefaultConnection")
?? throw new
InvalidOperationException("Connection string not found in Config");
//</
get_config >
//*
Connect Database
builder.Services.AddDbContext<ApplicationDbContext>(options
=> options.UseSqlServer(connectionString));
builder.Services.AddControllers();
//
Learn more about configuring Swagger/OpenAPI at
https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient("client",client=>client.Timeout=TimeSpan.FromMinutes(5));
//for
service
//builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options
=>
{
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Description = @"JWT
Authorization header using the Bearer scheme. \r\n\r\n
Enter 'Bearer' [space]
and then your token in the text input below.
\r\n\r\nExample:
'Bearer 12345abcdef'",
In = ParameterLocation.Header,
Name = "Authorization",
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer" //#added
});
options.OperationFilter<SecurityRequirementsOperationFilter>(); //by Swashbuckle
});
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) //by Microsoft.AspCore.Authentication
.AddJwtBearer(options =>
{
options.TokenValidationParameters
= new
TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new
SymmetricSecurityKey(Encoding.UTF8.GetBytes(config_App_SignInKey)), //*Decode AccessToken by
App-Key
ValidateIssuer = false,
ValidateAudience = false
};
});
//<
CORS >
//*allow
calls from AngularUI
builder.Services.AddCors(options => options.AddPolicy(
//#TODO:
policy.WithOrigins(url_FrontEnd) , problems on update
name: "FrontendUI", policy => {
policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); }));
//</
CORS >
//==</ Builder >==
#endregion //==</ Builder.Configure >==
#region //==< APP >==
//==< APP >==
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//< CORS >
app.UseCors("FrontendUI");
//</ CORS >
app.UseHttpsRedirection();
app.UseAuthentication(); //*Get User
app.UseAuthorization();
app.MapControllers();
app.Run();
//==</
APP >==
#endregion //==</ APP >==
|