5
5
using System . Reflection ;
6
6
using System . Security . Principal ;
7
7
using System . Threading ;
8
+ using System . Web . Http . Controllers ;
8
9
using Exceptionless . Dependency ;
9
10
using Exceptionless . Plugins ;
10
11
using Exceptionless . Extensions ;
@@ -15,23 +16,14 @@ namespace Exceptionless.WebApi {
15
16
[ Priority ( 90 ) ]
16
17
internal class ExceptionlessWebApiPlugin : IEventPlugin {
17
18
public void Run ( EventPluginContext context ) {
18
- if ( ! context . ContextData . ContainsKey ( "HttpActionContext" ) )
19
- return ;
20
-
21
19
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
+
22
24
if ( actionContext == null )
23
25
return ;
24
26
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
27
var ri = context . Event . GetRequestInfo ( serializer ) ;
36
28
if ( ri != null )
37
29
return ;
@@ -54,9 +46,19 @@ public void Run(EventPluginContext context) {
54
46
context . Event . AddRequestInfo ( ri ) ;
55
47
}
56
48
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
+
57
59
private static IPrincipal GetPrincipal ( HttpRequestMessage request ) {
58
60
if ( request == null )
59
- throw new ArgumentNullException ( "request" ) ;
61
+ return Thread . CurrentPrincipal ;
60
62
61
63
const string RequestContextKey = "MS_RequestContext" ;
62
64
@@ -73,7 +75,6 @@ private static IPrincipal GetPrincipal(HttpRequestMessage request) {
73
75
}
74
76
75
77
var principal = _principalGetAccessor ( context ) as IPrincipal ;
76
-
77
78
return principal ?? Thread . CurrentPrincipal ;
78
79
}
79
80
0 commit comments