Asp.net simple Cookie-based permission judgment

Source: Internet
Author: User

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;
}
}
}

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.