2022-12-06 12:27:09 +01:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2022-12-21 18:35:26 +01:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2022-12-06 12:27:09 +01:00
|
|
|
using System.Text;
|
|
|
|
|
using WebAPI;
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
var connectionString = builder.Configuration.GetConnectionString("SQLDatabase");
|
|
|
|
|
builder.Services.AddDbContext<AppDbContext>(x => x.UseSqlServer(connectionString));
|
|
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("CORSPolicy", builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.WithOrigins("http://localhost:4200")
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials();
|
|
|
|
|
|
|
|
|
|
builder.WithOrigins("https://diuna.bim-it.pl")
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowCredentials();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-09 00:14:05 +01:00
|
|
|
builder.Services.AddControllers();
|
2022-12-06 12:27:09 +01:00
|
|
|
|
2022-12-09 00:14:05 +01:00
|
|
|
builder.Services.AddAuthentication(options =>
|
|
|
|
|
{
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
}).AddJwtBearer(options =>
|
2022-12-06 12:27:09 +01:00
|
|
|
{
|
2022-12-09 00:14:05 +01:00
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
2022-12-06 12:27:09 +01:00
|
|
|
{
|
|
|
|
|
ValidateIssuer = false,
|
2022-12-09 00:14:05 +01:00
|
|
|
ValidateAudience = false,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Secret"]))
|
2022-12-06 12:27:09 +01:00
|
|
|
};
|
2022-12-21 18:35:26 +01:00
|
|
|
|
2022-12-06 12:27:09 +01:00
|
|
|
});
|
2022-12-09 00:14:05 +01:00
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
|
|
2022-12-06 12:27:09 +01:00
|
|
|
|
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2022-12-09 00:14:05 +01:00
|
|
|
app.Use(async (context, next) =>
|
|
|
|
|
{
|
2022-12-21 18:35:26 +01:00
|
|
|
string token = context.Request.Headers["Authorization"].ToString();
|
|
|
|
|
if (token.Length > 0) {
|
|
|
|
|
var handler = new JwtSecurityTokenHandler();
|
|
|
|
|
var data = handler.ReadJwtToken(token.Split(' ')[1]);
|
|
|
|
|
context.Request.Headers.Add("UserId", new Microsoft.Extensions.Primitives.StringValues(data.Subject));
|
|
|
|
|
}
|
2022-12-09 00:14:05 +01:00
|
|
|
await next(context);
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-06 12:27:09 +01:00
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// app.UseHttpsRedirection();
|
|
|
|
|
app.UseCors("CORSPolicy");
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
app.Run();
|