82 lines
2.3 KiB
C#
82 lines
2.3 KiB
C#
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using Microsoft.IdentityModel.Tokens;
|
||
|
|
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();
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
builder.Services.AddAuthentication(x =>
|
||
|
|
{
|
||
|
|
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||
|
|
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||
|
|
}).AddCookie(x =>
|
||
|
|
{
|
||
|
|
x.Cookie.Name = "token";
|
||
|
|
|
||
|
|
}).AddJwtBearer(x =>
|
||
|
|
{
|
||
|
|
x.RequireHttpsMetadata = false;
|
||
|
|
x.SaveToken = true;
|
||
|
|
x.TokenValidationParameters = new TokenValidationParameters
|
||
|
|
{
|
||
|
|
ValidateIssuerSigningKey = true,
|
||
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration.GetValue<string>("Secret"))),
|
||
|
|
ValidateIssuer = false,
|
||
|
|
ValidateAudience = false
|
||
|
|
};
|
||
|
|
x.Events = new JwtBearerEvents
|
||
|
|
{
|
||
|
|
OnMessageReceived = context =>
|
||
|
|
{
|
||
|
|
//context.Token = context.Request.Cookies["token"];
|
||
|
|
context.Token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Ik1pY2hhbCBaaWVsaW5za2kiLCJuYmYiOjE2NzAyODk3NTAsImV4cCI6MTY3MDg5NDU1MCwiaWF0IjoxNjcwMjg5NzUwfQ.XZ1lE_Jio9N5aetvY8qX8rS2xoIcPw3GJWGSatPh1VokQkrILOowvvibdGViQOOi39qGBOFKa8JC61XcaL-1qw";
|
||
|
|
return Task.CompletedTask;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
});
|
||
|
|
|
||
|
|
builder.Services.AddControllers();
|
||
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||
|
|
builder.Services.AddEndpointsApiExplorer();
|
||
|
|
builder.Services.AddSwaggerGen();
|
||
|
|
|
||
|
|
var app = builder.Build();
|
||
|
|
|
||
|
|
// 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();
|