After logging on to the asp.net instance, return to the previous page.

Source: Internet
Author: User

In web programming, some pages need to be accessed only after logon. Therefore, I have compiled a base class (Adminbasepage) to determine whether session and Cookies exist to control whether pages are accessible. The logon page is automatically displayed if you cannot access it. The login page I wrote is converted to a fixed page, so the question is, how do I go to the page of the last request?

1. First try to use Request. UrlReferrer

(1) first write the following code in the Load event

The Code is as follows: Copy code
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
If (Request. UrlReferrer! = Null)
{
ViewState ["UrlReferrer"] = Request. UrlReferrer. ToString ();
}
}
}

2) write the following code in the logon event:

The Code is as follows: Copy code
Protected void Signin ()
{
// After the user and password verification is completed here
/**************************
* The code here is omitted ......
***************************/
If (ViewState ["UrlReferrer"]! = Null)
{
// Go to the request page
Response. Redirect (ViewState ["UrlReferrer"]. ToString ());
}
Else
{
// Go to the hosts page
Response. Redirect ("Default. aspx ");
}
}

It should be possible on the surface, but this is not the case. Because my base class is written to determine Session, Cookies, and other information in the PreLoad event, if it does not exist, go to the logon page. Therefore, the login page is displayed before the Load event on the Request page, so that the value of Request. UrlReferrer is null. According to the stswordman blog, he wrote the following:

Note the following when using Request. UrlReferrer:

(1) If the document. location method is used on the previous page to navigate to the current page, Request. UrlReferrer returns a null value.

(2) If there are two pages A and B, directly Request page A in the browser and navigate to page B in the Page_Load event of page A, then Request. UrlReferrer returns NULL. Because the page has not been initialized in the Page_load event, you cannot record the information of the current page. If you navigate to page B, you cannot obtain the information of the previous page.

(3) clicking the refresh button will not change Request. UrlReferrer

This method does not work, so I decided to use Request. QueryString to solve this problem.

2. Use Request. QueryString

The key to this method is how to obtain the Url of the Request page, transfer the URl of the page to the login page, and then go to the Url after the login is successful.

(1) write the following code in the verification base class (Adminbasepage ):

The Code is as follows: Copy code

Protected override void OnInit (EventArgs e)
{
Base. OnInit (e );
This. PreLoad + = new EventHandler (BaseValidate );
}

Protected void BaseValidate (object sender, EventArgs e)
{

If (Request. Cookies ["manager"]! = Null)
{
// To do someting your want
}

Else
{
String PermissionUrl = VirtualPathUtility. ToAppRelative (Request. Url. AbsolutePath); // key

Response. Redirect ("Signin. aspx? Purl = "+ Server. UrlEncode (PermissionUrl ));
}

}

The logon event on the logon page is redirected

The Code is as follows: Copy code

If (Request. QueryString ["purl"] = null)
{
Response. Redirect ("Default. aspx ");
}
Else
{
Response. Redirect (Server. UrlDecode (Request. QueryString ["purl"]. ToString ()));
}

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.