Extaspnet application skills (III)-302 and ASP. NET Ajax

Source: Internet
Author: User
Problem description:
Mgzhenhong users mentioned this problem And provides an example:
1. Web. config enables Forms authentication. < Authentication Mode = "Forms" >
< Forms Name = ". Test" Loginurl = "~ /Login. aspx" Timeout = "20" Protection = "All" > </ Forms >
</ Authentication >
< Authorization >
< Deny Users = "? " />
</ Authorization >

2. On the login page (login. aspx), place a button and click the button to simulate Logon: Protected   Void Button#click ( Object Sender, eventargs E)
{
Formsauthentication. setauthcookie ( " Accountid " , False );
Pagecontext. Redirect ( " ~ /Default. aspx " );
}

3. Place a button on the home page and delete the logon credential during page_load: Protected   Void Page_load ( Object Sender, eventargs E)
{
Formsauthentication. signout ();
}
Protected   Void Button#click ( Object Sender, eventargs E)
{
// Nothing
}

4. When you click this button, the logon page will be displayed, but an error occurred due to the use of extaspnet:

Problem Analysis:
First, from the information provided by firebug, we know that two requests are indeed sent when you click the button on the default. aspx page. The first response is 302 found,
The second is the redirected logon page.
This reminds me of the previous error of using response. Redirect, which is exactly the same as this one. In the past, our solution was to tell you not to use response. Redirect in the future,
We use the method pagecontext. Redirect provided by extaspnet, but it seems that it cannot be bypassed now. We have reason to believe that the form authentication of ASP. NET is called internally.
Response. Redirect function. We may modify the implementation of ASP. NET.

Another path:
Since we cannot get around the 302 found response, why not support it, But the strange thing is that the Ajax request in extaspnetCodeMedium: Ext. Ajax. Request ({
URL: Document. Location. href,
Params: serializeform (theform. ID ),
Success: _ ajaxsuccess,
Failure: _ ajaxfailure
});

The two HTTP requests are changed to one and the status code is 200 rather than 302 in the callback function (_ ajaxsuccess.
In desperation, I had to use the network to find the following articleArticle:
Http://extjs.com/forum/showthread.php? T = 30278
The final conclusion is:

Unfortunately basex can't handle 302 as the browser PreemptsIt.
Bottom line-redirects considered harmful with extjs and other Ajax frameworks.

The focus of the above sentence is on the preempt word. I specifically checked this word, which means "vt. First Purchase (first take)". It seems that the 302 response is
The browser is ruthlessly hijacked, and XMLHttpRequest sees a complete HTTP request response.

It seems that the client cannot solve this problem.

peak return:
there is always a solution on the server side, we can capture 302 in the response stream and then process it and output it to the browser.
In fact, httpmodules does not do this. I was a little lazy before coding. asp is a common problem. net Ajax has already been implemented.
it is true that, in \ Microsoft ASP. net \ ASP. NET 2.0 Ajax extensions \ v1.0.61025 \ source \ system. web. extensions \ handlers \ scriptmodule. CS
I have seen a long-overdue 302 error:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> private void presendrequestheadershandler ( Object sender, eventargs ARGs) {
httpapplication application = (httpapplication) sender;
httpresponse response = application. response;

If(Response. statuscode= 302){
// .
}
}

We will implement an extaspnet. scriptmodule as follows: Public ClassScriptmodule: ihttpmodule
{

Private VoidPresendrequestheadershandler (ObjectSender, eventargs ARGs)
{
Httpapplication=(Httpapplication) sender;
Httpresponse response=Application. response;

If (Response. statuscode =   302 )
{
If (ResourceManager. isextaspnetajaxpostback2 (application. Request ))
{
String Redirectlocation = Response. redirectlocation;
List < Httpcookie > Cookies =   New List < Httpcookie > (Response. Cookies. Count );
For ( Int I =   0 ; I < Response. Cookies. Count; I ++ )
{
Cookies. Add (response. Cookies [I]);
}

Response. clearcontent ();
Response. clearheaders ();
For ( Int I =   0 ; I < Cookies. Count; I ++ )
{
Response. appendcookie (Cookies [I]);
}
Response. cache. setcacheability (httpcacheability. nocache );
Response. contenttype =   " Text/plain " ;
Response. Write (string. Format ( " Window. Location. href = '{0 }'; " , Redirectlocation ));
}
}
}

# RegionIhttpmodule Member

Public VoidDispose ()
{

}

Public VoidInit (httpapplication context)
{
Context. presendrequestheaders+ = NewEventhandler (presendrequestheadershandler );
}

# Endregion
}

In this way, the problem that previously left over response. Redirect cannot be used is also solved. Now you can use response. Redirect to redirect to the page.
You can also use pagecontext. Redirect and be compatible with form authentication of ASP. NET.

========================================================== ==================================
Now let's re-Describe the example at the beginning of this article:
1. Web. config enables Forms authentication. <? XML version = "1.0" ?>
< Configuration >
< Configsections >
< Section Name = "Extaspnet" Type = "Extaspnet. configsection, extaspnet" />
</ Configsections >
< System. Web >

pages
controls
Add Assembly =" extaspnet" namespace = "extaspnet" tagprefix = "Ext" />
controls
pages

<Httpmodules>
<AddName= "Scriptmodule"Type= "Extaspnet. scriptmodule, extaspnet"/>
</Httpmodules>

< Authentication Mode = "Forms" >
< Forms Name = ". Test" Loginurl = "~ /Login. aspx" Defaurl URL = "~ /Default. aspx" Timeout = "20" Protection = "All" > </ Forms >
</ Authentication >
< Authorization >
< Deny Users = "? " />
</ Authorization >

< Compilation Debug = "True" />
</ System. Web >
</ Configuration >

2. Place a button on the login. ASPX page and click the button to simulate Logon:

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> protected void button#click ( Object sender, eventargs E)
{< br> formsauthentication. redirectfromloginpage ( " accountid " , false );
}

3. Place a button on the home page and delete the logon credential during page_load: Protected VoidPage_load (ObjectSender, eventargs E)
{
Formsauthentication. signout ();
}
Protected VoidButton#click (ObjectSender, eventargs E)
{
//Nothing
}

4. Click this button to go To the logon page.

Enjoy coding.

Examples in this articleSource code

Note: download the latest extaspnet source code from SVN.

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.