Skip to content

Commit c27bfff

Browse files
committed
Added the ability to get the logged in user from the current thread if the http context is unavailable
1 parent 4e2b30d commit c27bfff

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

Source/Platforms/Web/ExceptionlessWebPlugin.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Web;
34
using Exceptionless.Dependency;
45
using Exceptionless.Plugins;
@@ -17,16 +18,13 @@ public void Run(EventPluginContext context) {
1718
// if the context is not passed in, try and grab it
1819
if (httpContext == null && HttpContext.Current != null)
1920
httpContext = HttpContext.Current.ToWrapped();
21+
22+
var serializer = context.Client.Configuration.Resolver.GetJsonSerializer();
23+
if (context.Client.Configuration.IncludePrivateInformation)
24+
AddUser(context, httpContext, serializer);
2025

2126
if (httpContext == null)
2227
return;
23-
24-
var serializer = context.Client.Configuration.Resolver.GetJsonSerializer();
25-
if (context.Client.Configuration.IncludePrivateInformation && httpContext.User != null && httpContext.User.Identity.IsAuthenticated) {
26-
var user = context.Event.GetUserIdentity(serializer);
27-
if (user == null)
28-
context.Event.SetUserIdentity(httpContext.User.Identity.Name);
29-
}
3028

3129
var tags = httpContext.Items[TAGS_HTTP_CONTEXT_NAME] as TagSet;
3230
if (tags != null)
@@ -56,5 +54,16 @@ public void Run(EventPluginContext context) {
5654

5755
context.Event.AddRequestInfo(ri);
5856
}
57+
58+
private static void AddUser(EventPluginContext context, HttpContextBase httpContext, IJsonSerializer serializer) {
59+
var user = context.Event.GetUserIdentity(serializer);
60+
if (user != null)
61+
return;
62+
63+
if (httpContext != null && httpContext.User != null && httpContext.User.Identity.IsAuthenticated)
64+
context.Event.SetUserIdentity(httpContext.User.Identity.Name);
65+
else if (Thread.CurrentPrincipal != null && Thread.CurrentPrincipal.Identity.IsAuthenticated)
66+
context.Event.SetUserIdentity(Thread.CurrentPrincipal.Identity.Name);
67+
}
5968
}
6069
}

Source/Platforms/WebApi/ExceptionlessWebApiPlugin.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Reflection;
66
using System.Security.Principal;
77
using System.Threading;
8+
using System.Web.Http.Controllers;
89
using Exceptionless.Dependency;
910
using Exceptionless.Plugins;
1011
using Exceptionless.Extensions;
@@ -15,23 +16,14 @@ namespace Exceptionless.WebApi {
1516
[Priority(90)]
1617
internal class ExceptionlessWebApiPlugin : IEventPlugin {
1718
public void Run(EventPluginContext context) {
18-
if (!context.ContextData.ContainsKey("HttpActionContext"))
19-
return;
20-
2119
var actionContext = context.ContextData.GetHttpActionContext();
20+
var serializer = context.Client.Configuration.Resolver.GetJsonSerializer();
21+
if (context.Client.Configuration.IncludePrivateInformation)
22+
AddUser(context, actionContext, serializer);
23+
2224
if (actionContext == null)
2325
return;
2426

25-
var serializer = context.Client.Configuration.Resolver.GetJsonSerializer();
26-
if (context.Client.Configuration.IncludePrivateInformation) {
27-
var user = context.Event.GetUserIdentity(serializer);
28-
if (user == null) {
29-
IPrincipal principal = GetPrincipal(actionContext.Request);
30-
if (principal != null && principal.Identity.IsAuthenticated)
31-
context.Event.SetUserIdentity(principal.Identity.Name);
32-
}
33-
}
34-
3527
var ri = context.Event.GetRequestInfo(serializer);
3628
if (ri != null)
3729
return;
@@ -54,9 +46,19 @@ public void Run(EventPluginContext context) {
5446
context.Event.AddRequestInfo(ri);
5547
}
5648

49+
private static void AddUser(EventPluginContext context, HttpActionContext actionContext, IJsonSerializer serializer) {
50+
var user = context.Event.GetUserIdentity(serializer);
51+
if (user != null)
52+
return;
53+
54+
var principal = GetPrincipal(actionContext != null ? actionContext.Request : null);
55+
if (principal != null && principal.Identity.IsAuthenticated)
56+
context.Event.SetUserIdentity(principal.Identity.Name);
57+
}
58+
5759
private static IPrincipal GetPrincipal(HttpRequestMessage request) {
5860
if (request == null)
59-
throw new ArgumentNullException("request");
61+
return Thread.CurrentPrincipal;
6062

6163
const string RequestContextKey = "MS_RequestContext";
6264

@@ -73,7 +75,6 @@ private static IPrincipal GetPrincipal(HttpRequestMessage request) {
7375
}
7476

7577
var principal = _principalGetAccessor(context) as IPrincipal;
76-
7778
return principal ?? Thread.CurrentPrincipal;
7879
}
7980

Source/Samples/SampleWebApi/Controllers/ValuesController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Web.Http;
4+
using Exceptionless.Logging;
45

56
namespace Exceptionless.SampleWebApi.Controllers {
67
public class ValuesController : ApiController {
78
// GET api/values
89
public IEnumerable<string> Get() {
10+
ExceptionlessClient.Default.SubmitLog("ValuesController", "Getting results", LogLevel.Info);
911
throw new ApplicationException("WebApi GET error");
1012
}
1113

0 commit comments

Comments
 (0)