using BlackRock.Reporting.API.Authentication; using BlackRock.Reporting.API.Core; using BlackRock.Reporting.API.Core.Models; using BlackRock.Reporting.API.Exceptions; using BlackRock.Reporting.API.Jwt; using BlackRock.Reporting.API.Mediator.AuthenticationMediator; using BlackRock.Reporting.API.Persistence; using BlackRock.Reporting.API.Persistence.Repositories; using MediatR; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddDbContext(config => //config.UseSqlServer(builder.Configuration.GetConnectionString("Default"))); config.UseSqlite(builder.Configuration.GetConnectionString("Default"))); builder.Services.AddScoped(); builder.Services.AddScoped(typeof(IRepository<>), typeof(EFRepository<>)); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddCors(); builder.Services.AddControllers(); builder.Services.AddAutoMapper(typeof(Program)); builder.Services.AddIdentity() .AddTokenProvider("MyApp",typeof(DataProtectorTokenProvider)) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.SaveToken = true; options.RequireHttpsMetadata = false; options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters() { ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, RequireExpirationTime = true, IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String("db3OIsj+BXE9NZDy0t8W3TcNekrF+2d/1sFnWG4HnV8TZY30iTOdtVWJG8abWvB1GlOgJuQZdcF2Luqm/hccMw==")) //IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String(builder.Configuration["SecurityKey"])) }; }); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen( c => { c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "BlackRock.Service.API", Version = "v1" }); c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme { Description = @"Enter 'Bearer' [space] and your token", Name = "Authorization", In = Microsoft.OpenApi.Models.ParameterLocation.Header, Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey, Scheme = "Bearer" }); c.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" }, Scheme = "OAuth2", Name = "Bearer", In = ParameterLocation.Header }, new List() } }); } ); builder.Services.AddMediatR(typeof(Program)); var app = builder.Build(); // Configure the HTTP request pipeline. app.ConfigureExceptionHandler(builder.Logging); app.UseCors(options => options.AllowAnyHeader() .AllowAnyMethod() .AllowAnyOrigin() ); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();