<?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>tinyint.com &#187; ASP.Net</title>
	<atom:link href="http://www.tinyint.com/index.php/category/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tinyint.com</link>
	<description>An engineering and development &#60;em&#62;Factory of Knowledge&#60;/em&#62;™</description>
	<lastBuildDate>Fri, 23 Sep 2011 05:39:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>ASP.NET Code Delimiters</title>
		<link>http://www.tinyint.com/index.php/2011/01/19/asp-net-code-delimiters/</link>
		<comments>http://www.tinyint.com/index.php/2011/01/19/asp-net-code-delimiters/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 00:51:38 +0000</pubDate>
		<dc:creator>tcnolan</dc:creator>
				<category><![CDATA[ASP.Net]]></category>

		<guid isPermaLink="false">http://www.tinyint.com/?p=193</guid>
		<description><![CDATA[When working in ASP.Net there are a number of code delimiters you need to be aware of, the most recognizable one is &#60;% which, for anyone who has worked with anything from Classic ASP 1.0 (anyone remember IIS 3?) through ASP.Net 4.0 will recognize. Some people call them ASP.net tags or code shortcuts, both of [...]]]></description>
			<content:encoded><![CDATA[<p>When working in ASP.Net there are a number of <strong>code delimiters</strong> you need to be aware of, the most recognizable one is &lt;% which, for anyone who has worked with anything from Classic ASP 1.0 (anyone remember IIS 3?) through ASP.Net 4.0 will recognize.  Some people call them ASP.net <em>tags</em> or <em>code shortcuts</em>, both of which are only partly true.  Calling them &#8220;<em>tags</em>&#8221; is a little misleading because ASP.NET <em>tags</em> are really things such as &#8220;<code>&lt;asp:DropDownList /&gt;</code>&#8221; and &#8220;<code>&lt;asp:ListItem /&gt;</code>&#8220;.  Calling them <em>code shortcuts</em> is a bit misleading as well because, while some of them are shortcuts (<code>&lt;%= ... %&gt;</code> is a shortcut for <code>&lt;% Response.Write (...) %&gt;</code>) not all of them have this kind of purpose.<br />
<span id="more-193"></span><br />
While &#8220;code delimiter&#8221; sometimes refers explicitly to the left carat + percent sign and percent sign + right carat only, the verbiage used in Microsoft documentation is usually &#8220;code delimiter&#8221; and as such is what I am going to refer to them throughout this post.  There are a number of different code delimiters available to an ASP.Net developer, but a beginner might have a bit of trouble finding out what they all do.  Microsoft lists the majority of them in the ASP.Net Page Syntax section of MSDN (<a href="http://msdn.microsoft.com/en-us/library/fy30at8h.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/fy30at8h.aspx</a>) but this post is meant to be a quick access list of those delimiters.  While an experienced developer is going to be intimately familiar with all of these, I thought it would be useful to write a short post to list all of them.</p>
<p>One thing to note about the examples I am using below: they are only intended to show how these would look in a block of code, rather than demonstrate real-world usage.  Refer to the MSDN article links for more detail on actual usage of each delimiter.</p>
<p>So, without further ado, here is the full list of ASP.NET code delimiters.</p>
<div style="padding-top: 18px; margin-top: 18px; border-top: 1px dashed #888888;">
<strong style="font-size: 15px;">&#8220;<code>&lt;% ... %&gt;</code>&#8220;</strong> &#8211; The first code delimiter I will mention is for embedding blocks of inline code (<a href="http://msdn.microsoft.com/en-us/library/k6xeyd4z.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/k6xeyd4z.aspx</a>)</p>
<pre class="brush: vb; title: ; notranslate">
&lt;%
'Basic inline code block
For x = 1 to 100
    Response.Write string.Format(&quot;x = {0}&lt;br /&gt;&quot;, x)
Next
%&gt;
</pre>
</div>
<div style="padding-top: 18px; margin-top: 18px; border-top: 1px dashed #888888;">
<strong style="font-size: 15px;">&#8220;<code>&lt;%= ... %&gt;</code>&#8220;</strong> &#8211; The next code delimiter is also for inline code but specifically a shortcut to the inline code block <strong style="font-size: 15px;">&#8220;<code>&lt;% Response.Write ( ... ) %&gt;</code>&#8220;</strong>.  (<a href="http://msdn.microsoft.com/en-us/library/k6xeyd4z.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/k6xeyd4z.aspx</a>)</p>
<pre class="brush: vb; title: ; notranslate">
Current Date and Time: &lt;%= DateTime.Now().ToString() %&gt;
</pre>
</div>
<div style="padding-top: 18px; margin-top: 18px; border-top: 1px dashed #888888;">
<strong style="font-size: 15px;">&#8220;<code>&lt;%-- ... --%&gt;</code>&#8220;</strong> &#8211; Another delimiter that is a shortcut of sorts, perhaps a lesser known one though, is the server-side comment.  It is simply used for commenting out information that you don&#8217;t want to get sent off to the client.  There is a gotcha to the server-side comment however, that you cannot use it in embedded code blocks, which will actually cause a compilation error if you try.  (<a href="http://msdn.microsoft.com/en-us/library/4acf8afk.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/4acf8afk.aspx</a>)</p>
<pre class="brush: vb; title: ; notranslate">
&lt;%-- You can use a server side comment to keep information from getting to the browser of a user --%&gt;
</pre>
</div>
<div style="padding-top: 18px; margin-top: 18px; border-top: 1px dashed #888888;">
<strong style="font-size: 15px;">&#8220;<code>&lt;%@ ... %&gt;</code>&#8220;</strong> &#8211; The template directive is an important delimiter, one that the majority of ASP.Net web forms pages will have on them.  There are quite a few different directives that you need to be familiar with, and not all are for web forms pages, but the most common one is probably the page directive, which allows you to specify code behind, master pages, enable or disable viewstate, buffering options, and a plethora of others.  (<a href="http://msdn.microsoft.com/en-us/library/xz702w3e.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/xz702w3e.aspx</a>)</p>
<pre class="brush: vb; title: ; notranslate">
&lt;%@ Page Title=&quot;Home&quot; Language=&quot;VB&quot; MasterPageFile=&quot;~/Site.master&quot; AutoEventWireup=&quot;false&quot; CodeFile=&quot;Default.aspx.vb&quot; Inherits=&quot;_Default&quot; EnableSessionState=&quot;False&quot; %&gt;
</pre>
</div>
<div style="padding-top: 18px; margin-top: 18px; border-top: 1px dashed #888888;">
<strong style="font-size: 15px;">&#8220;<code>&lt;%# ... %&gt;</code>&#8220;</strong> &#8211; Data-binding expressions are the next important set of code delimiters, which are used to create a binding between a server control property and a data source.  (<a href="http://msdn.microsoft.com/en-us/library/bda9bbfx.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/bda9bbfx.aspx</a>)</p>
<pre class="brush: vb; title: ; notranslate">
&lt;asp:DropDownList ID=&quot;ddlInsertUser&quot;
                        SelectedValue='&lt;%# Bind(&quot;UserID&quot;) %&gt;'
                        DataSourceID=&quot;UsersDataSource&quot;
                        DataTextField=&quot;UserName&quot;
                        DataValueField=&quot;UserID&quot;
                        RunAt=&quot;Server&quot; /&gt;
</pre>
</div>
<div style="padding-top: 18px; margin-top: 18px; border-top: 1px dashed #888888;">
<strong style="font-size: 15px;">&#8220;<code>&lt;%$ ... %&gt;</code>&#8220;</strong> &#8211; The last code delimiter to mention is the ASP.Net Expression delimiter. This delimiter allows you to set the properties of a control to an evaluated expression at runtime.  While this has many useful purposes, it is incredibly useful when localizing your web pages.  (<a href="http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/d5bd1tad.aspx</a>)</p>
<pre class="brush: vb; title: ; notranslate">
&lt;asp:Label id=&quot;label1&quot; runat=&quot;server&quot; text=&quot;&lt;%$ Resources: LocalizeData, WelcomeLabel %&gt;&quot; /&gt;
</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.tinyint.com/index.php/2011/01/19/asp-net-code-delimiters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>401 Error on HttpWebRequest with NTLM Authentication</title>
		<link>http://www.tinyint.com/index.php/2009/08/24/401-error-on-httpwebrequest-with-ntlm-authentication/</link>
		<comments>http://www.tinyint.com/index.php/2009/08/24/401-error-on-httpwebrequest-with-ntlm-authentication/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 00:05:44 +0000</pubDate>
		<dc:creator>tcnolan</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[DNS]]></category>
		<category><![CDATA[Sharepoint]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[401 error]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[iis7]]></category>
		<category><![CDATA[ntlm]]></category>
		<category><![CDATA[webrequest]]></category>

		<guid isPermaLink="false">http://www.tinyint.com/?p=109</guid>
		<description><![CDATA[Due to a security update to SMB that fixes a remote code execution vulnerability, you may experience 401.1 or 401.2 errors in certain situations while performing a WebRequest to one of your servers. This is because part of the security update institutes a loopback check on the authentication requests to prevent replay attacks. The Microsoft [...]]]></description>
			<content:encoded><![CDATA[<p>Due to a <a href="http://support.microsoft.com/kb/957097">security update</a> to SMB that fixes a remote code execution vulnerability, you may experience 401.1 or 401.2 errors in certain situations while performing a WebRequest to one of your servers.  This is because part of the security update institutes a loopback check on the authentication requests to prevent replay attacks.  The Microsoft KB article refers to a few different scenarios where you can see authentication problems after applying this patch, but the one I’m most interested in is when you start getting 401 errors after an HttpWebRequest on an ASP.Net page.</p>
<p>The issues I experienced were while creating a page to display general status of one of my companies Sharepoint servers.  The main issue which is the meat of this article was when I switched the authentication over to use NTLM instead of Digest, which broke my script.  Everything I had in place should have worked, but the previously mentioned security update slipped under the radar and it took a while to figure out what was going on.  You can do a basic web request on a server with Basic authentication by doing the following:</p>
<pre class="brush: vb; title: ; notranslate">Dim _response As String
Dim _auth As String = &quot;Basic&quot;
Dim _uri As Uri = New Uri(&quot;http://my.domain.local/my-page.aspx&quot;)
Dim _req As HttpWebRequest = WebRequest.Create(_uri)
Dim _cc As CredentialCache = New CredentialCache()
Dim _res As HttpWebResponse
Dim _sr As StreamReader

_cc.Add(_uri, _auth, New NetworkCredential(&quot;username&quot;, &quot;password&quot;, &quot;domain&quot;))
_req.PreAuthenticate = True
_req.Credentials = _cc.GetCredential(_uri, _auth)
_res = _req.GetResponse
_sr = New StreamReader(_res.GetResponseStream)
_response = _sr.ReadToEnd
_sr.Close()</pre>
<p><span id="more-109"></span><br />
That code works perfectly well with Basic and even Digest(if you change the _auth value) authentication, however it will most likely not work for you if you are using NTLM authentication and your server has had the <a href="http://support.microsoft.com/kb/957097">MS08-068 patch</a> applied to it.  Prior to discovering the conflict with this patch I went through a lot of effort to investigate the issue.  One of the most useful debugging utilities in IIS7 is the failed request tracing logs.  These logs give you really great insight as to what is going on inside your web request traffic.  You can also use a tool like Fiddler to try and see what’s happening, but unfortunately because our Sharepoint server was live, it would have caused SSL certificate issues if I put it in the mix in my situation.</p>
<p>After looking at the failed request traces logs, I noticed an interesting difference between one of the failed requests from my health check script and a purposely failed request through a web browser that I pointed to a file I had no access to read.</p>
<p><strong>401.3 Error message &#8211; Unauthorized due to ACL on resource</strong></p>
<p><a href="http://www.tinyint.com/wp-content/uploads/2009/08/401_3.png"><img class="alignnone size-full wp-image-110 mceItem" title="401_3" src="http://www.tinyint.com/wp-content/uploads/2009/08/401_3.png" alt="401_3" width="579" height="427" /></a></p>
<p><strong>401.2 Error message &#8211; Logon failed due to server configuration</strong></p>
<p><a href="http://www.tinyint.com/wp-content/uploads/2009/08/401_2.png"><img class="alignnone size-full wp-image-111 mceItem" title="401_2" src="http://www.tinyint.com/wp-content/uploads/2009/08/401_2.png" alt="401_2" width="575" height="252" /></a></p>
<p><strong>Note: </strong>Check out <a href="http://support.microsoft.com/kb/943891">http://support.microsoft.com/kb/943891</a> for all the status codes in IIS7</p>
<p>The difference here is that on the WebRequest, it wasn’t even getting an NTLM token from the .Net app.  This lead me astray however and made me think that there was a problem with the pre-authentication of the credentials as well as whether I was missing something in the web.config, or even if it was some unique problem with Sharepoint.  After a lot of investigation I discovered the Microsoft KB article titled: “<a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;896861">You receive error 401.1 when you browse a Web site that uses Integrated Authentication and is hosted on IIS 5.1 or IIS 6</a>”  After giving a thorough read of that document I realized the problem, Microsoft forgot to update the title of the KB article.  The sub status code I was getting on the 401 errors is different than Microsoft mentions in the article, and while they don’t mention IIS 7 in the article’s title, it does show up in the applies to section.</p>
<p>Since Microsoft recommends you leave the loopback check in place because it is part of the security update, I followed their recommended method of adding in a specified host name to the BackConnectionHostNames key in the registry.</p>
<p>As always, make registry modifications at your own risk, Microsoft always recommends you make a backup before making any modifications, check out the following KB article for backup instructions: <a href="http://support.microsoft.com/kb/322756">http://support.microsoft.com/kb/322756</a></p>
<blockquote><p><strong>Warning</strong> Serious problems might occur if you modify the registry incorrectly by using Registry Editor or by using another method. These problems might require that you reinstall the operating system. TinyInt.Com cannot guarantee that these problems can be solved. Modify the registry at your own risk.</p></blockquote>
<blockquote><p>To specify the host names that are mapped to the loopback address and can connect to Web sites on your computer, follow these steps:</p>
<ol style="margin-top: 0in;" type="1">
<li>Click <strong>Start</strong>,      click <strong>Run</strong>, type regedit, and then click <strong>OK</strong>.</li>
<li>In Registry Editor,      locate and then click the following registry key:</li>
</ol>
<p style="padding-left: 60px;"><strong>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0</strong></p>
<ol style="margin-top: 0in;" type="1">
<li>Right-click <strong>MSV1_0</strong>,      point to <strong>New</strong>, and then click <strong>Multi-String Value</strong>.</li>
<li>Type      BackConnectionHostNames, and then press ENTER.</li>
<li>Right-click <strong>BackConnectionHostNames</strong>,      and then click <strong>Modify</strong>.</li>
<li>In the <strong>Value data</strong> box, type the host name or the host names for the sites that are on the      local computer, and then click <strong>OK</strong>.</li>
<li>Quit Registry Editor,      and then restart the IISAdmin service.</li>
</ol>
</blockquote>
<p>Now that you have taken care of editing the registry to include the hostname you are using in your code, you should be able to make your WebRequest over NTLM without a problem, just make sure you have impersonate=”true” in your web.config file.</p>
<p>Attached below is the code for the health check script that I have been talking about throughout this article.  As I mentioned earlier, it does some very basic checks on the server just to see if things are running smoothly.  To use it you would simply need to change the variables being passed into the various functions to check your database connectivity.  We use WhatsUp Gold to track the value of ALL_STATUS_OK to make sure things are working properly, and once one of the values fails it sends us an alert.  Another thing to keep in mind is if you are using the WebRequest piece in this code, make sure you grant the user you are authenticating as access to the Sharepoint page you are checking.  Lastly, if you use the code, make sure you read the comment in the web.config file about encrypting the settings so your password is not exposed, even if you are using a low privileged user account.</p>
<p>Download Source: <strong><a href="http://www.tinyint.com/wp-content/plugins/download-monitor/download.php?id=2"><img alt="Download sharepoint-contentscan.zip" border="0" src="/images/download.png" style="width: 48px; height: 48px; border-width: 0pt; border-style: solid; vertical-align: middle;" title="Download sharepoint-contentscan.zip" /><span style="font-size:14px;">Download sharepoint-contentscan.zip</span></a></strong><br /><br /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tinyint.com/index.php/2009/08/24/401-error-on-httpwebrequest-with-ntlm-authentication/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

