Skip to content

Commit e0c0730

Browse files
committed
API authentication fixed when AD down or unreachable
1 parent 729f33a commit e0c0730

File tree

11 files changed

+31
-23
lines changed

11 files changed

+31
-23
lines changed

rls/API.Library.dll

512 Bytes
Binary file not shown.

rls/API.Library.pdb

0 Bytes
Binary file not shown.

src/API.Library/Entities/API.Common.cs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/API.Library/Entities/API.JSONRPC.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void ProcessRequest(HttpContext context)
6363
}
6464

6565
// Authenticate and append credentials
66-
if (!Authenticate(ref context))
66+
if (Authenticate(ref context) == false)
6767
{
6868
JSONRPC_Error error = new JSONRPC_Error { code = -32002 };
6969
ParseError(ref context, JSONRPC_Request.id, error);

src/API.Library/Entities/API.RESTful.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void ProcessRequest(HttpContext context)
4747
}
4848

4949
// Authenticate and append credentials
50-
if (!Authenticate(ref context))
50+
if (Authenticate(ref context) == false)
5151
{
5252
ParseError(ref context, HttpStatusCode.Unauthorized, "Invalid authentication");
5353
}

src/API.Library/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("4.1.2")]
35-
[assembly: AssemblyFileVersion("4.1.2")]
34+
[assembly: AssemblyVersion("4.1.3")]
35+
[assembly: AssemblyFileVersion("4.1.3")]
3636

3737
// Configure log4net using the Web.config file by default
3838
[assembly: log4net.Config.XmlConfigurator(Watch = true)]

test/API.Test/API.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@
7272
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
7373
</PropertyGroup>
7474
<ItemGroup>
75-
<Reference Include="API.Library, Version=4.1.2, Culture=neutral, processorArchitecture=MSIL">
75+
<Reference Include="API.Library, Version=4.1.3, Culture=neutral, processorArchitecture=MSIL">
7676
<SpecificVersion>False</SpecificVersion>
77-
<HintPath>..\packages\API.Library.4.1.2\API.Library.dll</HintPath>
77+
<HintPath>..\packages\API.Library.4.1.3\API.Library.dll</HintPath>
7878
</Reference>
7979
<Reference Include="Enyim.Caching, Version=2.16.0.0, Culture=neutral, PublicKeyToken=cec98615db04012e, processorArchitecture=MSIL">
8080
<HintPath>..\packages\EnyimMemcached.2.16.0\lib\net35\Enyim.Caching.dll</HintPath>
Binary file not shown.
71.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)