<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>the kiZZ notes &#187; ajax http credentials</title>
	<atom:link href="http://kizzx2.com/blog/index.php/tag/ajax-http-credentials/feed/" rel="self" type="application/rss+xml" />
	<link>http://kizzx2.com/blog</link>
	<description>random stupid ideas from a narcissistic chode</description>
	<lastBuildDate>Thu, 01 Dec 2011 16:25:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>let the Web browser &#8220;remember&#8221; HTTP authentication credentials using AJAX &#8211; the correct way vs. the wrong way</title>
		<link>http://kizzx2.com/blog/index.php/2008/08/25/let-the-web-browser-remember-http-authentication-credentials-using-ajax-the-correct-way-vs-the-wrong-way/</link>
		<comments>http://kizzx2.com/blog/index.php/2008/08/25/let-the-web-browser-remember-http-authentication-credentials-using-ajax-the-correct-way-vs-the-wrong-way/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 10:50:06 +0000</pubDate>
		<dc:creator>kiZZ kiZZ</dc:creator>
				<category><![CDATA[geek stuffs]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[ajax http credentials]]></category>
		<category><![CDATA[http authentication]]></category>

		<guid isPermaLink="false">http://kizzx2.com/blog/?p=71</guid>
		<description><![CDATA[Under normally circumstances, if you request an HTTP protected resource with your Web browser, the server would return 401 Unauthorized and an popup would appear to ask you for the correct credentials: That doesn&#8217;t look too Web 2.0-ish. Currently there is way to work around this using an AJAX request first (there&#8217;s an article that [...]]]></description>
			<content:encoded><![CDATA[<p>Under normally circumstances, if you request an HTTP protected resource with your Web browser, the server would return 401 Unauthorized and an popup would appear to ask you for the correct credentials:</p>

<p><a href="http://kizzx2.com/blog/wp-content/uploads/2008/08/auth-box.png"><img class="alignnone size-medium wp-image-72" title="auth-box" src="http://kizzx2.com/blog/wp-content/uploads/2008/08/auth-box-300x81.png" alt="" width="300" height="81" /></a></p>

<p>That doesn&#8217;t look too Web 2.0-ish. Currently there is way to work around this using an AJAX request first (there&#8217;s <a href="http://www.peej.co.uk/articles/http-auth-with-html-forms.html">an article</a> that discusses the specific technique used). That way, the browser would &#8220;remember&#8221; the Authorization header used in the previous AJAX request, so when the user requests another protected resource in the same domain, the browser would also try to include the previously used Authorization header.</p>

<p>However, I couldn&#8217;t get it to work.</p>

<p>It turns out that the difference between my approach and the approach used in the article above is this:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// My way (doesn't work)</span><br />
<span style="color: #003366; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span><br />
xhr.<span style="color: #000066;">open</span> <span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'/url/to/resource'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
xhr.<span style="color: #660066;">setRequestHeader</span> <span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Authorization&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Basic &quot;</span> <span style="color: #339933;">+</span> Base64.<span style="color: #660066;">encode</span> <span style="color: #009900;">&#40;</span> username<span style="color: #339933;">,</span> password <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
xhr.<span style="color: #660066;">send</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// The working way</span><br />
<span style="color: #003366; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span><br />
xhr.<span style="color: #000066;">open</span> <span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'/url/to/resource'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> username<span style="color: #339933;">,</span> password <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
xhr.<span style="color: #660066;">send</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>Unbeknown to me was the fact that the 4th and 5th parameter of <code>XMLHttpRequest.open()</code> could be used to specify the username and password for the request.</p>

<p>Apparently, the browser doesn&#8217;t remember the credentials if I call <code>XMLHttpRequest.setRequestHeader</code> directly.</p>

<p><em>Note: All these have only been tested to work with MSIE/Firefox. Opera for instance, doesn&#8217;t seem to remember the credentials no matter what I tried &#8211; it would still pop up an authorization box when the user request a protected resource.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://kizzx2.com/blog/index.php/2008/08/25/let-the-web-browser-remember-http-authentication-credentials-using-ajax-the-correct-way-vs-the-wrong-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

