Search...

Tuesday, February 14, 2012

Safely Storing Business Objects in Session State


using System;
using System.Collections.Generic;

/// <summary>
/// Summary description for Employee
/// </summary>
public class Employee
{
public Employee()
{
//
// TODO: Add constructor logic here
//
}
    public String DepartmentId { get; set; }
    public String EmployeeId { get; set; }
    public String EmployeeName { get; set; }
    public String LoginId { get; set; }
    public String Password { get; set; }
    public DateTime LoginTime { get; set; }

    public List<Employee> EmployeeData()
    {
        List<Employee> emplist = new List<Employee>();
        emplist.Add(new Employee { DepartmentId = "1", EmployeeId = "1", EmployeeName = "Kaushik Mistry", LoginId = "001", Password = "123" });
        emplist.Add(new Employee { DepartmentId = "1", EmployeeId = "2", EmployeeName = "Sunil Kumar", LoginId = "002", Password = "123" });
        emplist.Add(new Employee { DepartmentId = "2", EmployeeId = "3", EmployeeName = "Rajesh Singh", LoginId = "003", Password = "123" });
        return emplist;
    }
}

*********************************************************************************

using System;

/// <summary>
/// Summary description for SmartSession
/// </summary>
public class SmartSession
{
public SmartSession()
{
//
// TODO: Add constructor logic here
//
}

    public String DepartmentId { get; set; }
    public String EmployeeId { get; set; }
    public String EmployeeName { get; set; }
    public String LoginId { get; set; }
    public DateTime LoginTime { get; set; }
}

********************************************************************************* 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Safely Storing Business Objects in Session State</title>
    <style type="text/css">
    #SignUp
    {
    border:1px solid #018ff0;
    height:150px;
    width:500px;
    position:absolute;
    top:50%;
    left:50%;
    margin-top:-95px;
    margin-left:-250px;
    }
    p
    {
    text-align:center;
    border-bottom:1px solid #018ff0;
    }
    table
    {
    margin:0px auto;
    }
  
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div id="SessionData" runat="server"></div>
    <div id="SignUp">
    <p>Sign Up</p>
    <table>
    <tr>
    <td>Login Id:</td>
    <td><asp:TextBox ID="txtLoginId" runat="server" Text="001"></asp:TextBox></td>
    </tr>
    <tr>
    <td>Password:</td>
    <td><asp:TextBox ID="txtPassword" runat="server" Text="123"></asp:TextBox></td>
    </tr>
    <tr>
    <td></td>
    <td><asp:Button ID="butLogin" runat="server" Text="Login" OnClick="butLogin_Click"/></td>
    </tr>
    </table>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        butLogin.Click += new EventHandler(butLogin_Click);
    }

    protected void butLogin_Click(object sender, EventArgs e)
    {
        Boolean status = false;
        Employee employee=new Employee();
        Employee empData = employee.EmployeeData().SingleOrDefault(q=>q.LoginId.Equals(txtLoginId.Text) && q.Password.Equals(txtPassword.Text));
        if (empData != null)
        {
            SmartSession session = new SmartSession();
            session.DepartmentId = empData.DepartmentId;
            session.EmployeeId = empData.EmployeeId;
            session.EmployeeName = empData.EmployeeName;
            session.LoginId = empData.LoginId;
            session.LoginTime = DateTime.Now;
            Session["LoginDataObjects"] = session;
            status = true;
        }
        else
        {
            status = false;
            SessionData.InnerHtml = "User is not authenticated !";
        }

        if (status)
        {
            Response.Redirect("~/Home.aspx",false);
        }
    }
    
}

********************************************************************************* 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Home Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="SessionData" runat="server"></div>

    </form>
</body>
</html>


using System;
using System.Text;

public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["LoginDataObjects"] != null)
        {
            SmartSession sessionObject = (SmartSession)Session["LoginDataObjects"];
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("Department Id : {0}<br/>", sessionObject.DepartmentId));
            sb.Append(String.Format("Employee Id   : {0}<br/>", sessionObject.EmployeeId));
            sb.Append(String.Format("Employee Name : {0}<br/>", sessionObject.EmployeeName));
            sb.Append(String.Format("Login Id      : {0}<br/>", sessionObject.LoginId));
            sb.Append(String.Format("Login Time    : {0}", sessionObject.LoginTime.ToString()));
            SessionData.InnerHtml = sb.ToString();
        }
        else
        {
            SessionData.InnerHtml = "No session object is present !";
            Response.Redirect("~/default.aspx");
        }
    }
}



No comments: