Added authentication and authorization, updated dependency injections, removed hard-coded connection string
This commit is contained in:
		| @@ -1,6 +1,10 @@ | ||||
| using System.Reflection; | ||||
| using System.Text; | ||||
| using ApplicationLayer; | ||||
| using Infrastructure; | ||||
| using Infrastructure.Auth; | ||||
| using Microsoft.AspNetCore.Authentication.JwtBearer; | ||||
| using Microsoft.IdentityModel.Tokens; | ||||
|  | ||||
| namespace SchengenVisaApi; | ||||
|  | ||||
| @@ -8,21 +12,61 @@ namespace SchengenVisaApi; | ||||
| public static class DependencyInjection | ||||
| { | ||||
|     /// Add needed services | ||||
|     public static IServiceCollection RegisterServices(this IServiceCollection services) | ||||
|     public static void RegisterServices(this WebApplicationBuilder builder) | ||||
|     { | ||||
|         services | ||||
|             .AddInfrastructure() | ||||
|         var config = builder.Configuration; | ||||
|         var environment = builder.Environment; | ||||
|  | ||||
|         builder.Services | ||||
|             .AddInfrastructure(config, environment.IsDevelopment()) | ||||
|             .AddApplicationLayer() | ||||
|             .AddPresentation(); | ||||
|             .AddAuth(config) | ||||
|             .AddPresentation(environment); | ||||
|     } | ||||
|  | ||||
|     /// Add services needed for Presentation layer | ||||
|     private static void AddPresentation(this IServiceCollection services, | ||||
|         IWebHostEnvironment environment) | ||||
|     { | ||||
|         if (environment.IsDevelopment()) | ||||
|         { | ||||
|             services.AddSwagger(); | ||||
|         } | ||||
|  | ||||
|         services.AddControllers(); | ||||
|     } | ||||
|  | ||||
|     /// Adds authentication, authorization and token generator | ||||
|     private static IServiceCollection AddAuth(this IServiceCollection services, IConfigurationManager configurationManager) | ||||
|     { | ||||
|         var parameters = new TokenValidationParameters | ||||
|         { | ||||
|             ValidIssuer = configurationManager["JwtSettings:Issuer"], | ||||
|             ValidAudience = configurationManager["JwtSettings:Audience"], | ||||
|             IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configurationManager["JwtSettings:Key"]!)), | ||||
|             ValidateIssuer = true, | ||||
|             ValidateAudience = true, | ||||
|             ValidateLifetime = true, | ||||
|             ValidateIssuerSigningKey = true | ||||
|         }; | ||||
|  | ||||
|         services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) | ||||
|             .AddJwtBearer(opts => opts.TokenValidationParameters = parameters); | ||||
|         services.AddAuthorization(); | ||||
|  | ||||
|         services.AddTokenGenerator(new TokenGeneratorOptions( | ||||
|             Issuer: parameters.ValidIssuer!, | ||||
|             Audience: parameters.ValidAudience!, | ||||
|             Credentials: new SigningCredentials(parameters.IssuerSigningKey, SecurityAlgorithms.HmacSha256), | ||||
|             ValidTime: TimeSpan.FromMinutes(30) | ||||
|         )); | ||||
|  | ||||
|         return services; | ||||
|     } | ||||
|  | ||||
|     /// Add services needed for Presentation layer | ||||
|     private static void AddPresentation(this IServiceCollection services) | ||||
|     /// Add swagger | ||||
|     private static void AddSwagger(this IServiceCollection services) | ||||
|     { | ||||
|         services.AddControllers(); | ||||
|         services.AddEndpointsApiExplorer(); | ||||
|         services.AddSwaggerGen(options => | ||||
|         { | ||||
|             var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user