📘 AllSessionKeys.cs Documentation
Module:
IrbyImaging.UI.Helpers
File:AllSessionKeys.cs
Purpose: Centralized definitions of session‐state keys for the Portal application.
📑 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Session Keys Reference Table
- Usage & Code Examples
- UML Class Diagram
- Conclusion
1. Overview & Purpose
The AllSessionKeys class is a static helper that centralizes all the string keys used to store and retrieve values in the ASP.NET Session. By defining these keys in one place, you:
- 🚀 Avoid “magic strings” scattered across the codebase
- 🔒 Ensure consistency and minimize typos when accessing session data
- 🛠️ Simplify maintenance—add, modify, or remove keys in one location
namespace IrbyImaging.UI.Helpers
{
public static class AllSessionKeys
{
public const string RoleId = "Portal.RoleId";
public const string UserName = "Portal.UserName";
public const string LangId = "Portal.LangId";
public const string UserId = "Portal.UserId";
public const string IsFirstLogin = "Portal.IsFirstLogin";
}
}
2. Dependencies & Technologies
| Technology | Version / Notes |
|---|---|
| .NET Framework | 4.x / .NET Core / .NET 5+ (any ASP.NET flavor) |
| ASP.NET Session | Relies on HttpContext.Session or HttpContext.Current.Session |
| C# Language | 6.0+ |
Note: No external NuGet packages or third‑party libraries are required—this is pure .NET code.
3. Key Functional Areas
-
Session Key Management
- Definition: Constant strings representing session entries
- Access: Read‑only at compile time—cannot be modified at runtime
-
Namespace Organization
- Placed under
IrbyImaging.UI.Helpersto indicate its utility role in the UI layer
- Placed under
4. Session Keys Reference Table
| Constant Name | Key String | Description |
|---|---|---|
| RoleId | "Portal.RoleId" |
Numeric or GUID identifier for the user’s role. |
| UserName | "Portal.UserName" |
The display or login name of the current user. |
| LangId | "Portal.LangId" |
Identifier for the user’s preferred language. |
| UserId | "Portal.UserId" |
Primary key of the authenticated user. |
| IsFirstLogin | "Portal.IsFirstLogin" |
Boolean flag: true if this is the user’s first login. |
5. Usage & Code Examples
5.1 Storing Values in Session
// In an ASP.NET Controller or Page
HttpContext.Current.Session[AllSessionKeys.UserId] = currentUser.Id;
HttpContext.Current.Session[AllSessionKeys.UserName] = currentUser.UserName;
HttpContext.Current.Session[AllSessionKeys.RoleId] = currentUser.RoleId;
HttpContext.Current.Session[AllSessionKeys.LangId] = currentUser.LanguageId;
HttpContext.Current.Session[AllSessionKeys.IsFirstLogin] = currentUser.IsFirstLogin;
5.2 Retrieving Values from Session
// Safely retrieve with null-checking
var userIdObj = HttpContext.Current.Session[AllSessionKeys.UserId];
if (userIdObj is int userId)
{
// Use userId ...
}
// Shorthand with C# 7.0 pattern matching
if (HttpContext.Current.Session[AllSessionKeys.RoleId] is Guid roleId)
{
// Use roleId ...
}
5.3 Clearing or Removing Session Keys
// Remove a single key
HttpContext.Current.Session.Remove(AllSessionKeys.IsFirstLogin);
// Clear entire session
HttpContext.Current.Session.Clear();
6. UML Class Diagram
classDiagram
direction LR
class AllSessionKeys {
<<static>>
+string RoleId
+string UserName
+string LangId
+string UserId
+string IsFirstLogin
}
- Static class: cannot be instantiated
- Fields:
const stringvalues only
7. Conclusion
🎯 AllSessionKeys.cs is a small but crucial helper in the IrbyImaging portal, providing a single source of truth for all session variable names. This approach:
- 🎨 Promotes clean, maintainable code
- ✅ Prevents typos and mismatched session keys
- 🔄 Eases future updates when adding or renaming session entries
Next Steps:
- If you introduce new session variables, simply add a new
public const stringto this file.- Consider grouping keys by feature or module if the list grows significantly.
Happy coding! 🚀