Write cookie page, create cookie, set cookie property, and add to Response.Cookies to read cookies, use the name or index of the cookie to obtain the overwrite cookie from the Request.Cookies, first create a cookie with the same name, read the cookie in the request with the same name, The property value of the read cookie is paid to the new object, added to the response.cookies to create a BasePage page, the other pages inherit from this page, the code that judges the permission has a single page Page_ Load is transferred to BasePage's preload, following is the main code for BasePage
Copy Code code as follows:
public class BasePage:System.Web.UI.Page
{
private string pagename;
Public BasePage ()
{
This. Page.preload + = Page_Load;
}
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Uri r = this. Request.url;
PAGENAME = r.absolutepath;
if (Needtocheck ())
{
if (! Hasauthentication ())
{
HttpContext.Current.Response.Redirect ("noauthenticationpage.aspx");
}
}
}
}
private bool Needtocheck ()
{
if (Pagename.contains ("noauthenticationpage.aspx") | | | pagename = = "login.aspx")
{
return false;
}
return true;
}
private bool Hasauthentication ()
{
Look into the config file or database,to the Allow accessing list of the whether.
The signature of the function is like this
Queryinconfig (M_userrole,pagename);
if (Pagename.contains ("default3.aspx") && userrole = = "2")
{
return false;
}
return true;
}
protected HttpCookie _requestcookie;
protected HttpCookie _responsecookie;
private bool B_isnewcookie = true;
public string Userrole
{
Get
{
Return Getcookievalue ("Userrole");
}
Set
{
Setcookievalue ("Userrole", value);
}
}
public string UserName
{
Get
{
Return Getcookievalue ("UserName");
}
Set
{
Setcookievalue ("UserName", value);
}
}
protected void Setcookievalue (string name, String value)
{
Setresponsecookie ();
_responsecookie[name] = value;
}
private string Getcookievalue (string name)
{
Setreqeustcookie ();
if (_requestcookie!= null)
{
return _requestcookie[name];
}
return null;
}
protected void Setreqeustcookie ()
{
_requestcookie = httpcontext.current.request.cookies["Cookie_name"];
}
protected void Setresponsecookie ()
{
if (B_isnewcookie)
{
HttpContext.Current.Response.Cookies.Remove ("Cookie_name");
_responsecookie = new HttpCookie ("Cookie_name");
DateTime Dtnow = DateTime.Now;
TimeSpan Tsminute = new TimeSpan (0, 2, 0, 0);
_responsecookie.expires = Dtnow + Tsminute;
_responsecookie["userrole"] = userrole;
_responsecookie["UserName"] = UserName;
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (_responsecookie);
B_isnewcookie = false;
}
}
}