﻿<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://developers.de/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>Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx<description>In some sophisticated scenarios there might be requirement to implement the custom security header. Because this is not well documented in WCF documentation I decided to post one the example. By implementing of custom security there will be some custom</description><dc:language>en</dc:language><generator>CommunityServer 2008 SP1 (Build: 30619.63)</generator><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#6160<pubdate>Sat, 27 Jun 2009 13:51:39 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:6160</guid><dc:creator>Damir Dobric</dc:creator><description>&lt;p&gt;Please take a look here: &lt;a rel="nofollow" target="_new" href="http://developers.de/blogs/rolf_nebhuth/archive/2009/05/13/signing-xml-documents.aspx"&gt;developers.de/.../signing-xml-documents.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Thi smight be helpful. Signing in context of WCF ist mostly very complex topic, because it is not well documented etc. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=6160" width="1" height="1"&gt;</description></item><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#6154<pubdate>Sat, 27 Jun 2009 02:15:26 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:6154</guid><dc:creator>Alex</dc:creator><description>&lt;p&gt;I&amp;#39;ve tried this exact code and it does work, however, there is a problem when it comes to signing the token with a pki cert.&lt;/p&gt;
&lt;p&gt;When WCF signs a message, it doesn&amp;#39;t care what has been written to the xml message stream, it goes directly to the object. This can be a problem if you write out a custom attribute in a way that is different from that of the standard serializer.Serialize (this) method. There has to be another way. Any ideas?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=6154" width="1" height="1"&gt;</description></item><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#5415<pubdate>Mon, 18 May 2009 21:45:13 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:5415</guid><dc:creator>ktk</dc:creator><description>&lt;p&gt;Thanks Damir. I appreciate your help. I&amp;#39;m posting the code blocks here:&lt;/p&gt;
&lt;p&gt;//////////////////////////////////&lt;/p&gt;
&lt;p&gt;//This is from Damir&amp;#39;s Blog&lt;/p&gt;
&lt;p&gt;/////////////////////////////////&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;internal class MySecurityTokenSerializer : WSSecurityTokenSerializer&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{ &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private SecurityToken m_Token;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public MySecurityTokenSerializer(SecurityToken token, SecurityVersion version) : base(version) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{ &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;m_Token = token; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;	///&amp;lt;summary&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// Writes the specified security token using the specified XML writer. Called &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/// by the base class. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;///&amp;lt;/summary&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;///&amp;lt;param name=&amp;quot;writer&amp;quot;&amp;gt;The writer used to wite the token data.&amp;lt;/param&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;///&amp;lt;param name=&amp;quot;token&amp;quot;&amp;gt;The security token which should be serialized.&amp;lt;/param&amp;gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;protected override void WriteTokenCore(XmlWriter writer, SecurityToken token) &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (m_Token is XmlSecurityToken) &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;serializeSbbSamlToken(writer, token as XmlSecurityToken); &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;base.WriteTokenCore(writer, token); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private void serializeSbbSamlToken(XmlWriter writer, XmlSecurityToken token) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;XmlNodeReader reader = new XmlNodeReader(token); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;writer.WriteNode(reader, true); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;/////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;p&gt;//This is my calling code&lt;/p&gt;
&lt;p&gt;/////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Claim[] claims = new Claim[] &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{ &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new Claim(@&amp;quot;&lt;a rel="nofollow" target="_new" href="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn&amp;quot;"&gt;schemas.xmlsoap.org/.../upn&amp;quot;&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;quot;Someone@hotmail.com&amp;quot;) &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;};&lt;/p&gt;
&lt;p&gt;//my internal method to get a security token from my STS&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SecurityToken stsToken = CreateSTSToken(claims);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SecurityVersion version = SecurityVersion.WSSecurity11;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MySecurityTokenSerializer serializer = new MySecurityTokenSerializer(stsToken, version);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StringBuilder stringBuilder = new StringBuilder();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;XmlWriter xr = XmlWriter.Create(new StringWriter(stringBuilder), new XmlWriterSettings { OmitXmlDeclaration = true });&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;serializer.WriteToken(xr, stsToken);&lt;/p&gt;
&lt;p&gt;//////////////////////////////////////////////////////////////////////&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=5415" width="1" height="1"&gt;</description></item><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#5385<pubdate>Sat, 16 May 2009 18:06:28 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:5385</guid><dc:creator>Damir Dobric</dc:creator><description>&lt;p&gt;Can you post the code?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=5385" width="1" height="1"&gt;</description></item><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#5375<pubdate>Fri, 15 May 2009 18:03:36 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:5375</guid><dc:creator>ktk</dc:creator><description>&lt;p&gt;I&amp;#39;m getting an error saying &amp;#39;type or namespace XmlSecurityToken &amp;nbsp;could not be found&amp;#39;. does this need to be defined somewhere?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=5375" width="1" height="1"&gt;</description></item><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#1909<pubdate>Fri, 09 May 2008 21:51:46 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:1909</guid><dc:creator>Damir Dobric</dc:creator><description>&lt;p&gt;Could you please describe the error?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1909" width="1" height="1"&gt;</description></item><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#1908<pubdate>Fri, 09 May 2008 21:44:09 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:1908</guid><dc:creator>James Syed</dc:creator><description>&lt;p&gt;There was an error serializing the security token&lt;/p&gt;
&lt;p&gt;I get the above error when I rin your code. Any ideas what I am doing wrong?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1908" width="1" height="1"&gt;</description></item><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#1719<pubdate>Thu, 13 Dec 2007 07:20:39 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:1719</guid><dc:creator>Damir Dobric</dc:creator><description>&lt;p&gt;Hi Chris,&lt;/p&gt;
&lt;p&gt;In general, there are two ways how to do that:&lt;/p&gt;
&lt;p&gt;1.	Binary Share&lt;/p&gt;
&lt;p&gt;You use the same library at both sides. This is not a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;real SOA approach, but anytime you are going to make changes in the protocol it will be necessary to do that. If your app is designed for .NET30, this should not be a problem.&lt;/p&gt;
&lt;p&gt;2.	There are two implementations of the same thing. In a case of MessageHeader, this is not &amp;nbsp;a big problem, because the message header just keeps a list of some “strings”, semantically describe something. However the reusability of this is not well &lt;/p&gt;
&lt;p&gt;Last but not least, I’m not sure that exactly the same functionality in context o Message Header has to be implemented on both sides. Usually, I would expect that client appends something to header and service reads it (and vice versa in duplex scenario).&lt;/p&gt;
&lt;p&gt;Hope this helps you.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1719" width="1" height="1"&gt;</description></item><item><title>re: Building of Custom Security Token in WCF</title><link />http://developers.de/blogs/damir_dobric/archive/2007/05/23/Building-of-Custom-Security-Token-in-WCF.aspx#1718<pubdate>Thu, 13 Dec 2007 01:28:46 GMT</pubdate><guid ispermalink="false">7e491611-45ad-4dae-a68f-c4cb64439510:1718</guid><dc:creator>Chris</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;Greate Article!&lt;/p&gt;
&lt;p&gt;I also want to write a custom MessageHeader. I want to attach it in a client interceptor an get it on the serverside in a server interceptor.&lt;/p&gt;
&lt;p&gt;My problem is: How can I share the MyHeader Type bewteen Client and Server, so tha I can do that on the client side:&lt;/p&gt;
&lt;p&gt;request.Headers.Add(new MyHeader ());&lt;/p&gt;
&lt;p&gt;and that on the serverside:&lt;/p&gt;
&lt;p&gt;MyHeader header = new MyHeader;&lt;/p&gt;
&lt;p&gt;header = request.Headers.GetHeader&amp;lt;MyHeader&amp;gt;(header.Name, header.Namespace);&lt;/p&gt;
&lt;p&gt;Thank you!!!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://developers.de/aggbug.aspx?PostID=1718" width="1" height="1"&gt;</description></item></channel></rss>