@@ -88,9 +88,9 @@ public class Common
88
88
/// Authenticate the user in the context
89
89
/// </summary>
90
90
/// <param name="context"></param>
91
- internal bool Authenticate ( ref HttpContext context )
91
+ internal bool ? Authenticate ( ref HttpContext context )
92
92
{
93
- bool isAuthenticated = false ;
93
+ bool ? isAuthenticated = null ;
94
94
Log . Instance . Info ( "Stateless: " + API_STATELESS ) ;
95
95
96
96
// Get the username if any
@@ -129,10 +129,14 @@ internal bool Authenticate(ref HttpContext context)
129
129
{
130
130
isAuthenticated = AuthenticateByType ( ) ;
131
131
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 )
134
134
{
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
+ }
136
140
}
137
141
}
138
142
}
@@ -146,9 +150,13 @@ internal bool Authenticate(ref HttpContext context)
146
150
147
151
isAuthenticated = AuthenticateByType ( ) ;
148
152
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
+ }
152
160
}
153
161
else
154
162
{
@@ -171,7 +179,7 @@ internal bool Authenticate(ref HttpContext context)
171
179
/// <summary>
172
180
/// Authenticate the user by the relative Authentication Type
173
181
/// </summary>
174
- private bool AuthenticateByType ( )
182
+ private bool ? AuthenticateByType ( )
175
183
{
176
184
string [ ] AuthenticationTypeAllowed = new string [ ]
177
185
{
@@ -207,7 +215,7 @@ private bool AuthenticateByType()
207
215
/// <summary>
208
216
/// Process Windows Authentication
209
217
/// </summary>
210
- private bool WindowsAuthentication ( )
218
+ private bool ? WindowsAuthentication ( )
211
219
{
212
220
// Override userPrincipal for security
213
221
UserPrincipal = null ;
@@ -216,13 +224,13 @@ private bool WindowsAuthentication()
216
224
if ( string . IsNullOrEmpty ( NetworkUsername ) )
217
225
{
218
226
Log . Instance . Fatal ( "Undefined Network Username" ) ;
219
- return false ;
227
+ return null ;
220
228
}
221
229
222
230
if ( String . IsNullOrEmpty ( API_AD_DOMAIN ) )
223
231
{
224
232
Log . Instance . Fatal ( "Undefined AD Domain" ) ;
225
- return false ;
233
+ return null ;
226
234
}
227
235
228
236
// Query AD
@@ -245,22 +253,22 @@ private bool WindowsAuthentication()
245
253
if ( UserPrincipal == null )
246
254
{
247
255
Log . Instance . Fatal ( "Undefined User Principal against AD" ) ;
248
- return false ;
256
+ return null ;
249
257
}
250
258
return true ;
251
259
}
252
260
catch ( Exception e )
253
261
{
254
262
Log . Instance . Fatal ( "Unable to connect/query AD" ) ;
255
263
Log . Instance . Fatal ( e ) ;
256
- return false ;
264
+ return null ;
257
265
}
258
266
}
259
267
260
268
/// <summary>
261
269
/// Process Anonymous Authentication
262
270
/// </summary>
263
- private bool AnonymousAuthentication ( )
271
+ private bool ? AnonymousAuthentication ( )
264
272
{
265
273
// Override userPrincipal for security
266
274
UserPrincipal = null ;
@@ -270,7 +278,7 @@ private bool AnonymousAuthentication()
270
278
/// <summary>
271
279
/// Process Any Authentication
272
280
/// </summary>
273
- private bool AnyAuthentication ( )
281
+ private bool ? AnyAuthentication ( )
274
282
{
275
283
// Override userPrincipal for security
276
284
UserPrincipal = null ;
@@ -302,7 +310,7 @@ private bool AnyAuthentication()
302
310
{
303
311
Log . Instance . Fatal ( "Unable to connect/query AD" ) ;
304
312
Log . Instance . Fatal ( e ) ;
305
- return false ;
313
+ return null ;
306
314
}
307
315
}
308
316
0 commit comments