@@ -114,6 +114,8 @@ public Common()
114
114
//Log.Instance.Info("AD Username: " + ApiServicesHelper.ApiConfiguration.Settings["API_AD_USERNAME"]);
115
115
//Log.Instance.Info("AD Password: ********"); // Hide API_AD_PASSWORD from logs
116
116
117
+
118
+
117
119
// Check if the request is Stateless
118
120
if ( Convert . ToBoolean ( ApiServicesHelper . ApiConfiguration . Settings [ "API_STATELESS" ] ) )
119
121
{
@@ -122,7 +124,7 @@ public Common()
122
124
if ( userPricipalCache . hasData )
123
125
{
124
126
isAuthenticated = true ;
125
- var userPricipalCacheDeserialized = Utility . JsonDeserialize_IgnoreLoopingReference < ExpandoObject > ( userPricipalCache . data . ToString ( ) ) ;
127
+ var userPricipalCacheDeserialized = Utility . JsonDeserialize_IgnoreLoopingReference < ExpandoObject > ( userPricipalCache . data . ToString ( ) ) ;
126
128
UserPrincipal = userPricipalCacheDeserialized == null ? null : userPricipalCacheDeserialized ;
127
129
Log . Instance . Info ( "Authentication retrieved from Cache" ) ;
128
130
}
@@ -137,7 +139,7 @@ public Common()
137
139
UserPrincipal = ApiServicesHelper . ActiveDirectory . CreateAPIUserPrincipalObject ( UserPrincipal ) ;
138
140
139
141
// Set the cache to expire at midnight
140
- if ( ApiServicesHelper . CacheD . Store_BSO < dynamic > ( "API" , "Common" , "Authenticate" , NetworkIdentity , Utility . JsonSerialize_IgnoreLoopingReference ( UserPrincipal ) , DateTime . Today . AddDays ( 1 ) ) )
142
+ if ( ApiServicesHelper . CacheD . Store_BSO < dynamic > ( "API" , "Common" , "Authenticate" , NetworkIdentity , Utility . JsonSerialize_IgnoreLoopingReference ( UserPrincipal ) , DateTime . Today . AddDays ( 1 ) ) )
141
143
{
142
144
Log . Instance . Info ( "Authentication stored in Cache" ) ;
143
145
}
@@ -152,26 +154,36 @@ public Common()
152
154
// Call the SessionID to initiate the Session
153
155
Log . Instance . Info ( "Session ID: " + context . Session . Id ) ;
154
156
155
- isAuthenticated = AuthenticateByType ( ) ;
156
-
157
- // Initiate a new Session only when authentication works.
158
- if ( isAuthenticated != null )
157
+ var user = context . Session . GetString ( UserPrincipal_Container ) ;
158
+ if ( user == null )
159
159
{
160
- // Save the serialized userPrincipal in the Session
161
- //context.Session[UserPrincipal_Container] = Utility.JsonSerialize_IgnoreLoopingReference(UserPrincipal);
162
- Log . Instance . Info ( "Authentication stored in Session" ) ;
160
+ isAuthenticated = AuthenticateByType ( ) ;
161
+
162
+ // Initiate a new Session only when authentication works.
163
+ if ( isAuthenticated != null )
164
+ {
165
+ // Save the serialized userPrincipal in the Session
166
+
167
+ //set userprincipal to be a smaller object
168
+ UserPrincipal = ApiServicesHelper . ActiveDirectory . CreateAPIUserPrincipalObject ( UserPrincipal ) ;
169
+ string upString = Utility . JsonSerialize_IgnoreLoopingReference ( UserPrincipal ) ;
170
+ context . Session . SetString ( UserPrincipal_Container , upString ) ;
171
+ Log . Instance . Info ( "Authentication stored in Session" ) ;
172
+ }
173
+
163
174
}
164
- }
165
- else
166
- {
167
- isAuthenticated = true ;
168
175
169
- // Call the SessionID to initiate the Session
170
- Log . Instance . Info ( "Session ID: " + context . Session . Id ) ;
176
+ else
177
+ {
178
+ isAuthenticated = true ;
179
+
180
+ // Call the SessionID to initiate the Session
181
+ Log . Instance . Info ( "Session ID: " + context . Session . Id ) ;
182
+ // Deserialise userPrincipal from Session
183
+ UserPrincipal = Utility . JsonDeserialize_IgnoreLoopingReference ( context . Session . GetString ( UserPrincipal_Container ) ) ;
184
+ Log . Instance . Info ( "Authentication retrieved from Session" ) ;
185
+ }
171
186
172
- // Deserialise userPrincipal from Session
173
- //UserPrincipal = Utility.JsonDeserialize_IgnoreLoopingReference((string)(context.Session[UserPrincipal_Container]));
174
- Log . Instance . Info ( "Authentication retrieved from Session" ) ;
175
187
}
176
188
}
177
189
@@ -252,7 +264,8 @@ public Common()
252
264
else
253
265
{
254
266
//if account is enabled
255
- if ( UserPrincipal . Enabled ) {
267
+ if ( UserPrincipal . Enabled )
268
+ {
256
269
return true ;
257
270
}
258
271
else
@@ -408,7 +421,8 @@ internal async Task returnResponseAsync(HttpContext context, string message, Can
408
421
throw new TaskCanceledException ( ) ;
409
422
}
410
423
411
- if ( context . Response . HasStarted ) {
424
+ if ( context . Response . HasStarted )
425
+ {
412
426
sourceToken . Cancel ( true ) ;
413
427
414
428
if ( sourceToken . IsCancellationRequested )
@@ -447,24 +461,25 @@ internal Cookie CheckCookie(string SessionCookieName, HttpContext httpContext)
447
461
//httpContext.Request.Headers.Add("Cookie", "session=\"84c2f0b319460ee991924908198d46795049c83f1ebdfcaf90bd899c8d9d0bd2\";");
448
462
449
463
Cookie sessionCookie = new Cookie ( ) ;
450
-
464
+
451
465
if ( ! string . IsNullOrEmpty ( SessionCookieName ) )
452
- {
453
- //need to create a cookie using the value and the SessionCookieName
454
- string testSessionCookieValue = httpContext . Request . Cookies [ SessionCookieName ] ;
466
+ {
467
+ //need to create a cookie using the value and the SessionCookieName
468
+ string testSessionCookieValue = httpContext . Request . Cookies [ SessionCookieName ] ;
455
469
456
- if ( ! string . IsNullOrEmpty ( testSessionCookieValue ) )
457
- {
458
- sessionCookie . Name = SessionCookieName ;
459
- sessionCookie . Value = testSessionCookieValue ;
460
- }
470
+ if ( ! string . IsNullOrEmpty ( testSessionCookieValue ) )
471
+ {
472
+ sessionCookie . Name = SessionCookieName ;
473
+ sessionCookie . Value = testSessionCookieValue ;
474
+ }
461
475
}
462
476
return sessionCookie ;
463
477
}
464
478
465
479
internal void GatherTraceInformation ( dynamic apiRequest , Trace trace )
466
480
{
467
- if ( ApiServicesHelper . CacheConfig . API_CACHE_TRACE_ENABLED ) {
481
+ if ( ApiServicesHelper . CacheConfig . API_CACHE_TRACE_ENABLED )
482
+ {
468
483
//gather trace information
469
484
trace . TrcParams = MaskParameters ( apiRequest . parameters . ToString ( ) ) ;
470
485
trace . TrcIp = apiRequest . ipAddress ;
@@ -476,7 +491,7 @@ internal void GatherTraceInformation(dynamic apiRequest, Trace trace)
476
491
trace . TrcUsername = apiRequest . userPrincipal . SamAccountName . ToString ( ) ;
477
492
}
478
493
}
479
- }
494
+ }
480
495
}
481
496
482
497
/// <summary>
0 commit comments