Write the cookie page. After creating the cookie, set the cookie attribute and add it to response. the cookie is read from the cookie, and the cookie name or index is used from the request. cookies get rewrite cookies. First, create a cookie with the same name, read the cookie with the same name in the request, and pay the attribute value of the cookie to the new object and add it to response. create a basepage page in cookies. Other pages are inherited from this page. Code The page_load of a single page is transferred to the basepage preload. below is the main basepage code.
Copy code The Code is 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 see whether this page is in the allow accessing list of the role or not;
// 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;
}
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.