protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { try { AuthenticationHeaderValue authValue = request.Headers.Authorization; if (authValue == null || authValue.Scheme != BasicAuthResponseHeaderValue || authValue.Parameter == "undefined") { return Unauthorized(request); } string[] credentials = Encoding.ASCII.GetString(Convert.FromBase64String(authValue.Parameter)).Split(new[] { ':' }); if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0]) || string.IsNullOrEmpty(credentials[1])) { return Unauthorized(request); } var userName = credentials[0]; var password = credentials[1]; User user; using (var context = new TrainingContext()) { try { user = context.Users.Single(x => x.UserName == userName && x.Password == password); } catch (InvalidOperationException) { return DBContextChanged(request); } } if (user == null) { return Unauthorized(request); } IPrincipal principal = new GenericPrincipal(new GenericIdentity(credentials[0], BasicAuthResponseHeaderValue), null); Thread.CurrentPrincipal = principal; HttpContext.Current.User = principal; var data = await base.SendAsync(request, cancellationToken); if (data.IsSuccessStatusCode) { Console.WriteLine("SUCCESS : " + request.RequestUri); } return data; } catch (Exception ex) { Logger.Error(ex); return Unauthorized(request); } }
public ReportObject(DateTime startDate, DateTime endDate, int companyId, string courseStatus, int divisionId = 0, int employeeId = 0, int courseId = 0, DateTime examStartDate = default(DateTime), DateTime examEndDate = default(DateTime), int instructorId = 0, int regionId = 0) { if (endDate < startDate) { endDate = DateTime.MaxValue; startDate = DateTime.MaxValue; } this.companyId = companyId; this.divisionId = divisionId; this.employeeId = employeeId; this.courseId = courseId; this.instructorId = instructorId; this.endDate = endDate; this.startDate = startDate; this.examEndDate = examEndDate; this.examStartDate = examStartDate; this.courseStatus = courseStatus; this.regionId = regionId; context = new TrainingContext(); }