<?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>ToddHa's WebLog : CardSpace</title><link>http://blogs.msdn.com/toddha/archive/tags/CardSpace/default.aspx</link><description>Tags: CardSpace</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Hey You! This is a stick up!</title><link>http://blogs.msdn.com/toddha/archive/2007/02/22/hey-you-this-is-a-stick-up.aspx</link><pubDate>Fri, 23 Feb 2007 00:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1743501</guid><dc:creator>toddha</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/toddha/comments/1743501.aspx</comments><wfw:commentRss>http://blogs.msdn.com/toddha/commentrss.aspx?PostID=1743501</wfw:commentRss><description>&lt;p&gt;Hand over your &lt;strike&gt;wallet&lt;/strike&gt; InfoCard!&lt;/p&gt; &lt;p&gt;I've been playing around with CardSpace and trying to get it to do what I need it to do : authenticate some user in a little 3-tier play app that I'm writing.&amp;nbsp;It's not as simple to use as most people would think. Here's my experience with it and how I got it to work. Hopefully I'll save you some time and frustration. (Note your milage may vary, especially since I'm not as detailed here as I would like to be). &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Also, please note : this is&amp;nbsp;NOT prescriptive guidance.&amp;nbsp;There are no claims, warranties, or rights implied.&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;First, install IE7. CardSpace requires it. You can do the next couple of steps without it, but eventually you'll need it.&lt;/p&gt; &lt;p&gt;I needed a cert in my test environment. You may or may not need this step, but&amp;nbsp;I went off and got the SelfSSL&amp;nbsp;tool in the &lt;a href="http://support.microsoft.com/kb/840671"&gt;IIS 6.0 Resource Kit&lt;/a&gt;. I created a self signed cert and installed it into my computer's certificates store (via mmc).&lt;/p&gt; &lt;p&gt;Next, I created a new IIS website (via inetmgr). I right clicked on it, clicked properties, went to the Directory Security tab, clicked on Server Certificate, and chose my cert. I checked that my site (https) worked in Internet Explorer and that there were no cert issues.&lt;/p&gt; &lt;p&gt;There's one small step that you'll probably need to do with certs that I missed, however. You'll need to know what identity your web site is running under. Open up inetmgr, find the web site or virtual directory that you are using. Right click it, click properties, and on the Web Site or Virtual Directory tab it will say the AppPool. Go back to the main window of inetmgr, expand Application Pools, find that AppPool, right click it, properties, and it will show you on the Identity tab what account it's running under.&lt;/p&gt; &lt;p&gt;Next.&lt;/p&gt; &lt;p&gt;Go download and install the WSE 3.0 tools from &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=018A09FD-3A74-43C5-8EC1-8D789091255D&amp;amp;displaylang=en"&gt;here&lt;/a&gt;. Run the Certificates tool.&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Choose Local Computer as the location.  &lt;li&gt;Click&amp;nbsp;Open Certificate  &lt;li&gt;Choose the Certificate you signed and click OK.  &lt;li&gt;Click View Private Key File Properties.  &lt;li&gt;Click on the Security tab.  &lt;li&gt;Make sure the account that we identified as the AppPool identity is listed and has read access.  &lt;li&gt;Done.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Now you can implement your login. See &lt;a href="http://msdn2.microsoft.com/en-us/library/aa967562.aspx"&gt;here&lt;/a&gt;&amp;nbsp;for the sample code on how to do this. I used some of this. I wrote my own code for looking up the Certificates by subject like this :&lt;/p&gt; &lt;p&gt;&lt;code&gt;protected static X509Certificate2 LookupCert(string subject)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;X509Store store = new X509Store(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/code&gt;&lt;code&gt;StoreName.My,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; StoreLocation.LocalMachine);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;store.Open(OpenFlags.ReadOnly);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (X509Certificate2 cert in store.Certificates)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (cert.Subject.Equals(subject)) return cert;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;&lt;br&gt;}&lt;/code&gt;&lt;/p&gt; &lt;p&gt;Probably not the most efficient as you can call store.Certificates.Find(...).&lt;/p&gt; &lt;p&gt;I then took just Token.DecryptToken to do what I needed and discarded most of the rest (there are some supporting data structures that DecryptToken requires). Remember, no rights or warranties implied. Use at your own risk.&lt;/p&gt; &lt;p&gt;&lt;code&gt;string xmlToken = Request.Params["_xmlToken"];&lt;br&gt;if (xmlToken == null || xmlToken.Length == 0)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowError("ERROR : Token presented was null");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;}&lt;br&gt;string serverSubject = Request.Params["HTTPS_SERVER_SUBJECT"];&lt;br&gt;if (serverSubject == null || serverSubject.Length == 0)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowError("ERROR : serverSubject was null");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;} &lt;br&gt;try&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; X509Certificate2 c = LookupCert(serverSubject);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (c == null)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowError("ERROR : Could not find cert to decrypt with");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// decrypt the token into a byte array&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte [] decrypted = DecryptToken(c, xmlToken);&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // get&amp;nbsp;SAML string from decrypted&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string data = new UnicodeEncoding().GetString(bytes);&lt;/code&gt;&lt;code&gt;&lt;br&gt;}&lt;br&gt;catch (Exception ex)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ShowError("ERROR : " + ex.ToString());&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return;&lt;br&gt;}&lt;br&gt;&lt;/code&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;You now have the SAML and can authenticate as appropriate!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1743501" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/toddha/archive/tags/InfoCard/default.aspx">InfoCard</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Encryption/default.aspx">Encryption</category><category domain="http://blogs.msdn.com/toddha/archive/tags/CardSpace/default.aspx">CardSpace</category><category domain="http://blogs.msdn.com/toddha/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Programming/default.aspx">Programming</category></item></channel></rss>