1) First Create SessionManger class
using System.Web;
using System.Collections.Generic;
namespace ManageSession
{
public class SessionManger
{
public static int AdminID
{
get
{
return (int)System.Web.HttpContext.Current.Session["AdminID"];
}
set { System.Web.HttpContext.Current.Session["AdminID"] = value; }
}
public static int UserType
{
get
{
return (int)System.Web.HttpContext.Current.Session["UserType"];
}
set { System.Web.HttpContext.Current.Session["UserType"] = value; }
}
public static int CompanyID
{
get
{
return (int)System.Web.HttpContext.Current.Session["CompanyID"];
}
set { System.Web.HttpContext.Current.Session["CompanyID"] = value; }
}
public static string UserTheme
{
get
{
return (string)System.Web.HttpContext.Current.Session["UserTheme"];
}
set { System.Web.HttpContext.Current.Session["UserTheme"] = value; }
}
public static int BranchID
{
get
{
return (int)System.Web.HttpContext.Current.Session["BranchID"];
}
set { System.Web.HttpContext.Current.Session["BranchID"] = value; }
}
}
}
using System.Web;
using System.Collections.Generic;
namespace ManageSession
{
public class SessionManger
{
public static int AdminID
{
get
{
return (int)System.Web.HttpContext.Current.Session["AdminID"];
}
set { System.Web.HttpContext.Current.Session["AdminID"] = value; }
}
public static int UserType
{
get
{
return (int)System.Web.HttpContext.Current.Session["UserType"];
}
set { System.Web.HttpContext.Current.Session["UserType"] = value; }
}
public static int CompanyID
{
get
{
return (int)System.Web.HttpContext.Current.Session["CompanyID"];
}
set { System.Web.HttpContext.Current.Session["CompanyID"] = value; }
}
public static string UserTheme
{
get
{
return (string)System.Web.HttpContext.Current.Session["UserTheme"];
}
set { System.Web.HttpContext.Current.Session["UserTheme"] = value; }
}
public static int BranchID
{
get
{
return (int)System.Web.HttpContext.Current.Session["BranchID"];
}
set { System.Web.HttpContext.Current.Session["BranchID"] = value; }
}
}
}
2) PreInitModule class
using System;
using System.Web;
using System.Web.UI;
using System.Web.Configuration;
using System.Text;
using System.Security.Cryptography;
using System.IO;
/// <summary>
/// Ashish Srivastava
/// 21 Sep 2017
///
/// </summary>
public class PreInitModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
//throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += HandlePreRequest;
}
void HandlePreRequest(object sender, EventArgs e)
{
var page = HttpContext.Current.CurrentHandler as System.Web.Mvc.MvcHandler;
if (page != null)
{
//page.PreInit += delegate
//{
if (HttpContext.Current.Request.Path.ToLower().Contains("/admin/"))
{
if (HttpContext.Current.Session["AdminID"] != null)
{
if (HttpContext.Current.Session["CompanyID"] != null)
{
if (HttpContext.Current.Session["BranchID"] != null) return;
var loginPageBranch = WebConfigurationManager.AppSettings["LoginPagePath"] ?? "~/Admin/Branch";
if (HttpContext.Current.Request.Path.Contains(loginPageBranch.Substring(loginPageBranch.LastIndexOf("/") + 1)) == false
&& HttpContext.Current.Request.Path.Contains("Branch") == false
&& HttpContext.Current.Request.Path.Contains("BillingCompanyReport.aspx") == false
&& HttpContext.Current.Request.Path.Contains("UserCompanyMapping.aspx") == false
&& HttpContext.Current.Request.Path.Contains("AddCompany.aspx") == false
&& HttpContext.Current.Request.Path.Contains("AddBranch.aspx") == false
&& HttpContext.Current.Request.Path.Contains("InvoceHeadSettings.aspx") == false
//&& HttpContext.Current.Request.Path.Contains("ForgetPassword.aspx") == false
)
{
HttpContext.Current.Response.Redirect(loginPageBranch, true);
}
else
{
return;
}
}
var loginPageCompany = WebConfigurationManager.AppSettings["LoginPagePath"] ?? "~/Admin/Company";
if (HttpContext.Current.Request.Path.Contains(loginPageCompany.Substring(loginPageCompany.LastIndexOf("/") + 1)) == false
&& HttpContext.Current.Request.Path.Contains("Company") == false
&& HttpContext.Current.Request.Path.Contains("AddCompany.aspx") == false
&& HttpContext.Current.Request.Path.Contains("BillingCompanyReport.aspx") == false
&& HttpContext.Current.Request.Path.Contains("UserCompanyMapping.aspx") == false
&& HttpContext.Current.Request.Path.Contains("AddCompany.aspx") == false
&& HttpContext.Current.Request.Path.Contains("AddBranch.aspx") == false
&& HttpContext.Current.Request.Path.Contains("InvoceHeadSettings.aspx") == false
)
{
HttpContext.Current.Response.Redirect(loginPageCompany, true);
}
else
{
return;
}
}
var loginPage = WebConfigurationManager.AppSettings["LoginPagePath"] ?? "~/Login?Session=Expire";
if (HttpContext.Current.Request.Path.Contains(loginPage.Substring(loginPage.LastIndexOf("/") + 1)) == false)
{
HttpContext.Current.Response.Redirect(loginPage, true);
}
else
{
}
}
}
}
#endregion
}
3) Web.config
<system.web>
<httpModules>
<add name="MyPreInitModule" type="PreInitModule" />
</httpModules>
</system.web>
<system.webServer>
<modules>
<add name="MyPreInitModule" type="PreInitModule" />
</modules>
</system.webServer>