asp.net based on cookies simple permission to judge _ practical skills

Source: Internet
Author: User
Tags httpcontext set cookie
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;
}
}
}
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.