ProTech's home page

ProTech-Online.com

Microsoft c# ASPX code example for masterpage code behind.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.Security;
public partial class sans : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            checksession();
            SystemSessionID.Text = "System Session ID: " + Session.SessionID.ToString();
            SoftwareSessionID.Text = "Software Session ID: " + Session["SoftwareID"];
            if (Request.IsAuthenticated)
            {
                UserRole.Text = "User Role: " + Roles.GetRolesForUser()[0];
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void logoff()
    {
        try
        {
            SqlConnection myConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestConnectionString"].ToString());
            myConnection.Open();
            SqlCommand myCommand = new SqlCommand("delete_sessions", myConnection);
            myCommand.CommandType = System.Data.CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@userid", Context.User.Identity.Name);
            int x = myCommand.ExecuteNonQuery();
            Session.Abandon();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    public bool checksession()
    {
        try
        {
            Guid sessionid = new Guid();
            string userid = "";
            string clienthash = "";
            DateTime timeout = new DateTime();
            if (Request.IsAuthenticated)
            {
                SqlConnection myConnection = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestConnectionString"].ToString());
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand("get_sessions", myConnection);
                myCommand.CommandType = System.Data.CommandType.StoredProcedure;
                myCommand.Parameters.AddWithValue("@sessionid", Session["SoftwareID"]);
                SqlDataReader reader = myCommand.ExecuteReader();
                if (!reader.HasRows)
                {
                    Session.Abandon();
                    FormsAuthentication.SignOut();
                    Response.Redirect("~/Default.aspx");
                    Response.Write("No session!");
                    reader.Close();
                    myConnection.Close();
                    return false;
                }
                while (reader.Read())
                {
                    sessionid = (Guid)reader[0];
                    userid = reader[1].ToString();
                    clienthash = reader[2].ToString();
                    timeout = (DateTime)reader[3];
                }
                reader.Close();
                myConnection.Close();
                string tmpstr = Context.Request.UserAgent +
                            Context.Request.ServerVariables["REMOTE_ADDR"];
                string hashed = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpstr, "SHA1");
                if (userid == Context.User.Identity.Name && clienthash == hashed && timeout.CompareTo(DateTime.Now) > 0)
                {
                    myConnection.Open();
                    myCommand.CommandText = "update_sessions";
                    myCommand.Parameters.AddWithValue("@timeout", DateTime.Now.AddMinutes(30));
                    myCommand.ExecuteNonQuery();
                    Table cart_items = (Table)LoginView1.FindControl("tbCarts");
                    cart_items.Rows.Clear();
                    TableRow tmpTR = new TableRow();
                    TableCell tmpTD = new TableCell();
                    myCommand.CommandText = "get_carts";
                    myCommand.Parameters.RemoveAt("@timeout");
                    reader = myCommand.ExecuteReader();
                    if (reader.HasRows)
                    {
                        tmpTD.Text = "Item #";
                        tmpTR.Cells.Add(tmpTD);
                        tmpTD = new TableCell();
                        tmpTD.Text = "Price";
                        tmpTR.Cells.Add(tmpTD);
                        cart_items.Rows.Add(tmpTR);
                        tmpTR = new TableRow();
                        tmpTD = new TableCell();
                        tmpTD.Text = "________";
                        tmpTR.Cells.Add(tmpTD);
                        tmpTD = new TableCell();
                        tmpTD.Text = "________";
                        tmpTR.Cells.Add(tmpTD);
                        cart_items.Rows.Add(tmpTR);
                        string item_name;
                        decimal item_price;
                        decimal total_cost = 0;
                        while (reader.Read())
                        {
                            tmpTR = new TableRow();
                            tmpTD = new TableCell();
                            item_name = reader[0].ToString();
                            item_price = (decimal)reader[1];
                            total_cost += item_price;
                            tmpTD.Text = item_name;
                            tmpTR.Cells.Add(tmpTD);
                            tmpTD = new TableCell();
                            tmpTD.Text = "$" + item_price.ToString();
                            tmpTR.Cells.Add(tmpTD);
                            cart_items.Rows.Add(tmpTR);
                        }
                        tmpTR = new TableRow();
                        tmpTD = new TableCell();
                        tmpTD.Text = "--------";
                        tmpTR.Cells.Add(tmpTD);
                        tmpTD = new TableCell();
                        tmpTD.Text = "--------";
                        tmpTR.Cells.Add(tmpTD);
                        cart_items.Rows.Add(tmpTR);
                        tmpTR = new TableRow();
                        tmpTD = new TableCell();
                        tmpTD.Text = "Total:";
                        tmpTR.Cells.Add(tmpTD);
                        tmpTD = new TableCell();
                        tmpTD.Text = "$" + total_cost.ToString();
                        tmpTR.Cells.Add(tmpTD);
                        cart_items.Rows.Add(tmpTR);
                    }
                    else
                    {
                        tmpTD.Text = "Nothing in Cart :)";
                        tmpTR.Cells.Add(tmpTD);
                        cart_items.Rows.Add(tmpTR);
                    }
                    reader.Close();
                    myConnection.Close();
                    return true;
                }
                else
                {
                    Session.Abandon();
                    FormsAuthentication.SignOut();
                    Response.Redirect("~/Default.aspx");
                    Response.Write("Identity mismatch or session timed out!");
                }
            }
            return false;
        }
        catch (Exception ex)
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Redirect("~/Default.aspx");
            Response.Write("Undefined Exception!\n");
            Response.Write(ex.Message);
            return false;
        }
    }
}


Copyright © 2013 ProTechs-Online.com; All rights reserved.