<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Decrypt my World : XEnroll</title><link>http://blogs.msdn.com/alejacma/archive/tags/XEnroll/default.aspx</link><description>Tags: XEnroll</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>FileNotFoundException when using XEnroll in ASP.NET</title><link>http://blogs.msdn.com/alejacma/archive/2008/09/02/filenotfoundexception-when-using-xenroll-in-asp-net.aspx</link><pubDate>Tue, 02 Sep 2008 13:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8919734</guid><dc:creator>alejacma</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/alejacma/comments/8919734.aspx</comments><wfw:commentRss>http://blogs.msdn.com/alejacma/commentrss.aspx?PostID=8919734</wfw:commentRss><description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;If you ever try to use &lt;STRONG&gt;XEnroll.dll&lt;/STRONG&gt; in your &lt;STRONG&gt;ASP.NET&lt;/STRONG&gt; application (through &lt;STRONG&gt;Interop.XENROLLLib.dll&lt;/STRONG&gt;, of course), you may face an issue like the following:&lt;/P&gt;
&lt;P&gt;Your ASP.NET app &lt;STRONG&gt;impersonates&lt;/STRONG&gt; the client&amp;nbsp;&lt;STRONG&gt;user&lt;/STRONG&gt; and tries to make a &lt;STRONG&gt;certificate request&lt;/STRONG&gt; on her behalf:&lt;/P&gt;&lt;PRE&gt;using &lt;STRONG&gt;XENROLLLib&lt;/STRONG&gt;;

&lt;STRONG&gt;CEnroll&lt;/STRONG&gt; objEnroll = new CEnrollClass();
string strCertRequest = objEnroll.&lt;STRONG&gt;createRequest&lt;/STRONG&gt;(XECR_CMC , "CN=Alex", "1.3.6.1.4.1.311.2.1.21");
&lt;/PRE&gt;
&lt;P&gt;But &lt;A class="" href="http://msdn.microsoft.com/en-us/library/aa382827(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/aa382827(VS.85).aspx"&gt;createRequest&lt;/A&gt; fails with the following exception:&lt;/P&gt;&lt;PRE&gt;&lt;STRONG&gt;System.IO.FileNotFoundException&lt;/STRONG&gt; was unhandled by user code
Message="&lt;STRONG&gt;The system cannot find the file specified&lt;/STRONG&gt;. (Exception from HRESULT: 0x80070002)"
Source="&lt;STRONG&gt;Interop.XENROLLLib&lt;/STRONG&gt;"
StackTrace:
at &lt;STRONG&gt;XENROLLLib.CEnrollClass.createRequest&lt;/STRONG&gt;(Int32 Flags, String strDNName, String Usage)
...
&lt;/PRE&gt;
&lt;P&gt;If we run the very same code from a &lt;STRONG&gt;VBScript&lt;/STRONG&gt;, it &lt;STRONG&gt;works&lt;/STRONG&gt;! Mmmmmm,&amp;nbsp;this error looks&amp;nbsp;familiar to me... Check this out: &lt;A class="" href="http://blogs.msdn.com/alejacma/archive/2007/12/03/rsacryptoserviceprovider-fails-when-used-with-asp-net.aspx" mce_href="http://blogs.msdn.com/alejacma/archive/2007/12/03/rsacryptoserviceprovider-fails-when-used-with-asp-net.aspx"&gt;RSACryptoServiceProvider fails when used with ASP.NET&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The exception is a bit different, but the error message is the same and the cause, too. In order to create a request on user's behalf, that &lt;STRONG&gt;user's profile needs to be loaded&lt;/STRONG&gt;. By default, &lt;STRONG&gt;ASP.NET won't load user profiles&lt;/STRONG&gt; for us as we already know. And we need the user profile to i.e. create keys associated to the cert request.&lt;/P&gt;
&lt;P&gt;That also explains why the code works when running in a script. We logged on the server to run the script and the user profile got loaded.&lt;/P&gt;
&lt;P&gt;Summing up (see the other post I mentioned for details), we have several options here:&lt;/P&gt;
&lt;P&gt;1) Load user profile via &lt;STRONG&gt;LoadUserProfile&lt;/STRONG&gt; API. This API requires many permissions to call it and&amp;nbsp;I wouldn't recommend promoting a standard user to admin to be able to call this API, for instance.&lt;/P&gt;
&lt;P&gt;2) Load user profile via &lt;STRONG&gt;dummy&amp;nbsp;Windows service&lt;/STRONG&gt;. This is not feasible if we are impersonating many users.&lt;/P&gt;
&lt;P&gt;3) Use &lt;STRONG&gt;machine profile&lt;/STRONG&gt; instead of user profile. This way we don't need to load user profile.&amp;nbsp;We can use &lt;A class="" href="http://msdn.microsoft.com/en-us/library/aa383164(VS.85).aspx)" mce_href="http://msdn.microsoft.com/en-us/library/aa383164(VS.85).aspx)"&gt;ICEnroll4::MyStoreFlags Property&lt;/A&gt; to select &lt;STRONG&gt;CERT_SYSTEM_STORE_LOCAL_MACHINE&lt;/STRONG&gt; instead of the default CERT_SYSTEM_STORE_CURRENT_USER before calling createRequest.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I hope this helps.&lt;/P&gt;
&lt;P&gt;Kind regards,&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alex (Alejandro Campos Magencio)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8919734" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/alejacma/archive/tags/XEnroll/default.aspx">XEnroll</category></item></channel></rss>