<?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>Sam Beauvois &#187; Source Code</title>
	<atom:link href="http://www.sambeauvois.be/blog/category/source-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sambeauvois.be/blog</link>
	<description>general dev, .net and other stuff</description>
	<lastBuildDate>Tue, 31 Jan 2012 13:38:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Bind StackPanel Visiblility to Radiobutton IsChecked</title>
		<link>http://www.sambeauvois.be/blog/2010/10/bind-stackpanel-visiblility-to-radiobutton-ischecked/</link>
		<comments>http://www.sambeauvois.be/blog/2010/10/bind-stackpanel-visiblility-to-radiobutton-ischecked/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 14:39:00 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=644</guid>
		<description><![CDATA[First steps with Silverlight &#8230; The context : I have some radiobuttons, and a stackpanel I want to be visible if one or the radiobutton is checked The solution I found : 1) Write the code The radio buttons // ... &#60;RadioButton Name=&#34;SomeRadio&#34;  IsChecked=&#34;True&#34; Content=&#34;some radio/&#62; &#60;RadioButton Name=&#34;WantedRadio&#34; Content=&#34;the wanted radio&#34;/&#62; // ... The StackPanel [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>First steps with Silverlight &#8230;</p></blockquote>
<p>The context :</p>
<p>I have some radiobuttons, and a stackpanel I want to be visible if one or the radiobutton is checked</p>
<p>The solution I found :</p>
<p>1) Write the code</p>
<p>The radio buttons</p>
<pre class="brush: xml; title: ;">

// ...

&lt;RadioButton Name=&quot;SomeRadio&quot;  IsChecked=&quot;True&quot; Content=&quot;some radio/&gt;
 &lt;RadioButton Name=&quot;WantedRadio&quot; Content=&quot;the wanted radio&quot;/&gt;

// ...
</pre>
<p>The StackPanel</p>
<pre class="brush: xml; title: ;">

// ...

&lt;StackPanel Name=&quot;ThePanel&quot;&gt;

// some content

&lt;/StackPanel&gt;
</pre>
<p>2) Convert</p>
<p>Since the Visibility property type  is an enum and the Ischeckd a boolean, we need a converter</p>
<p>Create it : you must implement the IValueConverter Inerface which is in the &#8220;System.Windows.Data&#8221; namespace</p>
<pre class="brush: csharp; title: ;">

using System;
using System.Windows;
using System.Windows.Data;

namespace TheNamespace
{
 public class VisibilityConverter : IValueConverter
 {
 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
// just in case
if (value == null)
{
return Visibility.Collapsed;
}

bool ischecked = System.Convert.ToBoolean(value);
return ischecked ? Visibility.Visible : Visibility.Collapsed;
 }

// had to be implemented
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
 }
}
</pre>
<p>Reference the namespace of the converter in the xaml file</p>
<pre class="brush: xml; title: ;">

xmlns:local=&quot;clr-namespace:TheNamespace&quot;
</pre>
<p>Add the class as ressource for the user control</p>
<pre class="brush: xml; title: ;">
&lt;UserControl.Resources&gt;

 &lt;local:VisibilityConverter x:Key=&quot;VisibilityConverter&quot; /&gt;
&lt;/UserControl.Resources&gt;
</pre>
<p>And now you can use it to bind the properties</p>
<pre class="brush: xml; title: ;">

Visibility=&quot;{Binding IsChecked,  ElementName=WantedRadio, Converter={StaticResource  VisibilityConverter}}&quot;&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/10/bind-stackpanel-visiblility-to-radiobutton-ischecked/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Image to byte array and vice-versa</title>
		<link>http://www.sambeauvois.be/blog/2010/10/image-to-byte-array-and-vice-versa/</link>
		<comments>http://www.sambeauvois.be/blog/2010/10/image-to-byte-array-and-vice-versa/#comments</comments>
		<pubDate>Tue, 05 Oct 2010 10:24:47 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=641</guid>
		<description><![CDATA[This is an old code snippet I posted on the www.developpez.com faq a few years ago. I needed it today, and I decided to post it here Image to byte array : public static Byte[] ImageToByteArray(Image img) { try { MemoryStream mstImage = new MemoryStream(); img.Save(mstImage, System.Drawing.Imaging.ImageFormat.Jpeg); Byte[] bytImage = mstImage.GetBuffer(); return bytImage; } catch [...]]]></description>
			<content:encoded><![CDATA[<p>This is an old code snippet I posted on the <a href="http://dotnet.developpez.com/faq/csharp/?page=gdiplus#gdip_convert_image_bytes" target="_blank">www.developpez.com faq</a> a few years ago.</p>
<p>I needed it today, and I decided to post it here</p>
<h3>Image to byte array :</h3>
<pre class="brush: csharp; title: ;">

public static Byte[] ImageToByteArray(Image img)
{
try
{
MemoryStream mstImage = new MemoryStream();
img.Save(mstImage, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] bytImage = mstImage.GetBuffer();
return bytImage;
}
catch (Exception ex)
{
// do something smart
return null;
}
}
</pre>
<h3>Image to Byte array</h3>
<pre class="brush: csharp; title: ;">

public static Image ByteArrayToImage(Byte[] BArray)
{
try
{
MemoryStream mstImage = new MemoryStream(BArray);
Image img = Image.FromStream(mstImage);
return img;
}
catch (Exception ex)
{
// do something smart
return null;
}
}
</pre>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow: hidden;">http://dotnet.developpez.com/faq/csharp/?page=gdiplus#gdip_convert_image_bytes</div>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/10/image-to-byte-array-and-vice-versa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The IX509CertificateRequestPkcs10 InitializeFromTemplateName adventure</title>
		<link>http://www.sambeauvois.be/blog/2010/04/the-ix509certificaterequestpkcs10-initializefromtemplatename-adventure/</link>
		<comments>http://www.sambeauvois.be/blog/2010/04/the-ix509certificaterequestpkcs10-initializefromtemplatename-adventure/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 16:54:31 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Certificate]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/2010/04/the-ix509certificaterequestpkcs10-initializefromtemplatename-adventure/</guid>
		<description><![CDATA[This week has been researches, tests and headaches to be able to make request on a Certificate Authority server from a web application. My client has a Win server 2008 CA server for my developments On this server I have a certificate template named “User Template” My assignment was to request a certificate via an [...]]]></description>
			<content:encoded><![CDATA[<p>This week has been researches, tests and headaches to be able to make request on a Certificate Authority server from a web application.</p>
<p>My client has a Win server 2008 CA server for my developments</p>
<p>On this server I have a certificate template named “User Template”</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/Certificate_server_templates.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Certificate_server_templates" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/Certificate_server_templates_thumb.png" border="0" alt="Certificate_server_templates" width="484" height="297" /></a></p>
<p>My assignment was to request a certificate via an ASP.NET application.</p>
<p>After researches on how to do that, I found this blog post that helped me a lot : <a title="http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx" href="http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx" target="_blank">http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx</a></p>
<p>In my development environment, I create a request for a certificate by using  this code</p>
<pre class="brush: csharp; title: ;">
 CERTENROLLLib.CX509CertificateRequestPkcs10Class request = new CERTENROLLLib.CX509CertificateRequestPkcs10Class();
 string templateName = &quot;User Template&quot;;
 try
 {
    request.InitializeFromTemplateName(CERTENROLLLib.X509CertificateEnrollmentContext.ContextUser, templateName);
 }
 catch (Exception ex)
 {
   log.DebugFormat(&quot;Error InitializeFromTemplateName : message {0}, inner : {1}, stack : {2}, source : {3}, target : {4} &quot;,
    ex.Message,
    ex.InnerException,
    ex.StackTrace,
    ex.Source,
    ex.TargetSite);
 }
</pre>
<p>And it worked just fine !</p>
<p>Then we moved the published solution to the staging environment, and the problems arrived . . .</p>
<p>It didn’t worked !</p>
<p>The error message said:</p>
<p>&#8220;CertEnroll::CX509CertificateRequestPkcs10::InitializeFromTemplateName: The requested certificate template is not supported by this CA. 0&#215;80094800 (-2146875392)&#8221;</p>
<p>stack :</p>
<p>&#8220;at CERTENROLLLib.CX509CertificateRequestPkcs10Class.InitializeFromTemplateName(X509CertificateEnrollmentContext Context, String strTemplateName)&#8221;</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/cererror.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="cererror" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/cererror_thumb.png" border="0" alt="cererror" width="484" height="294" /></a></p>
<p>We checked the security everywhere ( CA server, Certificate templates, IIS Account, …), give access to everyone on the CA, on the template, … without any success</p>
<p>Then, after days and hours of search and tests, I decided to re-read the method documentation : <a title="http://msdn.microsoft.com/en-us/library/aa377533%28v=VS.85%29.aspx" href="http://msdn.microsoft.com/en-us/library/aa377533%28v=VS.85%29.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/aa377533%28v=VS.85%29.aspx</a></p>
<p>And I noticed the second parameter description:</p>
<blockquote>
<dt><em>strTemplateName</em> [in] </dt>
<dd>Pointer to a <strong>BSTR</strong> variable that contains the Common Name (CN) of the template as it appears in Active Directory or the dotted decimal <a href="http://msdn.microsoft.com/en-us/library/ms721599%28v=VS.85%29.aspx#_security_object_identifier_gly" target="_blank"><em>object identifier</em></a>.</p>
</dd>
</blockquote>
<p>I had never tried using the dotted decimal object identifier, so i give it a shot.</p>
<p>I retrieved the object identifier on a certificate previously created with the “User Template”</p>
<p>On the CA server, I had a certificate request with the template “User Template”, so I right-click on it, go to All Tasks and click on the “View attributes/Extensions. . .” menu item.</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image14.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image_thumb14.png" border="0" alt="image" width="484" height="304" /></a></p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image15.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image_thumb15.png" border="0" alt="image" width="484" height="103" /></a></p>
<p>A property windows opened, I go to the Extensions tab and click on the “Certificate Template Information” item.</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image16.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/04/image_thumb16.png" border="0" alt="image" width="425" height="484" /></a></p>
<p>In the information panel, I can see the famous dotted decimal object identifier between brackets.</p>
<p>I copy-paste this identifier in my code :</p>
<pre class="brush: csharp; title: ;">
 CERTENROLLLib.CX509CertificateRequestPkcs10Class request = new CERTENROLLLib.CX509CertificateRequestPkcs10Class();
 string templateName = &quot;1.3.6.1.4.1.311.21.8.3531346.8488945.6374567.164989.5001604.52.12582268.10747996&quot;;
 try
 {
    request.InitializeFromTemplateName(CERTENROLLLib.X509CertificateEnrollmentContext.ContextUser, templateName);
 }
 catch (Exception ex)
 {
    log.DebugFormat(&quot;Error InitializeFromTemplateName : message {0}, inner : {1}, stack : {2}, source : {3}, target : {4} &quot;,
      ex.Message,
      ex.InnerException,
      ex.StackTrace,
      ex.Source,
      ex.TargetSite);
 }
</pre>
<p>I compiled, I tried in the development environment and it worked !!</p>
<p>(note: there is just a minimal change in my code since I put the “templateName” in a configuration file in order to be able to modify it)</p>
<p>So we moved the published solution to the staging environment,</p>
<p>retrieve the dotted decimal object identifier corresponding to the “User Template” on the staging CA server,</p>
<p>copy-pasted this identifier in the configuration file,</p>
<p>executed a “IISRESET” command and try to execute the application.</p>
<p>And it worked !</p>
<p>We can&#8217;t figured out why it worked in the development environment but not in the staging one while both are equals. So If anyone knows about it, please let me know.</p>
<p>So, don’t trust the template name and use the object identifier !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/04/the-ix509certificaterequestpkcs10-initializefromtemplatename-adventure/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How long need a portion of code to be executed ?</title>
		<link>http://www.sambeauvois.be/blog/2009/12/how-long-need-a-portion-of-code-to-be-executed/</link>
		<comments>http://www.sambeauvois.be/blog/2009/12/how-long-need-a-portion-of-code-to-be-executed/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 08:44:07 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=106</guid>
		<description><![CDATA[Use the Stopwatch object ! using System.Diagnostics; // ... Stopwatch myStopWatch = Stopwatch.StartNew(); // some code myStopWatch.Stop(); Console.WriteLine(&#34;{0} ms - {1} ticks&#34;, myStopWatch.Elapsed.TotalMilliseconds, myStopWatch.ElapsedTicks); // ...]]></description>
			<content:encoded><![CDATA[<p>Use the Stopwatch object !</p>
<pre class="brush: csharp; title: ;">
using System.Diagnostics;

// ...

Stopwatch myStopWatch = Stopwatch.StartNew();

// some code

myStopWatch.Stop();

Console.WriteLine(&quot;{0} ms - {1} ticks&quot;, myStopWatch.Elapsed.TotalMilliseconds, myStopWatch.ElapsedTicks);

// ...
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2009/12/how-long-need-a-portion-of-code-to-be-executed/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

