WrapperSecurityCommunicationObject.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.ServiceModel.Channels;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Diagnostics;
  9. using System.IdentityModel.Selectors;
  10. using System.Runtime.Diagnostics;
  11. class WrapperSecurityCommunicationObject : CommunicationObject
  12. {
  13. ISecurityCommunicationObject innerCommunicationObject;
  14. public WrapperSecurityCommunicationObject(ISecurityCommunicationObject innerCommunicationObject)
  15. : base()
  16. {
  17. if (innerCommunicationObject == null)
  18. {
  19. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("innerCommunicationObject");
  20. }
  21. this.innerCommunicationObject = innerCommunicationObject;
  22. }
  23. protected override Type GetCommunicationObjectType()
  24. {
  25. return this.innerCommunicationObject.GetType();
  26. }
  27. protected override TimeSpan DefaultCloseTimeout
  28. {
  29. get { return this.innerCommunicationObject.DefaultCloseTimeout; }
  30. }
  31. protected override TimeSpan DefaultOpenTimeout
  32. {
  33. get { return this.innerCommunicationObject.DefaultOpenTimeout; }
  34. }
  35. protected override void OnAbort()
  36. {
  37. this.innerCommunicationObject.OnAbort();
  38. }
  39. protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  40. {
  41. return this.innerCommunicationObject.OnBeginClose(timeout, callback, state);
  42. }
  43. protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  44. {
  45. return this.innerCommunicationObject.OnBeginOpen(timeout, callback, state);
  46. }
  47. protected override void OnClose(TimeSpan timeout)
  48. {
  49. this.innerCommunicationObject.OnClose(timeout);
  50. }
  51. protected override void OnClosed()
  52. {
  53. this.innerCommunicationObject.OnClosed();
  54. base.OnClosed();
  55. }
  56. protected override void OnClosing()
  57. {
  58. this.innerCommunicationObject.OnClosing();
  59. base.OnClosing();
  60. }
  61. protected override void OnEndClose(IAsyncResult result)
  62. {
  63. this.innerCommunicationObject.OnEndClose(result);
  64. }
  65. protected override void OnEndOpen(IAsyncResult result)
  66. {
  67. this.innerCommunicationObject.OnEndOpen(result);
  68. }
  69. protected override void OnFaulted()
  70. {
  71. this.innerCommunicationObject.OnFaulted();
  72. base.OnFaulted();
  73. }
  74. protected override void OnOpen(TimeSpan timeout)
  75. {
  76. this.innerCommunicationObject.OnOpen(timeout);
  77. }
  78. protected override void OnOpened()
  79. {
  80. this.innerCommunicationObject.OnOpened();
  81. base.OnOpened();
  82. }
  83. protected override void OnOpening()
  84. {
  85. this.innerCommunicationObject.OnOpening();
  86. base.OnOpening();
  87. }
  88. new internal void ThrowIfDisposedOrImmutable()
  89. {
  90. base.ThrowIfDisposedOrImmutable();
  91. }
  92. }
  93. abstract class CommunicationObjectSecurityTokenProvider : SecurityTokenProvider, ICommunicationObject, ISecurityCommunicationObject
  94. {
  95. EventTraceActivity eventTraceActivity;
  96. WrapperSecurityCommunicationObject communicationObject;
  97. protected CommunicationObjectSecurityTokenProvider()
  98. {
  99. communicationObject = new WrapperSecurityCommunicationObject(this);
  100. }
  101. internal EventTraceActivity EventTraceActivity
  102. {
  103. get
  104. {
  105. if (eventTraceActivity == null)
  106. {
  107. eventTraceActivity = EventTraceActivity.GetFromThreadOrCreate();
  108. }
  109. return this.eventTraceActivity;
  110. }
  111. }
  112. protected WrapperSecurityCommunicationObject CommunicationObject
  113. {
  114. get { return this.communicationObject; }
  115. }
  116. public event EventHandler Closed
  117. {
  118. add { this.communicationObject.Closed += value; }
  119. remove { this.communicationObject.Closed -= value; }
  120. }
  121. public event EventHandler Closing
  122. {
  123. add { this.communicationObject.Closing += value; }
  124. remove { this.communicationObject.Closing -= value; }
  125. }
  126. public event EventHandler Faulted
  127. {
  128. add { this.communicationObject.Faulted += value; }
  129. remove { this.communicationObject.Faulted -= value; }
  130. }
  131. public event EventHandler Opened
  132. {
  133. add { this.communicationObject.Opened += value; }
  134. remove { this.communicationObject.Opened -= value; }
  135. }
  136. public event EventHandler Opening
  137. {
  138. add { this.communicationObject.Opening += value; }
  139. remove { this.communicationObject.Opening -= value; }
  140. }
  141. public CommunicationState State
  142. {
  143. get { return this.communicationObject.State; }
  144. }
  145. public virtual TimeSpan DefaultOpenTimeout
  146. {
  147. get { return ServiceDefaults.OpenTimeout; }
  148. }
  149. public virtual TimeSpan DefaultCloseTimeout
  150. {
  151. get { return ServiceDefaults.CloseTimeout; }
  152. }
  153. // communication object
  154. public void Abort()
  155. {
  156. this.communicationObject.Abort();
  157. }
  158. public void Close()
  159. {
  160. this.communicationObject.Close();
  161. }
  162. public void Close(TimeSpan timeout)
  163. {
  164. this.communicationObject.Close(timeout);
  165. }
  166. public IAsyncResult BeginClose(AsyncCallback callback, object state)
  167. {
  168. return this.communicationObject.BeginClose(callback, state);
  169. }
  170. public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  171. {
  172. return this.communicationObject.BeginClose(timeout, callback, state);
  173. }
  174. public void EndClose(IAsyncResult result)
  175. {
  176. this.communicationObject.EndClose(result);
  177. }
  178. public void Open()
  179. {
  180. this.communicationObject.Open();
  181. }
  182. public void Open(TimeSpan timeout)
  183. {
  184. this.communicationObject.Open(timeout);
  185. }
  186. public IAsyncResult BeginOpen(AsyncCallback callback, object state)
  187. {
  188. return this.communicationObject.BeginOpen(callback, state);
  189. }
  190. public IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  191. {
  192. return this.communicationObject.BeginOpen(timeout, callback, state);
  193. }
  194. public void EndOpen(IAsyncResult result)
  195. {
  196. this.communicationObject.EndOpen(result);
  197. }
  198. public void Dispose()
  199. {
  200. this.Close();
  201. }
  202. // ISecurityCommunicationObject methods
  203. public virtual void OnAbort()
  204. {
  205. }
  206. public IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  207. {
  208. return new OperationWithTimeoutAsyncResult(new OperationWithTimeoutCallback(this.OnClose), timeout, callback, state);
  209. }
  210. public IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  211. {
  212. return new OperationWithTimeoutAsyncResult(new OperationWithTimeoutCallback(this.OnOpen), timeout, callback, state);
  213. }
  214. public virtual void OnClose(TimeSpan timeout)
  215. {
  216. }
  217. public virtual void OnClosed()
  218. {
  219. SecurityTraceRecordHelper.TraceTokenProviderClosed(this);
  220. }
  221. public virtual void OnClosing()
  222. {
  223. }
  224. public void OnEndClose(IAsyncResult result)
  225. {
  226. OperationWithTimeoutAsyncResult.End(result);
  227. }
  228. public void OnEndOpen(IAsyncResult result)
  229. {
  230. OperationWithTimeoutAsyncResult.End(result);
  231. }
  232. public virtual void OnFaulted()
  233. {
  234. this.OnAbort();
  235. }
  236. public virtual void OnOpen(TimeSpan timeout)
  237. {
  238. }
  239. public virtual void OnOpened()
  240. {
  241. SecurityTraceRecordHelper.TraceTokenProviderOpened(this.EventTraceActivity, this);
  242. }
  243. public virtual void OnOpening()
  244. {
  245. }
  246. }
  247. abstract class CommunicationObjectSecurityTokenAuthenticator : SecurityTokenAuthenticator, ICommunicationObject, ISecurityCommunicationObject
  248. {
  249. WrapperSecurityCommunicationObject communicationObject;
  250. protected CommunicationObjectSecurityTokenAuthenticator()
  251. {
  252. communicationObject = new WrapperSecurityCommunicationObject(this);
  253. }
  254. protected WrapperSecurityCommunicationObject CommunicationObject
  255. {
  256. get { return this.communicationObject; }
  257. }
  258. public event EventHandler Closed
  259. {
  260. add { this.communicationObject.Closed += value; }
  261. remove { this.communicationObject.Closed -= value; }
  262. }
  263. public event EventHandler Closing
  264. {
  265. add { this.communicationObject.Closing += value; }
  266. remove { this.communicationObject.Closing -= value; }
  267. }
  268. public event EventHandler Faulted
  269. {
  270. add { this.communicationObject.Faulted += value; }
  271. remove { this.communicationObject.Faulted -= value; }
  272. }
  273. public event EventHandler Opened
  274. {
  275. add { this.communicationObject.Opened += value; }
  276. remove { this.communicationObject.Opened -= value; }
  277. }
  278. public event EventHandler Opening
  279. {
  280. add { this.communicationObject.Opening += value; }
  281. remove { this.communicationObject.Opening -= value; }
  282. }
  283. public CommunicationState State
  284. {
  285. get { return this.communicationObject.State; }
  286. }
  287. public virtual TimeSpan DefaultOpenTimeout
  288. {
  289. get { return ServiceDefaults.OpenTimeout; }
  290. }
  291. public virtual TimeSpan DefaultCloseTimeout
  292. {
  293. get { return ServiceDefaults.CloseTimeout; }
  294. }
  295. // communication object
  296. public void Abort()
  297. {
  298. this.communicationObject.Abort();
  299. }
  300. public void Close()
  301. {
  302. this.communicationObject.Close();
  303. }
  304. public void Close(TimeSpan timeout)
  305. {
  306. this.communicationObject.Close(timeout);
  307. }
  308. public IAsyncResult BeginClose(AsyncCallback callback, object state)
  309. {
  310. return this.communicationObject.BeginClose(callback, state);
  311. }
  312. public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  313. {
  314. return this.communicationObject.BeginClose(timeout, callback, state);
  315. }
  316. public void EndClose(IAsyncResult result)
  317. {
  318. this.communicationObject.EndClose(result);
  319. }
  320. public void Open()
  321. {
  322. this.communicationObject.Open();
  323. }
  324. public void Open(TimeSpan timeout)
  325. {
  326. this.communicationObject.Open(timeout);
  327. }
  328. public IAsyncResult BeginOpen(AsyncCallback callback, object state)
  329. {
  330. return this.communicationObject.BeginOpen(callback, state);
  331. }
  332. public IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  333. {
  334. return this.communicationObject.BeginOpen(timeout, callback, state);
  335. }
  336. public void EndOpen(IAsyncResult result)
  337. {
  338. this.communicationObject.EndOpen(result);
  339. }
  340. public void Dispose()
  341. {
  342. this.Close();
  343. }
  344. // ISecurityCommunicationObject methods
  345. public virtual void OnAbort()
  346. {
  347. }
  348. public IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
  349. {
  350. return new OperationWithTimeoutAsyncResult(new OperationWithTimeoutCallback(this.OnClose), timeout, callback, state);
  351. }
  352. public IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
  353. {
  354. return new OperationWithTimeoutAsyncResult(new OperationWithTimeoutCallback(this.OnOpen), timeout, callback, state);
  355. }
  356. public virtual void OnClose(TimeSpan timeout)
  357. {
  358. }
  359. public virtual void OnClosed()
  360. {
  361. SecurityTraceRecordHelper.TraceTokenAuthenticatorClosed(this);
  362. }
  363. public virtual void OnClosing()
  364. {
  365. }
  366. public void OnEndClose(IAsyncResult result)
  367. {
  368. OperationWithTimeoutAsyncResult.End(result);
  369. }
  370. public void OnEndOpen(IAsyncResult result)
  371. {
  372. OperationWithTimeoutAsyncResult.End(result);
  373. }
  374. public virtual void OnFaulted()
  375. {
  376. this.OnAbort();
  377. }
  378. public virtual void OnOpen(TimeSpan timeout)
  379. {
  380. }
  381. public virtual void OnOpened()
  382. {
  383. SecurityTraceRecordHelper.TraceTokenAuthenticatorOpened(this);
  384. }
  385. public virtual void OnOpening()
  386. {
  387. }
  388. }
  389. }