Given the class is:
[Serializable]
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Save it to session:
protected void DoLogin()
{
Employee employee = new Employee()
{
Id = 1,
FirstName = "John",
LastName = "Smith"
};
// save it to session
Session.Add("Employee", employee);
}
Get it from session. Notice it needs to be cast to an "Employee":
protected Employee GetEmployee()
{
if (Session["Employee"] != null)
{
// needs to be cast
return (Employee)Session["Employee"];
}
else
{
return null;
}
}
I typically wrap this stuff into a property, for ease of use. You usually won't need an object like this in session though... Maybe what you're looking for is: ASP.NET MVC - Set custom IIdentity or IPrincipal