Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.Globalization;
- namespace FormsAuthenticateProject
- {
- public class DBConnLayer
- {
- SqlConnection conn;
- SqlCommand cmd;
- DataSet ds;
- SqlDataAdapter dwas;
- SqlDataReader dr;
- public DBConnLayer() //called a constructor .. notice no return point
- {
- Initialize();
- }
- protected void Initialize()
- {
- cmd = new SqlCommand();
- string DBC = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
- conn = new SqlConnection(DBC);
- }
- private int? getUserRoleBySessionID(string SessionID)
- {
- int? iUserRole = null;
- cmd.Parameters.Clear();
- cmd.CommandText = "Get_User_Role_By_SessionID";
- cmd.Connection = conn;
- cmd.Parameters.AddWithValue("@SessionID", SessionID);
- cmd.CommandType = CommandType.StoredProcedure;
- try
- {
- conn.Open();
- cmd.Prepare();
- iUserRole = (int?)cmd.ExecuteScalar();
- return iUserRole;
- }
- catch (Exception e)
- {
- return 0;
- }
- finally { conn.Close(); }
- }
- public void userRoleRedirect()
- {
- DBConnLayer DBCL = new DBConnLayer();
- string DTFormat = "dd-MM-yyyy HH:mm:ss";
- DateTime ckDateTime = new DateTime();
- string strLoginUrl = "~/Account/Login.aspx";
- var Request = HttpContext.Current.Request;
- var Response = HttpContext.Current.Response;
- if (Response.Cookies["Session"] != null)
- {
- HttpCookie myCookie = Request.Cookies["Session"];
- DateTime.TryParseExact(myCookie.Values["LogoutTimer"], DTFormat,
- CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out ckDateTime);
- if (myCookie["ID"] != null)
- {
- int? iUserRole = getUserRoleBySessionID(Request.Cookies["Session"]["ID"].ToString());
- if (DBCL.compareSessionID(myCookie.Values["ID"]))
- {
- if (ckDateTime.CompareTo(DateTime.Now) < 0 || Request.Cookies["Session"]["logoutTimer"] == null)
- {
- Response.Redirect(strLoginUrl);
- }
- else if (ckDateTime.CompareTo(DateTime.Now) > 0)
- {
- switch (iUserRole)
- {
- case 1:
- Response.Redirect("~/Customer/default.aspx");
- break;
- case 2:
- Response.Redirect("~/Administration/default.aspx");
- break;
- default:
- Response.Redirect(strLoginUrl);
- break;
- }
- }
- }
- }
- else if (!Request.Url.AbsolutePath.Contains("/Account/"))
- {
- Response.Redirect(strLoginUrl);
- }
- }
- }
- public string encodeToSHA1(string strunEncoded){
- string strencoded = string.Empty;
- strencoded = FormsAuthentication.HashPasswordForStoringInConfigFile(strunEncoded, "SHA1");
- return strencoded;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement