Create SecurityLib.cs in App_Code/Common Folder
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
using DataAccessLayer;
public class UtilityLibrary
{
public UtilityLibrary()
{
//
// TODO: Add constructor logic here
//
}
public static string SecureString(string TexttoValidate)
{
string TextVal;
TextVal = TexttoValidate;
try
{
//Build an array of characters that need to be filter.
string[] strDirtyQueryString = { "xp_", ";", "--", "<", ">", "script", "iframe", "delete", "drop", "exec","truncate","1==1" };
//Loop through all items in the array
foreach (string item in strDirtyQueryString)
{
if (TextVal.IndexOf(item) != -1)
{
//PageRedirect(1);//Redirect to page not found.
break;
}
}
}
catch (Exception ee)
{
ExceptionHandler.WriteException(ee.Message);
}
return TextVal;
}
public void SessionReset()
{
try
{
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpContext.Current.Session.RemoveAll();
if (HttpContext.Current.Request.Cookies["ASP.NET_SessionId"] != null)
{
HttpContext.Current.Response.Cookies["ASP.NET_SessionId"].Value = string.Empty;
HttpContext.Current.Response.Cookies["ASP.NET_SessionId"].Expires = DateTime.Now.AddMinutes(10);
}
if (HttpContext.Current.Request.Cookies["AuthToken"] != null)
{
HttpContext.Current.Response.Cookies["AuthToken"].Value = string.Empty;
HttpContext.Current.Response.Cookies["AuthToken"].Expires = DateTime.Now.AddMinutes(10);
}
//ChangeSession();
HttpContext.Current.Response.Redirect("~/Default.aspx", true);
}
catch (Exception ex)
{
ExceptionHandler.WriteException(ex.Message);
}
}
public void ChangeSession()
{
// SET SESSION AND COOKIE NEW VALUE EACH TIME THE METHOD IS ACCESSED
try
{
System.Web.Security.FormsAuthenticationTicket AuthTicket = null;
AuthTicket = new System.Web.Security.FormsAuthenticationTicket(1, "ATMA", DateTime.Now, DateTime.Now.AddMinutes(10), false, "");
string EncryptTicket = System.Web.Security.FormsAuthentication.Encrypt(AuthTicket);
HttpContext.Current.Response.Cookies.Add(new HttpCookie("AuthToken", EncryptTicket));
HttpContext.Current.Session["AuthToken"] = EncryptTicket;
}
catch (Exception ex)
{
ExceptionHandler.WriteException(ex.Message);
}
}
public void SetSessionCookie()
{
// SET SESSION COOKIE ON PAGE LOAD
try
{
System.Web.Security.FormsAuthenticationTicket AuthTicketPage = null;
AuthTicketPage = new System.Web.Security.FormsAuthenticationTicket(1, "ATMA_NEW", DateTime.Now, DateTime.Now.AddMinutes(10), false, "");
string PageTicket = System.Web.Security.FormsAuthentication.Encrypt(AuthTicketPage);
//HttpContext.Current.Response.Cookies.Add(new HttpCookie("AuthTokenPage", PageTicket));
HttpContext.Current.Session["AuthTokenPage"] = PageTicket;
}
catch (Exception Ex)
{
ExceptionHandler.WriteException(Ex.Message);
}
}
public void SetSessionCookieChildPage()
{
// SET SESSION COOKIE ON PAGE LOAD
try
{
System.Web.Security.FormsAuthenticationTicket AuthTicketPageChild = null;
AuthTicketPageChild = new System.Web.Security.FormsAuthenticationTicket(1, "ATMA_NEW_CHILD", DateTime.Now, DateTime.Now.AddMinutes(10), false, "");
string PageTicketChild = System.Web.Security.FormsAuthentication.Encrypt(AuthTicketPageChild);
HttpContext.Current.Session["AuthTokenPageChild"] = PageTicketChild;
}
catch (Exception Ex)
{
ExceptionHandler.WriteException(Ex.Message);
}
}
public int ddlSelIndex(DropDownList ddl, string id)
{
int val = 0;
for (int selInd = 0; selInd < ddl.Items.Count; selInd++)
{
if (ddl.Items[selInd].Value.Trim().ToLower().Equals(id.Trim().ToLower()))
{
val = selInd;
break;
}
}
return val;
}
public int ddlSelIndexText(DropDownList ddl, string id)
{
int val = 0;
for (int selInd = 0; selInd < ddl.Items.Count; selInd++)
{
if (ddl.Items[selInd].Text.Trim().ToLower().Equals(id.Trim().ToLower()))
{
val = selInd;
break;
}
}
return val;
}
public int RadioSelIndex(RadioButtonList rbtn, string id)
{
int val = 0;
for (int selInd = 0; selInd < rbtn.Items.Count; selInd++)
{
if (rbtn.Items[selInd].Value.Trim().ToLower().Equals(id.Trim().ToLower()))
{
val = selInd;
break;
}
}
return val;
}
public int RadioSelIndexText(RadioButtonList rbtn, string id)
{
int val = 0;
for (int selInd = 0; selInd < rbtn.Items.Count; selInd++)
{
if (rbtn.Items[selInd].Text.Trim().ToLower().Equals(id.Trim().ToLower()))
{
val = selInd;
break;
}
}
return val;
}
}
No comments:
Post a Comment