Session timeout is the worst nightmare of a developer especially if you do not have much experience in coding but senior enough to design an small application which becomes a mammoth in few months.
So to all the mid-level experience guy, here is the key to success, do not make session variables in code behind, instead, create separate c# file either in app_code or in a separate project which you can call Utils as it will act like your utility file and create a public property in it which will call your DB whenever your session is null like following.
So let's create a never ending session for the c#,
Here is the scenario, suppose you have a user data-table coming from database which is on another project/DB and you have to get it through AD authentication or let's imagine it's a heavy DB call so calling, again and again, will reduce the app performance and that's why we use session which is again a non-performing thing but still better than DB chat.
Here is your user entity:
Create the property like below in your utility class, where GetRoleInfo will get you the roles, user details from DB.
And use it while declaring an object for it like below:
Happy Coding with your infinite session, as whenever your session value will be null, it will fetch it for you.
So to all the mid-level experience guy, here is the key to success, do not make session variables in code behind, instead, create separate c# file either in app_code or in a separate project which you can call Utils as it will act like your utility file and create a public property in it which will call your DB whenever your session is null like following.
So let's create a never ending session for the c#,
Here is the scenario, suppose you have a user data-table coming from database which is on another project/DB and you have to get it through AD authentication or let's imagine it's a heavy DB call so calling, again and again, will reduce the app performance and that's why we use session which is again a non-performing thing but still better than DB chat.
Here is your user entity:
public class UserRoleInfo { public string FullName { get; set; } public string Username { get; set; } public bool? IsManager { get; set; } public string RoleName { get; set; } public int? LoginId { get; set; } public int? EmployeeID { get; set; } public int? RoleId { get; set; } } |
Create the property like below in your utility class, where GetRoleInfo will get you the roles, user details from DB.
/// <summary> /// UserSession property /// </summary> public UserRoleInfo UserSession { get { UserRoleInfo session = (UserRoleInfo)HttpContext.Current.Session["userRoleInfo"]; if (session == null) { session = GetRoleInfo(HttpContext.Current.User.Identity.Name.Remove(0, 8)); HttpContext.Current.Session["userRoleInfo"] = session; } return session; } } |
And use it while declaring an object for it like below:
userInfoHelper = new UserInfoHelper(); if ((userInfoHelper.UserSession.RoleId == (int)UserInfo.Roles.User) || (userInfoHelper.UserSession.RoleId == (int)UserInfo.Roles.Manager)) { ddlEmployee.Enabled = false; } |
Happy Coding with your infinite session, as whenever your session value will be null, it will fetch it for you.
Comments
Post a Comment
Important - Make sure to click the Notify Me check-box below the comment to be notified of follow up comments and replies.