Skip to content

Commit 1263291

Browse files
committed
Refactor session management logic to use a single sessionEnabled variable for clarity
1 parent 035f837 commit 1263291

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/Program.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
builder.Services.AddRazorPages();
1414
var disableSession = Environment.GetEnvironmentVariable("DISABLE_SESSION");
15-
if (string.IsNullOrEmpty(disableSession) || disableSession.ToLower() != "true")
15+
var sessionEnabled = string.IsNullOrEmpty(disableSession) || disableSession.ToLower() != "true";
16+
if (sessionEnabled)
1617
{
1718
builder.Services.AddSession(options =>
1819
{
@@ -61,16 +62,19 @@
6162
app.UseStaticFiles();
6263
app.UseRouting();
6364
app.UseAuthorization();
64-
if (string.IsNullOrEmpty(disableSession) || disableSession.ToLower() != "true")
65+
if (sessionEnabled)
6566
{
6667
app.UseSession();
6768
}
6869
app.Use(async (context, next) =>
6970
{
7071
var path = context.Request.Path;
7172
var isAccountPage = path.StartsWithSegments("/Account");
72-
var isAuthenticated = context.Session.GetInt32("UserId").HasValue;
73-
73+
var isAuthenticated = false;
74+
if (sessionEnabled)
75+
{
76+
isAuthenticated = context.Session.GetInt32("UserId").HasValue;
77+
}
7478
if (!isAuthenticated && !isAccountPage && !path.StartsWithSegments("/Index"))
7579
{
7680
context.Response.Redirect("/Account/Login");
@@ -95,7 +99,8 @@
9599

96100
builder.Services.AddRazorPages();
97101
var disableSession = Environment.GetEnvironmentVariable("DISABLE_SESSION");
98-
if (string.IsNullOrEmpty(disableSession) || disableSession.ToLower() != "true")
102+
var sessionEnabled = string.IsNullOrEmpty(disableSession) || disableSession.ToLower() != "true";
103+
if (sessionEnabled)
99104
{
100105
builder.Services.AddSession(options =>
101106
{
@@ -144,16 +149,19 @@
144149
app.UseStaticFiles();
145150
app.UseRouting();
146151
app.UseAuthorization();
147-
if (string.IsNullOrEmpty(disableSession) || disableSession.ToLower() != "true")
152+
if (sessionEnabled)
148153
{
149154
app.UseSession();
150155
}
151156
app.Use(async (context, next) =>
152157
{
153158
var path = context.Request.Path;
154159
var isAccountPage = path.StartsWithSegments("/Account");
155-
var isAuthenticated = context.Session.GetInt32("UserId").HasValue;
156-
160+
var isAuthenticated = false;
161+
if (sessionEnabled)
162+
{
163+
isAuthenticated = context.Session.GetInt32("UserId").HasValue;
164+
}
157165
if (!isAuthenticated && !isAccountPage && !path.StartsWithSegments("/Index"))
158166
{
159167
context.Response.Redirect("/Account/Login");

0 commit comments

Comments
 (0)