This is a custom built reusable SessionManager Class to keep track of all the used session variables in a web application. You don’t need to remember them or type them by hand every time you need to access them and with typed dataset rows, life becomes allot more easier but above all there is no need of casting where ever you access a session object, believe me, that’s the best part.
public class SessionManager
{
//set the login status in session for the current user.
public static void setIsLoggedIn(bool value)
{
//value is true when the the usser logs in
HttpContext.Current.Session[“isLoggedIn”] = value;
//value is false when the the usser logsout
if (!value)
HttpContext.Current.Session[“isLoggedIn”] = null;
}
//check to see if the user is logged in
public static bool isLoggedIn()
{
if (HttpContext.Current.Session[“isLoggedIn”].Equals(null))
return false;
else
return true;
}
//set current employee in the sessoin
public static void setCurrentEmployee(dsEmployees.EmployeesRow value)
{
HttpContext.Current.Session[“Employee”] = value;
if (value != null)
{
//following variables are set separately to be used in bindable control like
//GridView, DataList etc..
HttpContext.Current.Session[“EmployeeId”] = value.EmployeeId;
HttpContext.Current.Session[“Name”] = value.FirstName + ” “ +
value.MiddleName + ” “+ value.LastName;
}
}
//get current employee fro the session
public static dsEmployees.EmployeesRow getCurrentEmployee()
{
return(dsEmployees.EmployeesRow)HttpContext.Current.Session[“Employee”];
}
//bonus functions – to be used on the pages where it is neccessary for the users to login
public static void setRedirectUrl(stringredirectURL)
{
HttpContext.Current.Session[“redirectURL”] = redirectURL;
}
//bonus functions – once the users log in, they will be redirected back to the previously
//stored url or redirected to the default page
public static stringgetRedirectUrl()
{
if (object.Equals(HttpContext.Current.Session[“redirectURL”], null))
return “myaccount.aspx”;
else
return HttpContext.Current.Session[“redirectURL”].ToString();
}
}
Analyze one thing that setCurrentEmployee is taking the entire typed dataset’s row of the currently logged in user. This practice helps a great deal to quickly access any value from the currently logged in user’s details instead of hitting the database again and again and of course, typed datarow helps you avoid any mistakes in writing field names 🙂
Here is a sample code to set and get login sessoin variables.
{
Page.Validate();
if(!Page.IsValid)
return;
dsEmployeesTableAdapters.EmployeesTableAdapter ta = new dsEmployeesTableAdapters.EmployeesTableAdapter();
dsEmployees.EmployeesDataTable dt = ta.Login(txtuid.Text.Trim(), txtPassword.Text.Trim());
if(dt.Rows.Count > 0)
{
dsEmployees.EmployeesRow row = dt[0];
//set current user as logged in
Response.Redirect(rediectURL);
}
return;
}
Now it is very easy to check if the user is logged in.
//check if the user is logged in
if (!EmployeeSession.isLoggedIn())
{
//store current url and redirect to the login page
stringurl = Request.Url.AbsoluteUri.ToString();
EmployeeSession.setRedirectUrl(url);
Response.Redirect(“login.aspx”);
}
Accessing the data of the currently logged in user is piece of cake now.
//check if the user is logged in
if (EmployeeSession.isLoggedIn())
{
//display the name of the user from the session object
lblName.Text = SessionManager.getCurrentEmployee().Name;
}
Hope it helps, if you need more on it, drop me a few lines.
Cheers –
2 Steps Solutions
Re-engineering your business