public static HttpClient CreateClientWithJwtAuthentication <T>(this WebApplicationFactory <T> factory, string[] roles = null) where T : class { var client = factory.CreateClient(); var token = JwtTokenGeneratorService.GenerateJwtToken(roles); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); return(client); }
public void ConfigureServices(IServiceCollection services) { services.AddSingleton(Configuration); services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddTransient(_ => new SqlConnection(Configuration.GetConnectionString("DefaultConnection"))); var tokenFactoryService = new JwtTokenGeneratorService(Configuration); services.AddSingleton <IAuthTokenFactory>(tokenFactoryService); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.TokenValidationParameters = tokenFactoryService.TokenValidationParameters; }); services.AddTransient <AppUserRepository>(); services.AddTransient <CriticalMomentRepository>(); services.AddTransient <StateRecordRepository>(); services.AddTransient <TeamMemberRepository>(); services.AddTransient <TeamRepository>(); services.AddTransient <TrainingRepository>(); services.AddTransient <VideoStreamRepository>(); services.AddSingleton <IRefreshTokenFactory, RefreshTokenFactory>(); services.AddSingleton <IPasswordHasher, IdentityPasswordHasherService>(); services.AddTransient <IAppUserService, AppUserService>(); services.AddTransient <IAuthAsyncService, AuthService>(); services.AddTransient <IConfigChangeService, ConfigChangeService>(); services.AddTransient <ICriticalMomentService, CriticalMomentService>(); services.AddTransient <IStateRecordService, StateRecordService>(); services.AddTransient <IStressRecognitionService, StressRecognitionService>(); services.AddTransient <ITeamMemberService, TeamMemberService>(); services.AddTransient <ITeamService, TeamService>(); services.AddTransient <ITrainingService, TrainingService>(); services.AddTransient <IYouTubeService, YouTubeV3Service>(); services.AddTransient <DatabaseRepository>(); services.AddTransient <IDatabaseBackupService, DatabaseBackupService>(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); }); }
public IActionResult Post([FromBody] User userBody) { var user = users.FirstOrDefault(x => x.Name == userBody.Name && x.Password == userBody.Password); if (user == null) { return(BadRequest(new { message = "Username or password is incorrect" })); } var tokenInfo = JwtTokenGeneratorService.GenerateToken(user); tokenInfo.UserName = user.Name; return(Ok(tokenInfo)); }