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. The code for permission judgment includes Page_Load of a single page and is transferred to the BasePage PreLoad. below is the main BasePage code.
Copy codeThe 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;
}
}
}