Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Program.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using BlackRock.Reporting.API.Authentication;
  2. using BlackRock.Reporting.API.Core;
  3. using BlackRock.Reporting.API.Core.Models;
  4. using BlackRock.Reporting.API.Exceptions;
  5. using BlackRock.Reporting.API.Jwt;
  6. using BlackRock.Reporting.API.Mediator.AuthenticationMediator;
  7. using BlackRock.Reporting.API.Persistence;
  8. using BlackRock.Reporting.API.Persistence.Repositories;
  9. using MediatR;
  10. using Microsoft.AspNetCore.Authentication.JwtBearer;
  11. using Microsoft.AspNetCore.Identity;
  12. using Microsoft.EntityFrameworkCore;
  13. using Microsoft.IdentityModel.Tokens;
  14. using Microsoft.OpenApi.Models;
  15. var builder = WebApplication.CreateBuilder(args);
  16. // Add services to the container.
  17. builder.Services.AddDbContext<BRDbContext>(config =>
  18. //config.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
  19. config.UseSqlite(builder.Configuration.GetConnectionString("Default")));
  20. builder.Services.AddScoped<IGenerator, PdfGenerator>();
  21. builder.Services.AddScoped(typeof(IRepository<>), typeof(EFRepository<>));
  22. builder.Services.AddScoped<IUsersRepository, UsersRepository>();
  23. builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
  24. builder.Services.AddScoped<IJwtManager, JwtManager>();
  25. builder.Services.AddScoped<IRefreshTokenManager, RefreshTokenManager>();
  26. builder.Services.AddCors();
  27. builder.Services.AddControllers();
  28. builder.Services.AddAutoMapper(typeof(Program));
  29. builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
  30. .AddTokenProvider("MyApp",typeof(DataProtectorTokenProvider<ApplicationUser>))
  31. .AddEntityFrameworkStores<BRDbContext>()
  32. .AddDefaultTokenProviders();
  33. builder.Services.AddAuthentication(options =>
  34. {
  35. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  36. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  37. options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
  38. })
  39. .AddJwtBearer(options =>
  40. {
  41. options.SaveToken = true;
  42. options.RequireHttpsMetadata = false;
  43. options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters()
  44. {
  45. ValidateIssuer = false,
  46. ValidateAudience = false,
  47. ValidateLifetime = true,
  48. RequireExpirationTime = true,
  49. IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String("db3OIsj+BXE9NZDy0t8W3TcNekrF+2d/1sFnWG4HnV8TZY30iTOdtVWJG8abWvB1GlOgJuQZdcF2Luqm/hccMw=="))
  50. //IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String(builder.Configuration["SecurityKey"]))
  51. };
  52. });
  53. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  54. builder.Services.AddEndpointsApiExplorer();
  55. builder.Services.AddSwaggerGen(
  56. c => {
  57. c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "BlackRock.Service.API", Version = "v1" });
  58. c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
  59. {
  60. Description = @"Enter 'Bearer' [space] and your token",
  61. Name = "Authorization",
  62. In = Microsoft.OpenApi.Models.ParameterLocation.Header,
  63. Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey,
  64. Scheme = "Bearer"
  65. });
  66. c.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement {
  67. {
  68. new OpenApiSecurityScheme
  69. {
  70. Reference = new OpenApiReference
  71. {
  72. Type = ReferenceType.SecurityScheme,
  73. Id = "Bearer"
  74. },
  75. Scheme = "OAuth2",
  76. Name = "Bearer",
  77. In = ParameterLocation.Header
  78. },
  79. new List<String>()
  80. }
  81. });
  82. }
  83. );
  84. builder.Services.AddMediatR(typeof(Program));
  85. var app = builder.Build();
  86. // Configure the HTTP request pipeline.
  87. app.ConfigureExceptionHandler(builder.Logging);
  88. app.UseCors(options =>
  89. options.AllowAnyHeader()
  90. .AllowAnyMethod()
  91. .AllowAnyOrigin()
  92. );
  93. if (app.Environment.IsDevelopment())
  94. {
  95. app.UseSwagger();
  96. app.UseSwaggerUI();
  97. }
  98. app.UseHttpsRedirection();
  99. app.UseAuthentication();
  100. app.UseAuthorization();
  101. app.MapControllers();
  102. app.Run();