Asp.net logout (after exiting, click the browser to return to the page)

Source: Internet
Author: User

Copy codeThe Code is as follows:
Session. Abandon ();
Response. Redirect ("Login. aspx ");

However, after clicking the browser, you can still return to the page just now. This is not acceptable. After searching for it online, many people have encountered such problems and tried some methods, none of them work. But I still find it. Share it.
Http://blog.csdn.net/lhypang2006/archive/2008/03/11/2170751.aspx
Copy codeThe Code is as follows:
Session. Abandon ();
Response. Write ("<script> window. location. href = 'login. aspx '</script> ");

It is easy to change Response. Redirect to Response. Write, and output the script to achieve redirection.
Share another one, which is also about exiting.
Use HttpHandler in Asp. Net
I think the above method is good. Write a class to inherit IHttpHandler.
Copy codeThe Code is as follows:
Public class LogoutHttpHandler: IHttpHandler
{
/// <Summary>
/// Enable HTTP Web request processing by implementing the custom HttpHandler interface of the IHttpHandler interface.
/// </Summary>
/// <Param name = "context"> HttpContext object, which provides reference for internal Server objects (such as Request, Response, Session, and Server) used to provide services for HTTP requests. </Param>
Public void ProcessRequest (HttpContext context)
{
FormsAuthentication. SignOut ();
Context. Response. Redirect ("Login. aspx", true );
}

Modify web. config and add the following script to <system. web> </system. web>:
Copy codeThe Code is as follows:
<HttpHandlers>
<Add verb = "GET" path = "Logout. aspx" type = "LogoutHttpHandler"/>
</HttpHandlers>

Compile the class into a dll in the article, or you can just add this class in App_Code.
The previous ProcessRequest does not clear the Session. Response. Redirect is also used. You can also click back to return to the original page. I changed it.
Copy codeThe Code is as follows:
Public class LogoutHttpHandler: IHttpHandler, IRequiresSessionState
{
Public void ProcessRequest (HttpContext context)
{
// FormsAuthentication. SignOut (); // you can skip this step.
Context. Session. Abandon ();
Context. Response. Write ("<script> window. location. href = 'login. aspx '</script> ");
}
}

In this way, you do not need to add a page Logout. aspx, And the exit code is also simple.
Copy codeThe Code is as follows:
Protected void Exit_Click (object sender, EventArgs e)
{
Response. Redirect ("Logout. aspx ");
}

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.