Skip to content

Commit c761947

Browse files
committed
Added logging to filters.
1 parent 67f15cf commit c761947

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

PolyDeploy/WebAPI/APIAuthentication.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using Cantarus.Modules.PolyDeploy.Components;
22
using Cantarus.Modules.PolyDeploy.DataAccess.Models;
3+
using DotNetNuke.Services.Log.EventLog;
34
using System;
4-
using System.Collections;
5-
using System.Collections.Generic;
65
using System.Linq;
76
using System.Net;
87
using System.Net.Http;
9-
using System.Web;
108
using System.Web.Http.Controllers;
119
using System.Web.Http.Filters;
1210

@@ -21,13 +19,15 @@ public override void OnActionExecuting(HttpActionContext actionContext)
2119
bool authenticated = false;
2220
string message = "Access denied.";
2321

22+
string apiKey = null;
23+
2424
try
2525
{
2626
// Is there an api key header present?
2727
if (actionContext.Request.Headers.Contains("x-api-key"))
2828
{
2929
// Get the api key from the header.
30-
string apiKey = actionContext.Request.Headers.GetValues("x-api-key").FirstOrDefault();
30+
apiKey = actionContext.Request.Headers.GetValues("x-api-key").FirstOrDefault();
3131

3232
// Make sure it's not null and it's 32 characters or we're wasting our time.
3333
if (apiKey != null && apiKey.Length == 32)
@@ -55,6 +55,12 @@ public override void OnActionExecuting(HttpActionContext actionContext)
5555
// If authentication failure occurs, return a response without carrying on executing actions.
5656
if (!authenticated)
5757
{
58+
EventLogController elc = new EventLogController();
59+
60+
string log = string.Format("(APIKey: {1}) {2}", apiKey, message);
61+
62+
elc.AddLog("PolyDeploy", log, EventLogController.EventLogType.HOST_ALERT);
63+
5864
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, message);
5965
}
6066
}

PolyDeploy/WebAPI/InWhitelist.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using Cantarus.Modules.PolyDeploy.Components;
2-
using Cantarus.Modules.PolyDeploy.DataAccess.Models;
2+
using DotNetNuke.Services.Log.EventLog;
33
using System;
4-
using System.Collections;
5-
using System.Collections.Generic;
6-
using System.Linq;
74
using System.Net;
85
using System.Net.Http;
96
using System.Web;
@@ -21,9 +18,11 @@ public override void OnActionExecuting(HttpActionContext actionContext)
2118
bool authenticated = false;
2219
string message = "Access denied.";
2320

21+
string clientIpAddress = null;
22+
2423
try
2524
{
26-
string clientIpAddress = HttpContext.Current.Request.UserHostAddress;
25+
clientIpAddress = HttpContext.Current.Request.UserHostAddress;
2726

2827
// Got the ip address?
2928
if (!string.IsNullOrEmpty(clientIpAddress))
@@ -46,6 +45,12 @@ public override void OnActionExecuting(HttpActionContext actionContext)
4645
// If authentication failure occurs, return a response without carrying on executing actions.
4746
if (!authenticated)
4847
{
48+
EventLogController elc = new EventLogController();
49+
50+
string log = string.Format("(IP: {1}) {2}", clientIpAddress, message);
51+
52+
elc.AddLog("PolyDeploy", log, EventLogController.EventLogType.HOST_ALERT);
53+
4954
actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Forbidden, message);
5055
}
5156
}

0 commit comments

Comments
 (0)