@@ -88,9 +88,9 @@ public class Common
8888 /// Authenticate the user in the context
8989 /// </summary>
9090 /// <param name="context"></param>
91- internal bool Authenticate ( ref HttpContext context )
91+ internal bool ? Authenticate ( ref HttpContext context )
9292 {
93- bool isAuthenticated = false ;
93+ bool ? isAuthenticated = null ;
9494 Log . Instance . Info ( "Stateless: " + API_STATELESS ) ;
9595
9696 // Get the username if any
@@ -129,10 +129,14 @@ internal bool Authenticate(ref HttpContext context)
129129 {
130130 isAuthenticated = AuthenticateByType ( ) ;
131131
132- // Set the cache to expire at midnight
133- if ( MemCacheD . Store_BSO < dynamic > ( "API" , "Common" , "Authenticate" , NetworkIdentity , UserPrincipal , DateTime . Today . AddDays ( 1 ) ) )
132+ // Store the cache only when authentication works.
133+ if ( isAuthenticated != null )
134134 {
135- Log . Instance . Info ( "Authentication stored in Cache" ) ;
135+ // Set the cache to expire at midnight
136+ if ( MemCacheD . Store_BSO < dynamic > ( "API" , "Common" , "Authenticate" , NetworkIdentity , UserPrincipal , DateTime . Today . AddDays ( 1 ) ) )
137+ {
138+ Log . Instance . Info ( "Authentication stored in Cache" ) ;
139+ }
136140 }
137141 }
138142 }
@@ -146,9 +150,13 @@ internal bool Authenticate(ref HttpContext context)
146150
147151 isAuthenticated = AuthenticateByType ( ) ;
148152
149- // Save the serialized userPrincipal in the Session
150- context . Session [ UserPrincipal_Container ] = Utility . JsonSerialize_IgnoreLoopingReference ( UserPrincipal ) ;
151- Log . Instance . Info ( "Authentication stored in Session" ) ;
153+ // Initiate a new Session only when authentication works.
154+ if ( isAuthenticated != null )
155+ {
156+ // Save the serialized userPrincipal in the Session
157+ context . Session [ UserPrincipal_Container ] = Utility . JsonSerialize_IgnoreLoopingReference ( UserPrincipal ) ;
158+ Log . Instance . Info ( "Authentication stored in Session" ) ;
159+ }
152160 }
153161 else
154162 {
@@ -171,7 +179,7 @@ internal bool Authenticate(ref HttpContext context)
171179 /// <summary>
172180 /// Authenticate the user by the relative Authentication Type
173181 /// </summary>
174- private bool AuthenticateByType ( )
182+ private bool ? AuthenticateByType ( )
175183 {
176184 string [ ] AuthenticationTypeAllowed = new string [ ]
177185 {
@@ -207,7 +215,7 @@ private bool AuthenticateByType()
207215 /// <summary>
208216 /// Process Windows Authentication
209217 /// </summary>
210- private bool WindowsAuthentication ( )
218+ private bool ? WindowsAuthentication ( )
211219 {
212220 // Override userPrincipal for security
213221 UserPrincipal = null ;
@@ -216,13 +224,13 @@ private bool WindowsAuthentication()
216224 if ( string . IsNullOrEmpty ( NetworkUsername ) )
217225 {
218226 Log . Instance . Fatal ( "Undefined Network Username" ) ;
219- return false ;
227+ return null ;
220228 }
221229
222230 if ( String . IsNullOrEmpty ( API_AD_DOMAIN ) )
223231 {
224232 Log . Instance . Fatal ( "Undefined AD Domain" ) ;
225- return false ;
233+ return null ;
226234 }
227235
228236 // Query AD
@@ -245,22 +253,22 @@ private bool WindowsAuthentication()
245253 if ( UserPrincipal == null )
246254 {
247255 Log . Instance . Fatal ( "Undefined User Principal against AD" ) ;
248- return false ;
256+ return null ;
249257 }
250258 return true ;
251259 }
252260 catch ( Exception e )
253261 {
254262 Log . Instance . Fatal ( "Unable to connect/query AD" ) ;
255263 Log . Instance . Fatal ( e ) ;
256- return false ;
264+ return null ;
257265 }
258266 }
259267
260268 /// <summary>
261269 /// Process Anonymous Authentication
262270 /// </summary>
263- private bool AnonymousAuthentication ( )
271+ private bool ? AnonymousAuthentication ( )
264272 {
265273 // Override userPrincipal for security
266274 UserPrincipal = null ;
@@ -270,7 +278,7 @@ private bool AnonymousAuthentication()
270278 /// <summary>
271279 /// Process Any Authentication
272280 /// </summary>
273- private bool AnyAuthentication ( )
281+ private bool ? AnyAuthentication ( )
274282 {
275283 // Override userPrincipal for security
276284 UserPrincipal = null ;
@@ -302,7 +310,7 @@ private bool AnyAuthentication()
302310 {
303311 Log . Instance . Fatal ( "Unable to connect/query AD" ) ;
304312 Log . Instance . Fatal ( e ) ;
305- return false ;
313+ return null ;
306314 }
307315 }
308316
0 commit comments