Impersonation on .net service busHow to use impersonation or identity on .net service bus?© 2009 Microsoft Corporation. All rights reserved.Wed, 01 Jul 2009 10:10:48 Z5e61dab5-3b55-457d-8f88-9a8145da4acchttp://social.msdn.microsoft.com/Forums/en-US/netservices/thread/5e61dab5-3b55-457d-8f88-9a8145da4acc#5e61dab5-3b55-457d-8f88-9a8145da4acchttp://social.msdn.microsoft.com/Forums/en-US/netservices/thread/5e61dab5-3b55-457d-8f88-9a8145da4acc#5e61dab5-3b55-457d-8f88-9a8145da4accinfy123http://social.msdn.microsoft.com/Profile/en-US/?user=infy123Impersonation on .net service busHow to use impersonation or identity on .net service bus?Thu, 25 Jun 2009 12:13:32 Z2009-06-25T12:13:32Zhttp://social.msdn.microsoft.com/Forums/en-US/netservices/thread/5e61dab5-3b55-457d-8f88-9a8145da4acc#5bd01f60-05b5-494d-aef8-a09973031a03http://social.msdn.microsoft.com/Forums/en-US/netservices/thread/5e61dab5-3b55-457d-8f88-9a8145da4acc#5bd01f60-05b5-494d-aef8-a09973031a03Yi-Lun Luohttp://social.msdn.microsoft.com/Profile/en-US/?user=Yi-Lun%20LuoImpersonation on .net service bus<p>Please refer to <a href="http://social.msdn.microsoft.com/Forums/en-US/netservices/thread/f35aa4f8-0d05-487c-8d79-25b3a4431500">http://social.msdn.microsoft.com/Forums/en-US/netservices/thread/f35aa4f8-0d05-487c-8d79-25b3a4431500</a> to see how to use Windows identity on Service Bus. For impersonation, first your service needs to get a UPN claim issued by Geneva Server, which contains information about the user's identity. You can configure a rule in ACS: Input: the UPN claim, Output: Copy input value. That way, your service will be able to get the UPN claim. Then you can convert the UPN claim to a Windows token by calling S4UClient.UpnLogon method from Geneva Framework. The following code is extracted from the Convert Claims to NT Token sample from Geneva Framework. You can have a look at the complete sample under &quot;Microsoft Geneva Framework\Samples\Extensibility\Convert Claims to NT Token&quot;.<br/><br/><span style="font-size:x-small"><font size=2> <p> </p> </font></span></p> <p><span style="color:#008000;font-size:x-small"><span style="color:#008000;font-size:x-small">// Gets the current identity and extracts the UPN claim.</span></span></p> <span style="font-size:x-small"><font size=2> <p> </p> </font></span> <p><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">IClaimsIdentity</span></span><span style="font-size:x-small"> identity = ( </span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">ClaimsIdentity</span></span><span style="font-size:x-small"> )</span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">Thread</span></span><span style="font-size:x-small">.CurrentPrincipal.Identity;<font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">string</span></span><span style="font-size:x-small"> upn = </span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">null</span></span><span style="font-size:x-small">;<font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">foreach</span></span><span style="font-size:x-small"> ( </span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">Claim</span></span><span style="font-size:x-small"> claim </span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">in</span></span><span style="font-size:x-small"> identity.Claims ) <p>{</p> <font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">if</span></span><span style="font-size:x-small"> ( </span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">StringComparer</span></span><span style="font-size:x-small">.Ordinal.Equals( System.IdentityModel.Claims.</span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">ClaimTypes</span></span><span style="font-size:x-small">.Upn, claim.ClaimType ) ) <p>{</p> <p>upn = claim.Value;</p> <p>}</p> <p>}</p> <font size=2> <p> </p> </font></span></p> <p><span style="color:#008000;font-size:x-small"><span style="color:#008000;font-size:x-small">// Performs the UPN logon through the WTS service.</span></span></p> <span style="font-size:x-small"><font size=2> <p> </p> </font></span> <p><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">WindowsIdentity</span></span><span style="font-size:x-small"> windowsIdentity = </span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">null</span></span><span style="font-size:x-small">;<font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">if</span></span><span style="font-size:x-small"> ( !</span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">String</span></span><span style="font-size:x-small">.IsNullOrEmpty( upn ) ) <p>{</p> <font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">try</span></span></p> <span style="font-size:x-small"> <p>{</p> <font size=2> <p>windowsIdentity =</p> </font></span> <p><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">S4UClient</span></span><span style="font-size:x-small">.UpnLogon( upn ); <p>}</p> <font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">catch</span></span><span style="font-size:x-small"> ( </span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">SecurityAccessDeniedException</span></span><span style="font-size:x-small"> ) <p>{</p> <font size=2> <p> </p> </font></span></p> <p><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">Console</span></span><span style="font-size:x-small">.WriteLine( </span><span style="color:#a31515;font-size:x-small"><span style="color:#a31515;font-size:x-small">&quot;Could not map the upn claim to a valid windows identity.&quot;</span></span><span style="font-size:x-small"> );<font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">return</span></span><span style="font-size:x-small">; <p>}</p> <p>}</p> <font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">else</span></span></p> <span style="font-size:x-small"> <p>{</p> <font size=2> <p> </p> </font></span> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">throw</span></span><span style="font-size:x-small"> </span><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">new</span></span><span style="font-size:x-small"> </span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">Exception</span></span><span style="font-size:x-small">( </span><span style="color:#a31515;font-size:x-small"><span style="color:#a31515;font-size:x-small">&quot;No UPN claim found&quot;</span></span><span style="font-size:x-small"> ); <p>}</p> <font size=2> <p> </p> </font></span></p> <p><span style="color:#0000ff;font-size:x-small"><span style="color:#0000ff;font-size:x-small">using</span></span><span style="font-size:x-small"> ( </span><span style="color:#2b91af;font-size:x-small"><span style="color:#2b91af;font-size:x-small">WindowsImpersonationContext</span></span><span style="font-size:x-small"> ctxt = windowsIdentity.Impersonate() ) <p>{</p> <p>//Do work here...</p> <p>}</p> <font size=2> <p> </p> </font></span></p> <p><br/></p><hr class="sig">Lante, shanaolanxing This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Fri, 26 Jun 2009 06:49:37 Z2009-06-26T06:49:37Z