InfocardInteractiveChannelInitializer.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.IdentityModel.Selectors;
  7. using System.IdentityModel.Tokens;
  8. using System.Runtime;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel.Description;
  11. using System.ServiceModel.Dispatcher;
  12. public class InfocardInteractiveChannelInitializer : IInteractiveChannelInitializer
  13. {
  14. ClientCredentials credentials;
  15. Binding binding;
  16. public InfocardInteractiveChannelInitializer(ClientCredentials credentials, Binding binding)
  17. {
  18. this.credentials = credentials;
  19. this.binding = binding;
  20. }
  21. public Binding Binding
  22. {
  23. get
  24. {
  25. return binding;
  26. }
  27. }
  28. public virtual IAsyncResult BeginDisplayInitializationUI(IClientChannel channel, AsyncCallback callback, object state)
  29. {
  30. return new GetTokenUIAsyncResult(binding, channel, this.credentials, callback, state);
  31. }
  32. public virtual void EndDisplayInitializationUI(IAsyncResult result)
  33. {
  34. GetTokenUIAsyncResult.End(result);
  35. }
  36. }
  37. internal class GetTokenUIAsyncResult : AsyncResult
  38. {
  39. IClientChannel proxy;
  40. ClientCredentials credentials;
  41. Uri relyingPartyIssuer;
  42. bool requiresInfoCard;
  43. Binding binding;
  44. static AsyncCallback callback = Fx.ThunkCallback(new AsyncCallback(GetTokenUIAsyncResult.Callback));
  45. internal GetTokenUIAsyncResult(Binding binding,
  46. IClientChannel channel,
  47. ClientCredentials credentials,
  48. AsyncCallback callback,
  49. object state)
  50. : base(callback, state)
  51. {
  52. this.credentials = credentials;
  53. this.proxy = channel;
  54. this.binding = binding;
  55. this.CallBegin(true);
  56. }
  57. void CallBegin(bool completedSynchronously)
  58. {
  59. IAsyncResult result = null;
  60. Exception exception = null;
  61. try
  62. {
  63. CardSpacePolicyElement[] chain;
  64. SecurityTokenManager tokenManager = credentials.CreateSecurityTokenManager();
  65. requiresInfoCard = InfoCardHelper.IsInfocardRequired(binding, credentials, tokenManager, proxy.RemoteAddress, out chain, out relyingPartyIssuer);
  66. MessageSecurityVersion bindingSecurityVersion = InfoCardHelper.GetBindingSecurityVersionOrDefault(binding);
  67. WSSecurityTokenSerializer tokenSerializer = WSSecurityTokenSerializer.DefaultInstance;
  68. result = credentials.GetInfoCardTokenCallback.BeginInvoke(requiresInfoCard, chain, tokenManager.CreateSecurityTokenSerializer(bindingSecurityVersion.SecurityTokenVersion), callback, this);
  69. }
  70. catch (Exception e)
  71. {
  72. if (Fx.IsFatal(e))
  73. {
  74. throw;
  75. }
  76. exception = e;
  77. }
  78. if (exception == null)
  79. {
  80. if (!result.CompletedSynchronously)
  81. {
  82. return;
  83. }
  84. this.CallEnd(result, out exception);
  85. }
  86. if (exception != null)
  87. {
  88. return;
  89. }
  90. this.CallComplete(completedSynchronously, null);
  91. }
  92. static void Callback(IAsyncResult result)
  93. {
  94. if (result.CompletedSynchronously)
  95. {
  96. return;
  97. }
  98. GetTokenUIAsyncResult outer = (GetTokenUIAsyncResult)result.AsyncState;
  99. Exception exception = null;
  100. outer.CallEnd(result, out exception);
  101. outer.CallComplete(false, exception);
  102. }
  103. void CallEnd(IAsyncResult result, out Exception exception)
  104. {
  105. try
  106. {
  107. SecurityToken token = credentials.GetInfoCardTokenCallback.EndInvoke(result);
  108. ChannelParameterCollection channelParameters =
  109. proxy.GetProperty<ChannelParameterCollection>();
  110. if (null != channelParameters)
  111. {
  112. channelParameters.Add(new InfoCardChannelParameter(token, relyingPartyIssuer, requiresInfoCard));
  113. }
  114. exception = null;
  115. }
  116. catch (Exception e)
  117. {
  118. if (Fx.IsFatal(e))
  119. {
  120. throw;
  121. }
  122. exception = e;
  123. }
  124. }
  125. void CallComplete(bool completedSynchronously, Exception exception)
  126. {
  127. this.Complete(completedSynchronously, exception);
  128. }
  129. internal static void End(IAsyncResult result)
  130. {
  131. AsyncResult.End<GetTokenUIAsyncResult>(result);
  132. }
  133. }
  134. }