Skip to content

Commit 776b091

Browse files
committed
Fixed a bug where the web clients could overwrite previously set request info or user info
1 parent 14269b6 commit 776b091

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed
Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
11
using System;
22
using System.Web;
3+
using Exceptionless.Dependency;
34
using Exceptionless.Plugins;
45
using Exceptionless.Extensions;
56
using Exceptionless.Logging;
67
using Exceptionless.Models;
7-
using Exceptionless.Models.Data;
88

99
namespace Exceptionless.Web {
1010
[Priority(90)]
1111
internal class ExceptionlessWebPlugin : IEventPlugin {
1212
private const string TAGS_HTTP_CONTEXT_NAME = "Exceptionless.Tags";
1313

1414
public void Run(EventPluginContext context) {
15-
HttpContextBase httpContext = context.ContextData.GetHttpContext();
15+
var httpContext = context.ContextData.GetHttpContext();
1616

1717
// if the context is not passed in, try and grab it
1818
if (httpContext == null && HttpContext.Current != null)
1919
httpContext = HttpContext.Current.ToWrapped();
2020

2121
if (httpContext == null)
2222
return;
23-
24-
// ev.ExceptionlessClientInfo.Platform = ".NET Web";
25-
if (context.Client.Configuration.IncludePrivateInformation
26-
&& httpContext.User != null
27-
&& httpContext.User.Identity.IsAuthenticated)
28-
context.Event.SetUserIdentity(httpContext.User.Identity.Name);
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+
}
2930

3031
var tags = httpContext.Items[TAGS_HTTP_CONTEXT_NAME] as TagSet;
3132
if (tags != null)
3233
context.Event.Tags.UnionWith(tags);
34+
35+
var ri = context.Event.GetRequestInfo(serializer);
36+
if (ri != null)
37+
return;
3338

34-
RequestInfo requestInfo = null;
3539
try {
36-
requestInfo = httpContext.GetRequestInfo(context.Client.Configuration);
40+
ri = httpContext.GetRequestInfo(context.Client.Configuration);
3741
} catch (Exception ex) {
3842
context.Log.Error(typeof(ExceptionlessWebPlugin), ex, "Error adding request info.");
3943
}
4044

41-
if (requestInfo == null)
45+
if (ri == null)
4246
return;
4347

4448
var httpException = context.ContextData.GetException() as HttpException;
4549
if (httpException != null) {
4650
int httpCode = httpException.GetHttpCode();
4751
if (httpCode == 404) {
4852
context.Event.Type = Event.KnownTypes.NotFound;
49-
context.Event.Source = requestInfo.GetFullPath(includeHttpMethod: true, includeHost: false, includeQueryString: false);
53+
context.Event.Source = ri.GetFullPath(includeHttpMethod: true, includeHost: false, includeQueryString: false);
5054
}
5155
}
5256

53-
context.Event.AddRequestInfo(requestInfo);
57+
context.Event.AddRequestInfo(ri);
5458
}
5559
}
5660
}

Source/Platforms/WebApi/ExceptionlessWebApiPlugin.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
using System.Reflection;
66
using System.Security.Principal;
77
using System.Threading;
8-
using System.Web.Http.Controllers;
98
using Exceptionless.Dependency;
109
using Exceptionless.Plugins;
1110
using Exceptionless.Extensions;
1211
using Exceptionless.Logging;
1312
using Exceptionless.Models;
14-
using Exceptionless.Models.Data;
1513

1614
namespace Exceptionless.WebApi {
1715
[Priority(90)]
@@ -20,31 +18,40 @@ public void Run(EventPluginContext context) {
2018
if (!context.ContextData.ContainsKey("HttpActionContext"))
2119
return;
2220

23-
HttpActionContext actionContext = context.ContextData.GetHttpActionContext();
21+
var actionContext = context.ContextData.GetHttpActionContext();
2422
if (actionContext == null)
2523
return;
2624

27-
IPrincipal principal = GetPrincipal(actionContext.Request);
28-
if (context.Client.Configuration.IncludePrivateInformation && principal != null && principal.Identity.IsAuthenticated)
29-
context.Event.SetUserIdentity(principal.Identity.Name);
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+
35+
var ri = context.Event.GetRequestInfo(serializer);
36+
if (ri != null)
37+
return;
3038

31-
RequestInfo requestInfo = null;
3239
try {
33-
requestInfo = actionContext.GetRequestInfo(context.Client.Configuration);
40+
ri = actionContext.GetRequestInfo(context.Client.Configuration);
3441
} catch (Exception ex) {
3542
context.Log.Error(typeof(ExceptionlessWebApiPlugin), ex, "Error adding request info.");
3643
}
3744

38-
if (requestInfo == null)
45+
if (ri == null)
3946
return;
4047

41-
var error = context.Event.GetError(context.Client.Configuration.Resolver.GetJsonSerializer());
48+
var error = context.Event.GetError(serializer);
4249
if (error != null && error.Code == "404") {
4350
context.Event.Type = Event.KnownTypes.NotFound;
44-
context.Event.Source = requestInfo.GetFullPath(includeHttpMethod: true, includeHost: false, includeQueryString: false);
51+
context.Event.Source = ri.GetFullPath(includeHttpMethod: true, includeHost: false, includeQueryString: false);
4552
}
4653

47-
context.Event.AddRequestInfo(requestInfo);
54+
context.Event.AddRequestInfo(ri);
4855
}
4956

5057
private static IPrincipal GetPrincipal(HttpRequestMessage request) {

0 commit comments

Comments
 (0)