Advertisement
Guest User

DBConnLayer.aspx

a guest
Mar 4th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Configuration;
  9. using System.Globalization;
  10.  
  11. namespace FormsAuthenticateProject
  12. {
  13.     public class DBConnLayer
  14.     {
  15.         SqlConnection conn;
  16.         SqlCommand cmd;
  17.         DataSet ds;
  18.         SqlDataAdapter dwas;
  19.         SqlDataReader dr;
  20.  
  21.         public DBConnLayer() //called a constructor .. notice no return point
  22.         {
  23.             Initialize();
  24.         }
  25.         protected void Initialize()
  26.         {
  27.             cmd = new SqlCommand();
  28.             string DBC = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
  29.             conn = new SqlConnection(DBC);
  30.         }
  31.  
  32.         private int? getUserRoleBySessionID(string SessionID)
  33.         {
  34.             int? iUserRole = null;
  35.  
  36.             cmd.Parameters.Clear();
  37.             cmd.CommandText = "Get_User_Role_By_SessionID";
  38.             cmd.Connection = conn;
  39.             cmd.Parameters.AddWithValue("@SessionID", SessionID);
  40.             cmd.CommandType = CommandType.StoredProcedure;
  41.  
  42.             try
  43.             {
  44.                 conn.Open();
  45.                 cmd.Prepare();
  46.                 iUserRole = (int?)cmd.ExecuteScalar();
  47.                 return iUserRole;
  48.             }
  49.             catch (Exception e)
  50.             {
  51.                 return 0;
  52.             }
  53.             finally { conn.Close(); }
  54.         }
  55.  
  56.         public void userRoleRedirect()
  57.         {
  58.             DBConnLayer DBCL = new DBConnLayer();
  59.  
  60.             string DTFormat = "dd-MM-yyyy HH:mm:ss";
  61.             DateTime ckDateTime = new DateTime();
  62.  
  63.             string strLoginUrl = "~/Account/Login.aspx";
  64.             var Request = HttpContext.Current.Request;
  65.             var Response = HttpContext.Current.Response;
  66.  
  67.             if (Response.Cookies["Session"] != null)
  68.             {
  69.                 HttpCookie myCookie = Request.Cookies["Session"];
  70.  
  71.                 DateTime.TryParseExact(myCookie.Values["LogoutTimer"], DTFormat,
  72.                 CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out ckDateTime);
  73.  
  74.                 if (myCookie["ID"] != null)
  75.                 {
  76.                     int? iUserRole = getUserRoleBySessionID(Request.Cookies["Session"]["ID"].ToString());
  77.  
  78.                     if (DBCL.compareSessionID(myCookie.Values["ID"]))
  79.                     {
  80.                         if (ckDateTime.CompareTo(DateTime.Now) < 0 || Request.Cookies["Session"]["logoutTimer"] == null)
  81.                         {
  82.                             Response.Redirect(strLoginUrl);
  83.                         }
  84.                         else if (ckDateTime.CompareTo(DateTime.Now) > 0)
  85.                         {
  86.                             switch (iUserRole)
  87.                             {
  88.                                 case 1:
  89.                                     Response.Redirect("~/Customer/default.aspx");
  90.                                     break;
  91.                                 case 2:
  92.                                     Response.Redirect("~/Administration/default.aspx");
  93.                                     break;
  94.                                 default:
  95.                                     Response.Redirect(strLoginUrl);
  96.                                     break;
  97.                             }
  98.                         }
  99.                     }
  100.                 }
  101.                 else if (!Request.Url.AbsolutePath.Contains("/Account/"))
  102.                 {
  103.                     Response.Redirect(strLoginUrl);
  104.                 }
  105.             }
  106.         }
  107.  
  108.         public string encodeToSHA1(string strunEncoded){
  109.             string strencoded = string.Empty;
  110.  
  111.             strencoded = FormsAuthentication.HashPasswordForStoringInConfigFile(strunEncoded, "SHA1");
  112.  
  113.             return strencoded;
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement