WSTrustServiceContract.cs 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.IdentityModel;
  11. using System.IdentityModel.Configuration;
  12. using System.IdentityModel.Diagnostics;
  13. using System.IdentityModel.Protocols.WSTrust;
  14. using System.IdentityModel.Selectors;
  15. using System.IdentityModel.Tokens;
  16. using System.IO;
  17. using System.Security.Claims;
  18. using System.ServiceModel;
  19. using System.ServiceModel.Activation;
  20. using System.ServiceModel.Channels;
  21. using System.ServiceModel.Description;
  22. using System.Threading;
  23. using System.Web.Services.Description;
  24. using System.Xml;
  25. using System.Xml.Schema;
  26. using DiagnosticUtility = System.IdentityModel.DiagnosticUtility;
  27. using Message = System.ServiceModel.Channels.Message;
  28. using RequestContext = System.ServiceModel.Channels.RequestContext;
  29. using RST = System.IdentityModel.Protocols.WSTrust.RequestSecurityToken;
  30. using RSTR = System.IdentityModel.Protocols.WSTrust.RequestSecurityTokenResponse;
  31. using SR = System.ServiceModel.SR;
  32. using STS = System.IdentityModel.SecurityTokenService;
  33. using Fx = System.Runtime.Fx;
  34. /// <summary>
  35. /// Definition of Trust Contract Implementation. Implements the following ServiceContract interfaces,
  36. /// 1. IWSTrustFeb2005SyncContract
  37. /// 2. IWSTrust13SyncContract
  38. /// 3. IWSTrustFeb2005AsyncContract
  39. /// 4. IWSTrust13AsyncContract
  40. /// </summary>
  41. [ServiceBehavior(Name = WSTrustServiceContractConstants.ServiceBehaviorName, Namespace = WSTrustServiceContractConstants.Namespace, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
  42. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  43. public class WSTrustServiceContract : IWSTrustFeb2005SyncContract, IWSTrust13SyncContract, IWSTrustFeb2005AsyncContract, IWSTrust13AsyncContract, IWsdlExportExtension, IContractBehavior
  44. {
  45. const string soap11Namespace = "http://schemas.xmlsoap.org/soap/envelope/";
  46. const string soap12Namespace = "http://www.w3.org/2003/05/soap-envelope";
  47. SecurityTokenServiceConfiguration _securityTokenServiceConfiguration;
  48. event EventHandler<WSTrustRequestProcessingErrorEventArgs> _requestFailed;
  49. /// <summary>
  50. /// Initializes an instance of <see cref="WSTrustServiceContract"/>
  51. /// </summary>
  52. /// <param name="securityTokenServiceConfiguration">Configuration object that initializes this instance.</param>
  53. public WSTrustServiceContract(SecurityTokenServiceConfiguration securityTokenServiceConfiguration)
  54. {
  55. if (securityTokenServiceConfiguration == null)
  56. {
  57. throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("securityTokenServiceConfiguration");
  58. }
  59. _securityTokenServiceConfiguration = securityTokenServiceConfiguration;
  60. }
  61. /// <summary>
  62. /// Occurs when a Failure happens processing a Trust request from the
  63. /// client.
  64. /// </summary>
  65. public event EventHandler<WSTrustRequestProcessingErrorEventArgs> RequestFailed
  66. {
  67. add { _requestFailed += value; }
  68. remove { _requestFailed -= value; }
  69. }
  70. /// <summary>
  71. /// Returns the <see cref="SecurityTokenResolver" /> that resolves the following security tokens contained
  72. /// in the current WCF message request's security header: protection token, endorsing, or signed endorsing
  73. /// supporting tokens.
  74. /// </summary>
  75. /// <remarks>
  76. /// This <see cref="SecurityTokenResolver" /> is used to resolve any SecurityTokenIdentifiers
  77. /// when deserializing RST UseKey elements or RST RenewTarget elements.
  78. /// </remarks>
  79. /// <exception cref="ArgumentNullException"><param name="requestContext"/> is null.</exception>
  80. protected virtual SecurityTokenResolver GetSecurityHeaderTokenResolver(RequestContext requestContext)
  81. {
  82. if (requestContext == null)
  83. {
  84. throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("requestContext");
  85. }
  86. List<SecurityToken> tokenList = new List<SecurityToken>();
  87. if (requestContext.RequestMessage != null
  88. && requestContext.RequestMessage.Properties != null
  89. && requestContext.RequestMessage.Properties.Security != null)
  90. {
  91. // Add tokens in message
  92. SecurityMessageProperty msgProperty = requestContext.RequestMessage.Properties.Security;
  93. if (msgProperty.ProtectionToken != null)
  94. {
  95. tokenList.Add(msgProperty.ProtectionToken.SecurityToken);
  96. }
  97. if (msgProperty.HasIncomingSupportingTokens)
  98. {
  99. foreach (SupportingTokenSpecification tokenSpec in msgProperty.IncomingSupportingTokens)
  100. {
  101. if (tokenSpec != null &&
  102. (tokenSpec.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.Endorsing ||
  103. tokenSpec.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.SignedEndorsing))
  104. {
  105. tokenList.Add(tokenSpec.SecurityToken);
  106. }
  107. }
  108. }
  109. if (msgProperty.InitiatorToken != null)
  110. {
  111. tokenList.Add(msgProperty.InitiatorToken.SecurityToken);
  112. }
  113. }
  114. if (tokenList.Count > 0)
  115. {
  116. return SecurityTokenResolver.CreateDefaultSecurityTokenResolver(tokenList.AsReadOnly(), true);
  117. }
  118. else
  119. {
  120. return EmptySecurityTokenResolver.Instance;
  121. }
  122. }
  123. /// <summary>
  124. /// Returns the <see cref="SecurityTokenResolver" /> that will be used when resolving tokens and keys in the
  125. /// Trust message body.
  126. /// </summary>
  127. /// <returns>A <see cref="SecurityTokenResolver" /> instance.</returns>
  128. /// <seealso cref="GetSecurityHeaderTokenResolver"/>
  129. protected virtual SecurityTokenResolver GetRstSecurityTokenResolver()
  130. {
  131. if (_securityTokenServiceConfiguration != null)
  132. {
  133. SecurityTokenResolver tokenResolver = _securityTokenServiceConfiguration.SecurityTokenHandlers.Configuration.ServiceTokenResolver;
  134. if (tokenResolver != null && (!Object.ReferenceEquals(tokenResolver, EmptySecurityTokenResolver.Instance)))
  135. {
  136. return tokenResolver;
  137. }
  138. }
  139. if (OperationContext.Current != null && OperationContext.Current.Host != null &&
  140. OperationContext.Current.Host.Description != null)
  141. {
  142. ServiceCredentials serviceCreds = OperationContext.Current.Host.Description.Behaviors.Find<ServiceCredentials>();
  143. if (serviceCreds != null && serviceCreds.ServiceCertificate != null && serviceCreds.ServiceCertificate.Certificate != null)
  144. {
  145. List<SecurityToken> serviceTokens = new List<SecurityToken>(1);
  146. serviceTokens.Add(new X509SecurityToken(serviceCreds.ServiceCertificate.Certificate));
  147. return SecurityTokenResolver.CreateDefaultSecurityTokenResolver(serviceTokens.AsReadOnly(), false);
  148. }
  149. }
  150. return EmptySecurityTokenResolver.Instance;
  151. }
  152. /// <summary>
  153. /// Creates a WSTrustSerializationContext using the local resolver information
  154. /// of the WSTrustServiceClient.
  155. /// </summary>
  156. /// <returns>A WSTrustSerializationContext initialized with the current resolver information.</returns>
  157. protected virtual WSTrustSerializationContext CreateSerializationContext()
  158. {
  159. return new WSTrustSerializationContext(_securityTokenServiceConfiguration.SecurityTokenHandlerCollectionManager,
  160. this.GetRstSecurityTokenResolver(),
  161. this.GetSecurityHeaderTokenResolver(OperationContext.Current.RequestContext)
  162. );
  163. }
  164. /// <summary>
  165. /// Begins an asynchronous call to <see cref="DispatchRequest"/>.
  166. /// </summary>
  167. /// <param name="dispatchContext">Defines the request parameters to process and exposes properties
  168. /// that determine the response message and action.</param>
  169. /// <param name="asyncCallback">An optional asynchronous callback, to be called when the
  170. /// dispatch is complete.</param>
  171. /// <param name="asyncState">A user-provided object that distinguishes this particular asynchronous
  172. /// dispatch request from other requests.</param>
  173. /// <returns><see cref="IAsyncResult"/> that represents the asynchronous operation. Used as the input
  174. /// to <see cref="EndDispatchRequest"/>.</returns>
  175. protected virtual IAsyncResult BeginDispatchRequest(DispatchContext dispatchContext, AsyncCallback asyncCallback, object asyncState)
  176. {
  177. return new DispatchRequestAsyncResult(dispatchContext, asyncCallback, asyncState);
  178. }
  179. /// <summary>
  180. /// Completes an asynchronous call to <see cref="DispatchRequest"/>.
  181. /// </summary>
  182. /// <param name="ar"><see cref="IAsyncResult"/> that was returned by the
  183. /// call to <see cref="BeginDispatchRequest"/>.</param>
  184. /// <returns>The <see cref="DispatchContext"/> that exposes properties which determine the response
  185. /// message and action.</returns>
  186. protected virtual DispatchContext EndDispatchRequest(IAsyncResult ar)
  187. {
  188. return DispatchRequestAsyncResult.End(ar);
  189. }
  190. /// <summary>
  191. /// Processes a WS-Trust request message, and optionally determines the appropriate
  192. /// response message and the WS-Addressing action for the response message.
  193. /// </summary>
  194. /// <param name="dispatchContext">Defines the request parameters to process and exposes properties
  195. /// that determine the response message and action.</param>
  196. protected virtual void DispatchRequest(DispatchContext dispatchContext)
  197. {
  198. RST rst = dispatchContext.RequestMessage as RST;
  199. STS sts = dispatchContext.SecurityTokenService;
  200. ClaimsPrincipal icp = dispatchContext.Principal;
  201. if (rst != null)
  202. {
  203. switch (rst.RequestType)
  204. {
  205. case RequestTypes.Cancel:
  206. dispatchContext.ResponseMessage = sts.Cancel(icp, rst);
  207. break;
  208. case RequestTypes.Issue:
  209. dispatchContext.ResponseMessage = sts.Issue(icp, rst);
  210. break;
  211. case RequestTypes.Renew:
  212. dispatchContext.ResponseMessage = sts.Renew(icp, rst);
  213. break;
  214. case RequestTypes.Validate:
  215. dispatchContext.ResponseMessage = sts.Validate(icp, rst);
  216. break;
  217. default:
  218. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID3112, rst.RequestType)));
  219. }
  220. }
  221. else
  222. {
  223. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidRequestException(SR.GetString(SR.ID3022)));
  224. }
  225. }
  226. /// <summary>
  227. /// Handles Synchronous calls to the STS.
  228. /// </summary>
  229. /// <param name="requestMessage">Incoming Request message.</param>
  230. /// <param name="requestSerializer">Trust Request Serializer.</param>
  231. /// <param name="responseSerializer">Trust Response Serializer.</param>
  232. /// <param name="requestAction">Request SOAP action.</param>
  233. /// <param name="responseAction">Response SOAP action.</param>
  234. /// <param name="trustNamespace">Namespace URI of the trust version of the incoming request.</param>
  235. /// <returns>Response message that contains the serialized RSTR.</returns>
  236. /// <exception cref="ArgumentNullException">One of the argument is null.</exception>
  237. protected virtual Message ProcessCore(Message requestMessage, WSTrustRequestSerializer requestSerializer, WSTrustResponseSerializer responseSerializer, string requestAction, string responseAction, string trustNamespace)
  238. {
  239. if (requestMessage == null)
  240. {
  241. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("requestMessage");
  242. }
  243. if (requestSerializer == null)
  244. {
  245. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("requestSerializer");
  246. }
  247. if (responseSerializer == null)
  248. {
  249. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("responseSerializer");
  250. }
  251. if (String.IsNullOrEmpty(requestAction))
  252. {
  253. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("requestAction");
  254. }
  255. if (String.IsNullOrEmpty(responseAction))
  256. {
  257. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("responseAction");
  258. }
  259. if (String.IsNullOrEmpty(trustNamespace))
  260. {
  261. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("trustNamespace");
  262. }
  263. Message response = null;
  264. try
  265. {
  266. Fx.Assert(OperationContext.Current != null, "");
  267. Fx.Assert(OperationContext.Current.RequestContext != null, "");
  268. //
  269. // Create the Serialization and Dispatch context objects.
  270. //
  271. WSTrustSerializationContext serializationContext = CreateSerializationContext();
  272. DispatchContext dispatchContext = CreateDispatchContext(requestMessage,
  273. requestAction,
  274. responseAction,
  275. trustNamespace,
  276. requestSerializer,
  277. responseSerializer,
  278. serializationContext);
  279. //
  280. // Validate the dispatch context.
  281. //
  282. ValidateDispatchContext(dispatchContext);
  283. //
  284. // Dispatch the STS message.
  285. //
  286. DispatchRequest(dispatchContext);
  287. //
  288. // Create the response Message object with the appropriate action.
  289. //
  290. response = Message.CreateMessage(OperationContext.Current.RequestContext.RequestMessage.Version,
  291. dispatchContext.ResponseAction,
  292. new WSTrustResponseBodyWriter(dispatchContext.ResponseMessage, responseSerializer, serializationContext));
  293. }
  294. catch (Exception ex)
  295. {
  296. if (!HandleException(ex, trustNamespace, requestAction, requestMessage.Version.Envelope))
  297. {
  298. throw;
  299. }
  300. }
  301. return response;
  302. }
  303. /// <summary>
  304. /// Creates a <see cref="DispatchContext"/> object for use by the <see cref="DispatchRequest"/> method.
  305. /// </summary>
  306. /// <param name="requestMessage">The incoming request message.</param>
  307. /// <param name="requestAction">The SOAP action of the request.</param>
  308. /// <param name="responseAction">The default SOAP action of the response.</param>
  309. /// <param name="trustNamespace">Namespace URI of the trust version of the incoming request.</param>
  310. /// <param name="requestSerializer">The <see cref="WSTrustRequestSerializer"/> used to deserialize
  311. /// incoming RST messages.</param>
  312. /// <param name="responseSerializer">The <see cref="WSTrustResponseSerializer"/> used to deserialize
  313. /// incoming RSTR messages.</param>
  314. /// <param name="serializationContext">The <see cref="WSTrustSerializationContext"/> to use
  315. /// when deserializing incoming messages.</param>
  316. /// <returns>A <see cref="DispatchContext"/> object.</returns>
  317. protected virtual DispatchContext CreateDispatchContext(Message requestMessage,
  318. string requestAction,
  319. string responseAction,
  320. string trustNamespace,
  321. WSTrustRequestSerializer requestSerializer,
  322. WSTrustResponseSerializer responseSerializer,
  323. WSTrustSerializationContext serializationContext)
  324. {
  325. DispatchContext dispatchContext = new DispatchContext()
  326. {
  327. Principal = OperationContext.Current.ClaimsPrincipal as ClaimsPrincipal,
  328. RequestAction = requestAction,
  329. ResponseAction = responseAction,
  330. TrustNamespace = trustNamespace
  331. };
  332. XmlReader requestBodyReader = requestMessage.GetReaderAtBodyContents();
  333. //
  334. // Take a peek at the request with the serializers to figure out if this is a standard incoming
  335. // RST or if this is an instance of a challenge-response style message pattern where an RSTR comes in.
  336. //
  337. if (requestSerializer.CanRead(requestBodyReader))
  338. {
  339. dispatchContext.RequestMessage = requestSerializer.ReadXml(requestBodyReader, serializationContext);
  340. }
  341. else if (responseSerializer.CanRead(requestBodyReader))
  342. {
  343. dispatchContext.RequestMessage = responseSerializer.ReadXml(requestBodyReader, serializationContext);
  344. }
  345. else
  346. {
  347. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  348. new InvalidRequestException(SR.GetString(SR.ID3114)));
  349. }
  350. //
  351. // CAUTION: Don't create the STS until after the RST or RSTR is deserialized or the test team
  352. // has major infrastructure problems.
  353. //
  354. dispatchContext.SecurityTokenService = CreateSTS();
  355. return dispatchContext;
  356. }
  357. /// <summary>
  358. /// Validates the DispatchContext.
  359. /// </summary>
  360. /// <param name="dispatchContext">The <see cref="DispatchContext"/> to validate.</param>
  361. /// <remarks>
  362. /// This routine ensures that the <see cref="DispatchContext"/> represents a legal request
  363. /// prior to being passed into <see cref="DispatchRequest"/>. This routine's default implementation
  364. /// is to reject incoming RST messages with RSTR actions and vice versa.
  365. /// </remarks>
  366. protected virtual void ValidateDispatchContext(DispatchContext dispatchContext)
  367. {
  368. if (dispatchContext.RequestMessage is RST
  369. && !IsValidRSTAction(dispatchContext))
  370. {
  371. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  372. new InvalidRequestException(
  373. SR.GetString(SR.ID3113, "RequestSecurityToken", dispatchContext.RequestAction)));
  374. }
  375. if (dispatchContext.RequestMessage is RSTR
  376. && !IsValidRSTRAction(dispatchContext))
  377. {
  378. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  379. new InvalidRequestException(
  380. SR.GetString(SR.ID3113, "RequestSecurityTokenResponse", dispatchContext.RequestAction)));
  381. }
  382. }
  383. /// <summary>
  384. /// Determines if the DispatchContext contains a valid request action for incoming RST messages.
  385. /// </summary>
  386. private static bool IsValidRSTAction(DispatchContext dispatchContext)
  387. {
  388. bool valid = false;
  389. string action = dispatchContext.RequestAction;
  390. if (dispatchContext.TrustNamespace == WSTrust13Constants.NamespaceURI)
  391. {
  392. switch (action)
  393. {
  394. case WSTrust13Constants.Actions.Cancel:
  395. case WSTrust13Constants.Actions.Issue:
  396. case WSTrust13Constants.Actions.Renew:
  397. case WSTrust13Constants.Actions.Validate:
  398. valid = true;
  399. break;
  400. }
  401. }
  402. if (dispatchContext.TrustNamespace == WSTrustFeb2005Constants.NamespaceURI)
  403. {
  404. switch (action)
  405. {
  406. case WSTrustFeb2005Constants.Actions.Cancel:
  407. case WSTrustFeb2005Constants.Actions.Issue:
  408. case WSTrustFeb2005Constants.Actions.Renew:
  409. case WSTrustFeb2005Constants.Actions.Validate:
  410. valid = true;
  411. break;
  412. }
  413. }
  414. return valid;
  415. }
  416. /// <summary>
  417. /// Determines if the DispatchContext contains a valid request action for incoming RSTR messages.
  418. /// </summary>
  419. private static bool IsValidRSTRAction(DispatchContext dispatchContext)
  420. {
  421. bool valid = false;
  422. string action = dispatchContext.RequestAction;
  423. if (dispatchContext.TrustNamespace == WSTrust13Constants.NamespaceURI)
  424. {
  425. switch (action)
  426. {
  427. case WSTrust13Constants.Actions.CancelFinalResponse:
  428. case WSTrust13Constants.Actions.CancelResponse:
  429. case WSTrust13Constants.Actions.IssueFinalResponse:
  430. case WSTrust13Constants.Actions.IssueResponse:
  431. case WSTrust13Constants.Actions.RenewFinalResponse:
  432. case WSTrust13Constants.Actions.RenewResponse:
  433. case WSTrust13Constants.Actions.ValidateFinalResponse:
  434. case WSTrust13Constants.Actions.ValidateResponse:
  435. valid = true;
  436. break;
  437. }
  438. }
  439. if (dispatchContext.TrustNamespace == WSTrustFeb2005Constants.NamespaceURI)
  440. {
  441. switch (action)
  442. {
  443. case WSTrustFeb2005Constants.Actions.CancelResponse:
  444. case WSTrustFeb2005Constants.Actions.IssueResponse:
  445. case WSTrustFeb2005Constants.Actions.RenewResponse:
  446. case WSTrustFeb2005Constants.Actions.ValidateResponse:
  447. valid = true;
  448. break;
  449. }
  450. }
  451. return valid;
  452. }
  453. private STS CreateSTS()
  454. {
  455. STS sts = _securityTokenServiceConfiguration.CreateSecurityTokenService();
  456. if (sts == null)
  457. {
  458. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID3002)));
  459. }
  460. return sts;
  461. }
  462. /// <summary>
  463. /// Handles Asynchronous call to the STS.
  464. /// </summary>
  465. /// <param name="requestMessage">Incoming Request message.</param>
  466. /// <param name="requestSerializer">Trust Request Serializer.</param>
  467. /// <param name="responseSerializer">Trust Response Serializer.</param>
  468. /// <param name="requestAction">Request SOAP action.</param>
  469. /// <param name="responseAction">Response SOAP action.</param>
  470. /// <param name="trustNamespace">Namespace URI of the trust version of the incoming request.</param>
  471. /// <param name="callback">Callback that gets invoked when the Asynchronous call ends.</param>
  472. /// <param name="state">state information of the Asynchronous call.</param>
  473. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  474. /// <exception cref="ArgumentNullException">One of the argument is null.</exception>
  475. protected virtual IAsyncResult BeginProcessCore(Message requestMessage, WSTrustRequestSerializer requestSerializer, WSTrustResponseSerializer responseSerializer, string requestAction, string responseAction, string trustNamespace, AsyncCallback callback, object state)
  476. {
  477. if (requestMessage == null)
  478. {
  479. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("request");
  480. }
  481. if (requestSerializer == null)
  482. {
  483. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("requestSerializer");
  484. }
  485. if (responseSerializer == null)
  486. {
  487. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("responseSerializer");
  488. }
  489. if (String.IsNullOrEmpty(requestAction))
  490. {
  491. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("requestAction");
  492. }
  493. if (String.IsNullOrEmpty(responseAction))
  494. {
  495. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("responseAction");
  496. }
  497. if (String.IsNullOrEmpty(trustNamespace))
  498. {
  499. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("trustNamespace");
  500. }
  501. IAsyncResult result = null;
  502. try
  503. {
  504. Fx.Assert(OperationContext.Current != null, "");
  505. Fx.Assert(OperationContext.Current.RequestContext != null, "");
  506. //
  507. // Create the Serialization and Dispatch context objects.
  508. //
  509. WSTrustSerializationContext serializationContext = CreateSerializationContext();
  510. DispatchContext dispatchContext = CreateDispatchContext(requestMessage,
  511. requestAction,
  512. responseAction,
  513. trustNamespace,
  514. requestSerializer,
  515. responseSerializer,
  516. serializationContext);
  517. //
  518. // Validate the dispatch context.
  519. //
  520. ValidateDispatchContext(dispatchContext);
  521. //
  522. // Dispatch the message asynchronously.
  523. //
  524. result = new ProcessCoreAsyncResult(this,
  525. dispatchContext,
  526. OperationContext.Current.RequestContext.RequestMessage.Version,
  527. responseSerializer,
  528. serializationContext,
  529. callback,
  530. state);
  531. }
  532. catch (Exception ex)
  533. {
  534. if (!HandleException(ex, trustNamespace, requestAction, requestMessage.Version.Envelope))
  535. {
  536. throw;
  537. }
  538. }
  539. return result;
  540. }
  541. /// <summary>
  542. /// Completes an Asynchronous call to the STS.
  543. /// </summary>
  544. /// <param name="ar">IAsyncResult that was returned by the call to the Asynchronous Begin method.</param>
  545. /// <param name="requestAction">Request SOAP Action.</param>
  546. /// <param name="responseAction">Response SOAP Action.</param>
  547. /// <param name="trustNamespace">Namespace URI of the current trust version.</param>
  548. /// <returns>Message that contains the serialized RST message.</returns>
  549. /// <exception cref="ArgumentNullException">One of the argument is null.</exception>
  550. protected virtual Message EndProcessCore(IAsyncResult ar, string requestAction, string responseAction, string trustNamespace)
  551. {
  552. if (ar == null)
  553. {
  554. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("ar");
  555. }
  556. ProcessCoreAsyncResult asyncResult = ar as ProcessCoreAsyncResult;
  557. if (asyncResult == null)
  558. {
  559. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.ID2004, typeof(ProcessCoreAsyncResult), ar.GetType()), "ar"));
  560. }
  561. Message message = null;
  562. try
  563. {
  564. message = ProcessCoreAsyncResult.End(ar);
  565. }
  566. catch (Exception ex)
  567. {
  568. if (!HandleException(ex, trustNamespace, requestAction, asyncResult.MessageVersion.Envelope))
  569. {
  570. throw;
  571. }
  572. }
  573. return message;
  574. }
  575. /// <summary>
  576. /// Raises the Error Event and converts the given exception to a FaultException if required. If the original
  577. /// exception was a FaultException or PreserveOriginalException flag is set to true then the conversion to
  578. /// FaultException is not done.
  579. /// </summary>
  580. /// <param name="ex">The original exception.</param>
  581. /// <param name="trustNamespace">Trust Namespace of the current trust version.</param>
  582. /// <param name="action">The Trust action that caused the exception.</param>
  583. /// <param name="requestEnvelopeVersion">Version of the request envolope.</param>
  584. protected virtual bool HandleException(Exception ex, string trustNamespace, string action, EnvelopeVersion requestEnvelopeVersion)
  585. {
  586. if (System.Runtime.Fx.IsFatal(ex))
  587. {
  588. return false;
  589. }
  590. if (DiagnosticUtility.ShouldTrace(TraceEventType.Warning))
  591. {
  592. TraceUtility.TraceString(
  593. TraceEventType.Warning,
  594. "RequestFailed: TrustNamespace={0}, Action={1}, Exception={2}",
  595. trustNamespace,
  596. action,
  597. ex);
  598. }
  599. // raise the exception events.
  600. if (_requestFailed != null)
  601. {
  602. _requestFailed(this, new WSTrustRequestProcessingErrorEventArgs(action, ex));
  603. }
  604. bool preserveOriginalException = false;
  605. ServiceDebugBehavior debugBehavior = OperationContext.Current.Host.Description.Behaviors.Find<ServiceDebugBehavior>();
  606. if (debugBehavior != null)
  607. {
  608. preserveOriginalException = debugBehavior.IncludeExceptionDetailInFaults;
  609. }
  610. if (String.IsNullOrEmpty(trustNamespace) || String.IsNullOrEmpty(action) || preserveOriginalException || ex is FaultException)
  611. {
  612. // Just throw the original exception.
  613. return false;
  614. }
  615. else
  616. {
  617. FaultException faultException = OperationContext.Current.Host.Credentials.ExceptionMapper.FromException(ex, (requestEnvelopeVersion == EnvelopeVersion.Soap11) ? soap11Namespace : soap12Namespace, trustNamespace);
  618. if (faultException != null)
  619. {
  620. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(faultException);
  621. }
  622. // The exception is not one of the recognized exceptions. Just throw the original exception.
  623. return false;
  624. }
  625. }
  626. #region IWSTrustFeb2005SyncContract and IWSTrust13SyncContract Methods
  627. /// <summary>
  628. /// Processes a Trust 1.3 Cancel message synchronously.
  629. /// </summary>
  630. /// <param name="message">Incoming Request message.</param>
  631. /// <returns>Message with the serialized response.</returns>
  632. public Message ProcessTrust13Cancel(Message message)
  633. {
  634. return ProcessCore(message, _securityTokenServiceConfiguration.WSTrust13RequestSerializer, _securityTokenServiceConfiguration.WSTrust13ResponseSerializer, WSTrust13Constants.Actions.Cancel, WSTrust13Constants.Actions.CancelFinalResponse, WSTrust13Constants.NamespaceURI);
  635. }
  636. /// <summary>
  637. /// Processes a Trust 1.3 Issue message synchronously.
  638. /// </summary>
  639. /// <param name="message">Incoming Request message.</param>
  640. /// <returns>Message with the serialized response.</returns>
  641. public Message ProcessTrust13Issue(Message message)
  642. {
  643. return ProcessCore(message, _securityTokenServiceConfiguration.WSTrust13RequestSerializer, _securityTokenServiceConfiguration.WSTrust13ResponseSerializer, WSTrust13Constants.Actions.Issue, WSTrust13Constants.Actions.IssueFinalResponse, WSTrust13Constants.NamespaceURI);
  644. }
  645. /// <summary>
  646. /// Processes a Trust 1.3 Renew message synchronously.
  647. /// </summary>
  648. /// <param name="message">Incoming Request message.</param>
  649. /// <returns>Message with the serialized response.</returns>
  650. public Message ProcessTrust13Renew(Message message)
  651. {
  652. return ProcessCore(message, _securityTokenServiceConfiguration.WSTrust13RequestSerializer, _securityTokenServiceConfiguration.WSTrust13ResponseSerializer, WSTrust13Constants.Actions.Renew, WSTrust13Constants.Actions.RenewFinalResponse, WSTrust13Constants.NamespaceURI);
  653. }
  654. /// <summary>
  655. /// Processes a Trust 1.3 Validate message synchronously.
  656. /// </summary>
  657. /// <param name="message">Incoming Request message.</param>
  658. /// <returns>Message with the serialized response.</returns>
  659. public Message ProcessTrust13Validate(Message message)
  660. {
  661. return ProcessCore(message, _securityTokenServiceConfiguration.WSTrust13RequestSerializer, _securityTokenServiceConfiguration.WSTrust13ResponseSerializer, WSTrust13Constants.Actions.Validate, WSTrust13Constants.Actions.ValidateFinalResponse, WSTrust13Constants.NamespaceURI);
  662. }
  663. /// <summary>
  664. /// Processes a Trust 1.3 RSTR/Cancel message synchronously.
  665. /// </summary>
  666. /// <param name="message">Incoming Request message.</param>
  667. /// <returns>Message with the serialized response.</returns>
  668. public Message ProcessTrust13CancelResponse(Message message)
  669. {
  670. return ProcessCore(message,
  671. _securityTokenServiceConfiguration.WSTrust13RequestSerializer,
  672. _securityTokenServiceConfiguration.WSTrust13ResponseSerializer,
  673. WSTrust13Constants.Actions.CancelResponse,
  674. WSTrust13Constants.Actions.CancelFinalResponse,
  675. WSTrust13Constants.NamespaceURI);
  676. }
  677. /// <summary>
  678. /// Processes a Trust 1.3 RSTR/Issue message synchronously.
  679. /// </summary>
  680. /// <param name="message">Incoming Request message.</param>
  681. /// <returns>Message with the serialized response.</returns>
  682. public Message ProcessTrust13IssueResponse(Message message)
  683. {
  684. return ProcessCore(message,
  685. _securityTokenServiceConfiguration.WSTrust13RequestSerializer,
  686. _securityTokenServiceConfiguration.WSTrust13ResponseSerializer,
  687. WSTrust13Constants.Actions.IssueResponse,
  688. WSTrust13Constants.Actions.IssueFinalResponse,
  689. WSTrust13Constants.NamespaceURI);
  690. }
  691. /// <summary>
  692. /// Processes a Trust 1.3 RSTR/Renew message synchronously.
  693. /// </summary>
  694. /// <param name="message">Incoming Request message.</param>
  695. /// <returns>Message with the serialized response.</returns>
  696. public Message ProcessTrust13RenewResponse(Message message)
  697. {
  698. return ProcessCore(message,
  699. _securityTokenServiceConfiguration.WSTrust13RequestSerializer,
  700. _securityTokenServiceConfiguration.WSTrust13ResponseSerializer,
  701. WSTrust13Constants.Actions.RenewResponse,
  702. WSTrust13Constants.Actions.RenewFinalResponse,
  703. WSTrust13Constants.NamespaceURI);
  704. }
  705. /// <summary>
  706. /// Processes a Trust 1.3 RSTR/Validate message synchronously.
  707. /// </summary>
  708. /// <param name="message">Incoming Request message.</param>
  709. /// <returns>Message with the serialized response.</returns>
  710. public Message ProcessTrust13ValidateResponse(Message message)
  711. {
  712. return ProcessCore(message,
  713. _securityTokenServiceConfiguration.WSTrust13RequestSerializer,
  714. _securityTokenServiceConfiguration.WSTrust13ResponseSerializer,
  715. WSTrust13Constants.Actions.ValidateResponse,
  716. WSTrust13Constants.Actions.ValidateFinalResponse,
  717. WSTrust13Constants.NamespaceURI);
  718. }
  719. /// <summary>
  720. /// Processes a Trust Feb 2005 Cancel message synchronously.
  721. /// </summary>
  722. /// <param name="message">Incoming Request message.</param>
  723. /// <returns>Message with the serialized response.</returns>
  724. public Message ProcessTrustFeb2005Cancel(Message message)
  725. {
  726. return ProcessCore(message, _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer, _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer, WSTrustFeb2005Constants.Actions.Cancel, WSTrustFeb2005Constants.Actions.CancelResponse, WSTrustFeb2005Constants.NamespaceURI);
  727. }
  728. /// <summary>
  729. /// Processes a Trust Feb 2005 Issue message synchronously.
  730. /// </summary>
  731. /// <param name="message">Incoming Request message.</param>
  732. /// <returns>Message with the serialized response.</returns>
  733. public Message ProcessTrustFeb2005Issue(Message message)
  734. {
  735. return ProcessCore(message, _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer, _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer, WSTrustFeb2005Constants.Actions.Issue, WSTrustFeb2005Constants.Actions.IssueResponse, WSTrustFeb2005Constants.NamespaceURI);
  736. }
  737. /// <summary>
  738. /// Processes a Trust Feb 2005 Renew message synchronously.
  739. /// </summary>
  740. /// <param name="message">Incoming Request message.</param>
  741. /// <returns>Message with the serialized response.</returns>
  742. public Message ProcessTrustFeb2005Renew(Message message)
  743. {
  744. return ProcessCore(message, _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer, _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer, WSTrustFeb2005Constants.Actions.Renew, WSTrustFeb2005Constants.Actions.RenewResponse, WSTrustFeb2005Constants.NamespaceURI);
  745. }
  746. /// <summary>
  747. /// Processes a Trust Feb 2005 Validate message synchronously.
  748. /// </summary>
  749. /// <param name="message">Incoming Request message.</param>
  750. /// <returns>Message with the serialized response.</returns>
  751. public Message ProcessTrustFeb2005Validate(Message message)
  752. {
  753. return ProcessCore(message, _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer, _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer, WSTrustFeb2005Constants.Actions.Validate, WSTrustFeb2005Constants.Actions.ValidateResponse, WSTrustFeb2005Constants.NamespaceURI);
  754. }
  755. /// <summary>
  756. /// Processes a Trust Feb 2005 RSTR/Cancel message synchronously.
  757. /// </summary>
  758. /// <param name="message">Incoming Request message.</param>
  759. /// <returns>Message with the serialized response.</returns>
  760. public Message ProcessTrustFeb2005CancelResponse(Message message)
  761. {
  762. return ProcessCore(message,
  763. _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer,
  764. _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer,
  765. WSTrustFeb2005Constants.Actions.CancelResponse,
  766. WSTrustFeb2005Constants.Actions.CancelResponse,
  767. WSTrustFeb2005Constants.NamespaceURI);
  768. }
  769. /// <summary>
  770. /// Processes a Trust Feb 2005 RSTR/Issue message synchronously.
  771. /// </summary>
  772. /// <param name="message">Incoming Request message.</param>
  773. /// <returns>Message with the serialized response.</returns>
  774. public Message ProcessTrustFeb2005IssueResponse(Message message)
  775. {
  776. return ProcessCore(message,
  777. _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer,
  778. _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer,
  779. WSTrustFeb2005Constants.Actions.IssueResponse,
  780. WSTrustFeb2005Constants.Actions.IssueResponse,
  781. WSTrustFeb2005Constants.NamespaceURI);
  782. }
  783. /// <summary>
  784. /// Processes a Trust Feb 2005 RSTR/Renew message synchronously.
  785. /// </summary>
  786. /// <param name="message">Incoming Request message.</param>
  787. /// <returns>Message with the serialized response.</returns>
  788. public Message ProcessTrustFeb2005RenewResponse(Message message)
  789. {
  790. return ProcessCore(message,
  791. _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer,
  792. _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer,
  793. WSTrustFeb2005Constants.Actions.RenewResponse,
  794. WSTrustFeb2005Constants.Actions.RenewResponse,
  795. WSTrustFeb2005Constants.NamespaceURI);
  796. }
  797. /// <summary>
  798. /// Processes a Trust Feb 2005 RSTR/Validate message synchronously.
  799. /// </summary>
  800. /// <param name="message">Incoming Request message.</param>
  801. /// <returns>Message with the serialized response.</returns>
  802. public Message ProcessTrustFeb2005ValidateResponse(Message message)
  803. {
  804. return ProcessCore(message,
  805. _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer,
  806. _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer,
  807. WSTrustFeb2005Constants.Actions.ValidateResponse,
  808. WSTrustFeb2005Constants.Actions.ValidateResponse,
  809. WSTrustFeb2005Constants.NamespaceURI);
  810. }
  811. /// <summary>
  812. /// Gets the SecurityTokenServiceConfiguration
  813. /// </summary>
  814. public SecurityTokenServiceConfiguration SecurityTokenServiceConfiguration
  815. {
  816. get
  817. {
  818. return _securityTokenServiceConfiguration;
  819. }
  820. }
  821. #endregion
  822. #region IWSTrustFeb2005AsyncContract and IWSTrust13AsyncContract Methods
  823. /// <summary>
  824. /// Processes an Asynchronous call to Trust Feb 1.3 Cancel message.
  825. /// </summary>
  826. /// <param name="request">Incoming Request message.</param>
  827. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  828. /// <param name="state">Asynchronous state.</param>
  829. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  830. public IAsyncResult BeginTrust13Cancel(Message request, AsyncCallback callback, object state)
  831. {
  832. return BeginProcessCore(request, _securityTokenServiceConfiguration.WSTrust13RequestSerializer, _securityTokenServiceConfiguration.WSTrust13ResponseSerializer, WSTrust13Constants.Actions.Cancel, WSTrust13Constants.Actions.CancelFinalResponse, WSTrust13Constants.NamespaceURI, callback, state);
  833. }
  834. /// <summary>
  835. /// Completes an Asynchronous call to Trust 1.3 Cancel message.
  836. /// </summary>
  837. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  838. /// <returns>Message containing the Serialized RSTR.</returns>
  839. public Message EndTrust13Cancel(IAsyncResult ar)
  840. {
  841. return EndProcessCore(ar, WSTrust13Constants.Actions.Cancel, WSTrust13Constants.Actions.CancelFinalResponse, WSTrust13Constants.NamespaceURI);
  842. }
  843. /// <summary>
  844. /// Processes an Asynchronous call to Trust 1.3 Issue message.
  845. /// </summary>
  846. /// <param name="request">Incoming Request message.</param>
  847. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  848. /// <param name="state">Asynchronous state.</param>
  849. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  850. public IAsyncResult BeginTrust13Issue(Message request, AsyncCallback callback, object state)
  851. {
  852. return BeginProcessCore(request, _securityTokenServiceConfiguration.WSTrust13RequestSerializer, _securityTokenServiceConfiguration.WSTrust13ResponseSerializer, WSTrust13Constants.Actions.Issue, WSTrust13Constants.Actions.IssueFinalResponse, WSTrust13Constants.NamespaceURI, callback, state);
  853. }
  854. /// <summary>
  855. /// Completes an Asynchronous call to Trust 1.3 Issue message.
  856. /// </summary>
  857. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  858. /// <returns>Message containing the Serialized RSTR.</returns>
  859. public Message EndTrust13Issue(IAsyncResult ar)
  860. {
  861. return EndProcessCore(ar, WSTrust13Constants.Actions.Issue, WSTrust13Constants.Actions.IssueFinalResponse, WSTrust13Constants.NamespaceURI);
  862. }
  863. /// <summary>
  864. /// Processes an Asynchronous call to Trust 1.3 Renew message.
  865. /// </summary>
  866. /// <param name="request">Incoming Request message.</param>
  867. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  868. /// <param name="state">Asynchronous state.</param>
  869. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  870. public IAsyncResult BeginTrust13Renew(Message request, AsyncCallback callback, object state)
  871. {
  872. return BeginProcessCore(request, _securityTokenServiceConfiguration.WSTrust13RequestSerializer, _securityTokenServiceConfiguration.WSTrust13ResponseSerializer, WSTrust13Constants.Actions.Renew, WSTrust13Constants.Actions.RenewFinalResponse, WSTrust13Constants.NamespaceURI, callback, state);
  873. }
  874. /// <summary>
  875. /// Completes an Asynchronous call to Trust 1.3 Renew message.
  876. /// </summary>
  877. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  878. /// <returns>Message containing the Serialized RSTR.</returns>
  879. public Message EndTrust13Renew(IAsyncResult ar)
  880. {
  881. return EndProcessCore(ar, WSTrust13Constants.Actions.Renew, WSTrust13Constants.Actions.RenewFinalResponse, WSTrust13Constants.NamespaceURI);
  882. }
  883. /// <summary>
  884. /// Processes an Asynchronous call to Trust 1.3 Validate message.
  885. /// </summary>
  886. /// <param name="request">Incoming Request message.</param>
  887. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  888. /// <param name="state">Asynchronous state.</param>
  889. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  890. public IAsyncResult BeginTrust13Validate(Message request, AsyncCallback callback, object state)
  891. {
  892. return BeginProcessCore(request, _securityTokenServiceConfiguration.WSTrust13RequestSerializer, _securityTokenServiceConfiguration.WSTrust13ResponseSerializer, WSTrust13Constants.Actions.Validate, WSTrust13Constants.Actions.ValidateFinalResponse, WSTrust13Constants.NamespaceURI, callback, state);
  893. }
  894. /// <summary>
  895. /// Completes an Asynchronous call to Trust 1.3 Validate message.
  896. /// </summary>
  897. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  898. /// <returns>Message containing the Serialized RSTR.</returns>
  899. public Message EndTrust13Validate(IAsyncResult ar)
  900. {
  901. return EndProcessCore(ar, WSTrust13Constants.Actions.Validate, WSTrust13Constants.Actions.ValidateFinalResponse, WSTrust13Constants.NamespaceURI);
  902. }
  903. /// <summary>
  904. /// Processes an Asynchronous call to Trust 1.3 RSTR/Cancel message.
  905. /// </summary>
  906. /// <param name="request">Incoming Request message.</param>
  907. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  908. /// <param name="state">Asynchronous state.</param>
  909. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  910. public IAsyncResult BeginTrust13CancelResponse(Message request, AsyncCallback callback, object state)
  911. {
  912. return BeginProcessCore(request,
  913. _securityTokenServiceConfiguration.WSTrust13RequestSerializer,
  914. _securityTokenServiceConfiguration.WSTrust13ResponseSerializer,
  915. WSTrust13Constants.Actions.CancelResponse,
  916. WSTrust13Constants.Actions.CancelFinalResponse,
  917. WSTrust13Constants.NamespaceURI,
  918. callback,
  919. state);
  920. }
  921. /// <summary>
  922. /// Completes an Asynchronous call to Trust 1.3 RSTR/Cancel message.
  923. /// </summary>
  924. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  925. /// <returns>Message containing the Serialized RSTR.</returns>
  926. public Message EndTrust13CancelResponse(IAsyncResult ar)
  927. {
  928. return EndProcessCore(ar,
  929. WSTrust13Constants.Actions.CancelResponse,
  930. WSTrust13Constants.Actions.CancelFinalResponse,
  931. WSTrust13Constants.NamespaceURI);
  932. }
  933. /// <summary>
  934. /// Processes an Asynchronous call to Trust 1.3 RSTR/Issue message.
  935. /// </summary>
  936. /// <param name="request">Incoming Request message.</param>
  937. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  938. /// <param name="state">Asynchronous state.</param>
  939. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  940. public IAsyncResult BeginTrust13IssueResponse(Message request, AsyncCallback callback, object state)
  941. {
  942. return BeginProcessCore(request,
  943. _securityTokenServiceConfiguration.WSTrust13RequestSerializer,
  944. _securityTokenServiceConfiguration.WSTrust13ResponseSerializer,
  945. WSTrust13Constants.Actions.IssueResponse,
  946. WSTrust13Constants.Actions.IssueFinalResponse,
  947. WSTrust13Constants.NamespaceURI,
  948. callback,
  949. state);
  950. }
  951. /// <summary>
  952. /// Completes an Asynchronous call to Trust 1.3 RSTR/Issue message.
  953. /// </summary>
  954. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  955. /// <returns>Message containing the Serialized RSTR.</returns>
  956. public Message EndTrust13IssueResponse(IAsyncResult ar)
  957. {
  958. return EndProcessCore(ar,
  959. WSTrust13Constants.Actions.IssueResponse,
  960. WSTrust13Constants.Actions.IssueFinalResponse,
  961. WSTrust13Constants.NamespaceURI);
  962. }
  963. /// <summary>
  964. /// Processes an Asynchronous call to Trust 1.3 RSTR/Renew message.
  965. /// </summary>
  966. /// <param name="request">Incoming Request message.</param>
  967. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  968. /// <param name="state">Asynchronous state.</param>
  969. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  970. public IAsyncResult BeginTrust13RenewResponse(Message request, AsyncCallback callback, object state)
  971. {
  972. return BeginProcessCore(request,
  973. _securityTokenServiceConfiguration.WSTrust13RequestSerializer,
  974. _securityTokenServiceConfiguration.WSTrust13ResponseSerializer,
  975. WSTrust13Constants.Actions.RenewResponse,
  976. WSTrust13Constants.Actions.RenewFinalResponse,
  977. WSTrust13Constants.NamespaceURI,
  978. callback,
  979. state);
  980. }
  981. /// <summary>
  982. /// Completes an Asynchronous call to Trust 1.3 RSTR/Renew message.
  983. /// </summary>
  984. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  985. /// <returns>Message containing the Serialized RSTR.</returns>
  986. public Message EndTrust13RenewResponse(IAsyncResult ar)
  987. {
  988. return EndProcessCore(ar,
  989. WSTrust13Constants.Actions.RenewResponse,
  990. WSTrust13Constants.Actions.RenewFinalResponse,
  991. WSTrust13Constants.NamespaceURI);
  992. }
  993. /// <summary>
  994. /// Processes an Asynchronous call to Trust 1.3 RSTR/Validate message.
  995. /// </summary>
  996. /// <param name="request">Incoming Request message.</param>
  997. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  998. /// <param name="state">Asynchronous state.</param>
  999. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1000. public IAsyncResult BeginTrust13ValidateResponse(Message request, AsyncCallback callback, object state)
  1001. {
  1002. return BeginProcessCore(request,
  1003. _securityTokenServiceConfiguration.WSTrust13RequestSerializer,
  1004. _securityTokenServiceConfiguration.WSTrust13ResponseSerializer,
  1005. WSTrust13Constants.Actions.ValidateResponse,
  1006. WSTrust13Constants.Actions.ValidateFinalResponse,
  1007. WSTrust13Constants.NamespaceURI,
  1008. callback,
  1009. state);
  1010. }
  1011. /// <summary>
  1012. /// Completes an Asynchronous call to Trust 1.3 RSTR/Validate message.
  1013. /// </summary>
  1014. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1015. /// <returns>Message containing the Serialized RSTR.</returns>
  1016. public Message EndTrust13ValidateResponse(IAsyncResult ar)
  1017. {
  1018. return EndProcessCore(ar,
  1019. WSTrust13Constants.Actions.ValidateResponse,
  1020. WSTrust13Constants.Actions.ValidateFinalResponse,
  1021. WSTrust13Constants.NamespaceURI);
  1022. }
  1023. /// <summary>
  1024. /// Processes an Asynchronous call to Trust 2005 Cancel message.
  1025. /// </summary>
  1026. /// <param name="request">Incoming Request message.</param>
  1027. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  1028. /// <param name="state">Asynchronous state.</param>
  1029. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1030. public IAsyncResult BeginTrustFeb2005Cancel(Message request, AsyncCallback callback, object state)
  1031. {
  1032. return BeginProcessCore(request, _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer, _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer, WSTrustFeb2005Constants.Actions.Cancel, WSTrustFeb2005Constants.Actions.CancelResponse, WSTrustFeb2005Constants.NamespaceURI, callback, state);
  1033. }
  1034. /// <summary>
  1035. /// Completes an Asynchronous call to Trust Feb 2005 Cancel message.
  1036. /// </summary>
  1037. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1038. /// <returns>Message containing the Serialized RSTR.</returns>
  1039. public Message EndTrustFeb2005Cancel(IAsyncResult ar)
  1040. {
  1041. return EndProcessCore(ar, WSTrustFeb2005Constants.Actions.Cancel, WSTrustFeb2005Constants.Actions.CancelResponse, WSTrustFeb2005Constants.NamespaceURI);
  1042. }
  1043. /// <summary>
  1044. /// Processes an Asynchronous call to Trust Feb 2005 Issue message.
  1045. /// </summary>
  1046. /// <param name="request">Incoming Request message.</param>
  1047. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  1048. /// <param name="state">Asynchronous state.</param>
  1049. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1050. public IAsyncResult BeginTrustFeb2005Issue(Message request, AsyncCallback callback, object state)
  1051. {
  1052. return BeginProcessCore(request, _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer, _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer, WSTrustFeb2005Constants.Actions.Issue, WSTrustFeb2005Constants.Actions.IssueResponse, WSTrustFeb2005Constants.NamespaceURI, callback, state);
  1053. }
  1054. /// <summary>
  1055. /// Completes an Asynchronous call to Trust Feb 2005 Issue message.
  1056. /// </summary>
  1057. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1058. /// <returns>Message containing the Serialized RSTR.</returns>
  1059. public Message EndTrustFeb2005Issue(IAsyncResult ar)
  1060. {
  1061. return EndProcessCore(ar, WSTrustFeb2005Constants.Actions.Issue, WSTrustFeb2005Constants.Actions.IssueResponse, WSTrustFeb2005Constants.NamespaceURI);
  1062. }
  1063. /// <summary>
  1064. /// Processes an Asynchronous call to Trust Feb 2005 Renew message.
  1065. /// </summary>
  1066. /// <param name="request">Incoming Request message.</param>
  1067. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  1068. /// <param name="state">Asynchronous state.</param>
  1069. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1070. public IAsyncResult BeginTrustFeb2005Renew(Message request, AsyncCallback callback, object state)
  1071. {
  1072. return BeginProcessCore(request, _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer, _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer, WSTrustFeb2005Constants.Actions.Renew, WSTrustFeb2005Constants.Actions.RenewResponse, WSTrustFeb2005Constants.NamespaceURI, callback, state);
  1073. }
  1074. /// <summary>
  1075. /// Completes an Asynchronous call to Trust Feb 2005 Renew message.
  1076. /// </summary>
  1077. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1078. /// <returns>Message containing the Serialized RSTR.</returns>
  1079. public Message EndTrustFeb2005Renew(IAsyncResult ar)
  1080. {
  1081. return EndProcessCore(ar, WSTrustFeb2005Constants.Actions.Renew, WSTrustFeb2005Constants.Actions.RenewResponse, WSTrustFeb2005Constants.NamespaceURI);
  1082. }
  1083. /// <summary>
  1084. /// Processes an Asynchronous call to Trust Feb 2005 Validate message.
  1085. /// </summary>
  1086. /// <param name="request">Incoming Request message.</param>
  1087. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  1088. /// <param name="state">Asynchronous state.</param>
  1089. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1090. public IAsyncResult BeginTrustFeb2005Validate(Message request, AsyncCallback callback, object state)
  1091. {
  1092. return BeginProcessCore(request, _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer, _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer, WSTrustFeb2005Constants.Actions.Validate, WSTrustFeb2005Constants.Actions.ValidateResponse, WSTrustFeb2005Constants.NamespaceURI, callback, state);
  1093. }
  1094. /// <summary>
  1095. /// Completes an Asynchronous call to Trust Feb 2005 Validate message.
  1096. /// </summary>
  1097. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1098. /// <returns>Message containing the Serialized RSTR.</returns>
  1099. public Message EndTrustFeb2005Validate(IAsyncResult ar)
  1100. {
  1101. return EndProcessCore(ar, WSTrustFeb2005Constants.Actions.Validate, WSTrustFeb2005Constants.Actions.ValidateResponse, WSTrustFeb2005Constants.NamespaceURI);
  1102. }
  1103. /// <summary>
  1104. /// Processes an Asynchronous call to Trust Feb 2005 RSTR/Cancel message.
  1105. /// </summary>
  1106. /// <param name="request">Incoming Request message.</param>
  1107. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  1108. /// <param name="state">Asynchronous state.</param>
  1109. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1110. public IAsyncResult BeginTrustFeb2005CancelResponse(Message request, AsyncCallback callback, object state)
  1111. {
  1112. return BeginProcessCore(request,
  1113. _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer,
  1114. _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer,
  1115. WSTrustFeb2005Constants.Actions.CancelResponse,
  1116. WSTrustFeb2005Constants.Actions.CancelResponse,
  1117. WSTrustFeb2005Constants.NamespaceURI,
  1118. callback,
  1119. state);
  1120. }
  1121. /// <summary>
  1122. /// Completes an Asynchronous call to Trust Feb 2005 RSTR/Cancel message.
  1123. /// </summary>
  1124. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1125. /// <returns>Message containing the Serialized RSTR.</returns>
  1126. public Message EndTrustFeb2005CancelResponse(IAsyncResult ar)
  1127. {
  1128. return EndProcessCore(ar,
  1129. WSTrustFeb2005Constants.Actions.CancelResponse,
  1130. WSTrustFeb2005Constants.Actions.CancelResponse,
  1131. WSTrustFeb2005Constants.NamespaceURI);
  1132. }
  1133. /// <summary>
  1134. /// Processes an Asynchronous call to Trust Feb 2005 RSTR/Issue message.
  1135. /// </summary>
  1136. /// <param name="request">Incoming Request message.</param>
  1137. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  1138. /// <param name="state">Asynchronous state.</param>
  1139. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1140. public IAsyncResult BeginTrustFeb2005IssueResponse(Message request, AsyncCallback callback, object state)
  1141. {
  1142. return BeginProcessCore(request,
  1143. _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer,
  1144. _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer,
  1145. WSTrustFeb2005Constants.Actions.IssueResponse,
  1146. WSTrustFeb2005Constants.Actions.IssueResponse,
  1147. WSTrustFeb2005Constants.NamespaceURI,
  1148. callback,
  1149. state);
  1150. }
  1151. /// <summary>
  1152. /// Completes an Asynchronous call to Trust Feb 2005 RSTR/Issue message.
  1153. /// </summary>
  1154. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1155. /// <returns>Message containing the Serialized RSTR.</returns>
  1156. public Message EndTrustFeb2005IssueResponse(IAsyncResult ar)
  1157. {
  1158. return EndProcessCore(ar,
  1159. WSTrustFeb2005Constants.Actions.IssueResponse,
  1160. WSTrustFeb2005Constants.Actions.IssueResponse,
  1161. WSTrustFeb2005Constants.NamespaceURI);
  1162. }
  1163. /// <summary>
  1164. /// Processes an Asynchronous call to Trust Feb 2005 RSTR/Renew message.
  1165. /// </summary>
  1166. /// <param name="request">Incoming Request message.</param>
  1167. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  1168. /// <param name="state">Asynchronous state.</param>
  1169. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1170. public IAsyncResult BeginTrustFeb2005RenewResponse(Message request, AsyncCallback callback, object state)
  1171. {
  1172. return BeginProcessCore(request,
  1173. _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer,
  1174. _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer,
  1175. WSTrustFeb2005Constants.Actions.RenewResponse,
  1176. WSTrustFeb2005Constants.Actions.RenewResponse,
  1177. WSTrustFeb2005Constants.NamespaceURI,
  1178. callback,
  1179. state);
  1180. }
  1181. /// <summary>
  1182. /// Completes an Asynchronous call to Trust Feb 2005 RSTR/Renew message.
  1183. /// </summary>
  1184. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1185. /// <returns>Message containing the Serialized RSTR.</returns>
  1186. public Message EndTrustFeb2005RenewResponse(IAsyncResult ar)
  1187. {
  1188. return EndProcessCore(ar,
  1189. WSTrustFeb2005Constants.Actions.RenewResponse,
  1190. WSTrustFeb2005Constants.Actions.RenewResponse,
  1191. WSTrustFeb2005Constants.NamespaceURI);
  1192. }
  1193. /// <summary>
  1194. /// Processes an Asynchronous call to Trust Feb 2005 RSTR/Validate message.
  1195. /// </summary>
  1196. /// <param name="request">Incoming Request message.</param>
  1197. /// <param name="callback">Callback to be invoked when the Asynchronous operation ends.</param>
  1198. /// <param name="state">Asynchronous state.</param>
  1199. /// <returns>IAsyncResult that should be passed back to the End method to complete the Asynchronous call.</returns>
  1200. public IAsyncResult BeginTrustFeb2005ValidateResponse(Message request, AsyncCallback callback, object state)
  1201. {
  1202. return BeginProcessCore(request,
  1203. _securityTokenServiceConfiguration.WSTrustFeb2005RequestSerializer,
  1204. _securityTokenServiceConfiguration.WSTrustFeb2005ResponseSerializer,
  1205. WSTrustFeb2005Constants.Actions.ValidateResponse,
  1206. WSTrustFeb2005Constants.Actions.ValidateResponse,
  1207. WSTrustFeb2005Constants.NamespaceURI,
  1208. callback,
  1209. state);
  1210. }
  1211. /// <summary>
  1212. /// Completes an Asynchronous call to Trust Feb 2005 RSTR/Validate message.
  1213. /// </summary>
  1214. /// <param name="ar">IAsyncResult object returned by the Begin method that started the Asynchronous call.</param>
  1215. /// <returns>Message containing the Serialized RSTR.</returns>
  1216. public Message EndTrustFeb2005ValidateResponse(IAsyncResult ar)
  1217. {
  1218. return EndProcessCore(ar,
  1219. WSTrustFeb2005Constants.Actions.ValidateResponse,
  1220. WSTrustFeb2005Constants.Actions.ValidateResponse,
  1221. WSTrustFeb2005Constants.NamespaceURI);
  1222. }
  1223. #endregion
  1224. //
  1225. // An async result class that represents the async version of the ProcessCore method.
  1226. //
  1227. internal class ProcessCoreAsyncResult : AsyncResult
  1228. {
  1229. //
  1230. // Encapsulate the local variables in the [....] version of ProcessCore as fields.
  1231. //
  1232. WSTrustServiceContract _trustServiceContract;
  1233. DispatchContext _dispatchContext;
  1234. MessageVersion _messageVersion;
  1235. WSTrustResponseSerializer _responseSerializer;
  1236. WSTrustSerializationContext _serializationContext;
  1237. public ProcessCoreAsyncResult(WSTrustServiceContract contract,
  1238. DispatchContext dispatchContext,
  1239. MessageVersion messageVersion,
  1240. WSTrustResponseSerializer responseSerializer,
  1241. WSTrustSerializationContext serializationContext,
  1242. AsyncCallback asyncCallback,
  1243. object asyncState)
  1244. : base(asyncCallback, asyncState)
  1245. {
  1246. if (contract == null)
  1247. {
  1248. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contract");
  1249. }
  1250. if (dispatchContext == null)
  1251. {
  1252. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("dispatchContext");
  1253. }
  1254. if (responseSerializer == null)
  1255. {
  1256. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("responseSerializer");
  1257. }
  1258. if (serializationContext == null)
  1259. {
  1260. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializationContext");
  1261. }
  1262. _trustServiceContract = contract;
  1263. _dispatchContext = dispatchContext;
  1264. _messageVersion = messageVersion;
  1265. _responseSerializer = responseSerializer;
  1266. _serializationContext = serializationContext;
  1267. contract.BeginDispatchRequest(dispatchContext, OnDispatchRequestCompleted, null);
  1268. }
  1269. public WSTrustServiceContract TrustServiceContract
  1270. {
  1271. get { return _trustServiceContract; }
  1272. }
  1273. public DispatchContext DispatchContext
  1274. {
  1275. get { return _dispatchContext; }
  1276. }
  1277. public MessageVersion MessageVersion
  1278. {
  1279. get { return _messageVersion; }
  1280. }
  1281. public WSTrustResponseSerializer ResponseSerializer
  1282. {
  1283. get { return _responseSerializer; }
  1284. }
  1285. public WSTrustSerializationContext SerializationContext
  1286. {
  1287. get { return _serializationContext; }
  1288. }
  1289. public new static Message End(IAsyncResult ar)
  1290. {
  1291. AsyncResult.End(ar);
  1292. ProcessCoreAsyncResult pcar = ar as ProcessCoreAsyncResult;
  1293. if (pcar == null)
  1294. {
  1295. throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID2004, typeof(ProcessCoreAsyncResult), ar.GetType()));
  1296. }
  1297. //
  1298. // Create the response Message object with the appropriate action.
  1299. //
  1300. return Message.CreateMessage(OperationContext.Current.RequestContext.RequestMessage.Version,
  1301. pcar.DispatchContext.ResponseAction,
  1302. new WSTrustResponseBodyWriter(pcar.DispatchContext.ResponseMessage,
  1303. pcar.ResponseSerializer,
  1304. pcar.SerializationContext));
  1305. }
  1306. //
  1307. // Asynchronously invoked when WSTrustServiceContract.BeginDispatchRequest completes.
  1308. //
  1309. private void OnDispatchRequestCompleted(IAsyncResult ar)
  1310. {
  1311. try
  1312. {
  1313. _dispatchContext = _trustServiceContract.EndDispatchRequest(ar);
  1314. this.Complete(false);
  1315. }
  1316. catch (Exception ex)
  1317. {
  1318. if (System.Runtime.Fx.IsFatal(ex))
  1319. {
  1320. throw;
  1321. }
  1322. this.Complete(false, ex);
  1323. }
  1324. }
  1325. }
  1326. //
  1327. // AsyncResult to encapsulate the default async implementation of DispatchRequest
  1328. //
  1329. internal class DispatchRequestAsyncResult : AsyncResult
  1330. {
  1331. DispatchContext _dispatchContext;
  1332. public DispatchContext DispatchContext
  1333. {
  1334. get { return _dispatchContext; }
  1335. }
  1336. public DispatchRequestAsyncResult(DispatchContext dispatchContext, AsyncCallback asyncCallback, object asyncState)
  1337. : base(asyncCallback, asyncState)
  1338. {
  1339. _dispatchContext = dispatchContext;
  1340. ClaimsPrincipal icp = dispatchContext.Principal;
  1341. RST rst = dispatchContext.RequestMessage as RST;
  1342. STS sts = dispatchContext.SecurityTokenService;
  1343. if (rst == null)
  1344. {
  1345. this.Complete(true, DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidRequestException(SR.GetString(SR.ID3023))));
  1346. return;
  1347. }
  1348. switch (rst.RequestType)
  1349. {
  1350. case RequestTypes.Cancel:
  1351. sts.BeginCancel(icp, rst, OnCancelComplete, null);
  1352. break;
  1353. case RequestTypes.Issue:
  1354. sts.BeginIssue(icp, rst, OnIssueComplete, null);
  1355. break;
  1356. case RequestTypes.Renew:
  1357. sts.BeginRenew(icp, rst, OnRenewComplete, null);
  1358. break;
  1359. case RequestTypes.Validate:
  1360. sts.BeginValidate(icp, rst, OnValidateComplete, null);
  1361. break;
  1362. default:
  1363. this.Complete(true, DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID3112, rst.RequestType))));
  1364. break;
  1365. }
  1366. }
  1367. public new static DispatchContext End(IAsyncResult ar)
  1368. {
  1369. AsyncResult.End(ar);
  1370. DispatchRequestAsyncResult dcar = ar as DispatchRequestAsyncResult;
  1371. if (dcar == null)
  1372. {
  1373. throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID2004, typeof(DispatchRequestAsyncResult), ar.GetType()));
  1374. }
  1375. return dcar.DispatchContext;
  1376. }
  1377. void OnCancelComplete(IAsyncResult ar)
  1378. {
  1379. try
  1380. {
  1381. _dispatchContext.ResponseMessage = _dispatchContext.SecurityTokenService.EndCancel(ar);
  1382. Complete(false);
  1383. }
  1384. catch (Exception e)
  1385. {
  1386. System.ServiceModel.DiagnosticUtility.TraceHandledException(e, TraceEventType.Error);
  1387. if (Fx.IsFatal(e)) throw;
  1388. Complete(false, e);
  1389. }
  1390. }
  1391. void OnIssueComplete(IAsyncResult ar)
  1392. {
  1393. try
  1394. {
  1395. _dispatchContext.ResponseMessage = _dispatchContext.SecurityTokenService.EndIssue(ar);
  1396. Complete(false);
  1397. }
  1398. catch (Exception e)
  1399. {
  1400. System.ServiceModel.DiagnosticUtility.TraceHandledException(e, TraceEventType.Error);
  1401. if (Fx.IsFatal(e)) throw;
  1402. Complete(false, e);
  1403. }
  1404. }
  1405. void OnRenewComplete(IAsyncResult ar)
  1406. {
  1407. try
  1408. {
  1409. _dispatchContext.ResponseMessage = _dispatchContext.SecurityTokenService.EndRenew(ar);
  1410. Complete(false);
  1411. }
  1412. catch (Exception e)
  1413. {
  1414. System.ServiceModel.DiagnosticUtility.TraceHandledException(e, TraceEventType.Error);
  1415. if (Fx.IsFatal(e)) throw;
  1416. Complete(false, e);
  1417. }
  1418. }
  1419. void OnValidateComplete(IAsyncResult ar)
  1420. {
  1421. try
  1422. {
  1423. _dispatchContext.ResponseMessage = _dispatchContext.SecurityTokenService.EndValidate(ar);
  1424. Complete(false);
  1425. }
  1426. catch (Exception e)
  1427. {
  1428. System.ServiceModel.DiagnosticUtility.TraceHandledException(e, TraceEventType.Error);
  1429. if (Fx.IsFatal(e)) throw;
  1430. Complete(false, e);
  1431. }
  1432. }
  1433. }
  1434. #region IContractBehavior Members
  1435. /// <summary>
  1436. /// Configures any binding elements to support the contract behavior.
  1437. /// </summary>
  1438. /// <remarks>
  1439. /// Inherited from IContractBehavior
  1440. /// </remarks>
  1441. public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
  1442. {
  1443. return;
  1444. }
  1445. /// <summary>
  1446. /// Implements a modification or extension of the client across a contract.
  1447. /// </summary>
  1448. /// <remarks>
  1449. /// Inherited from IContractBehavior
  1450. /// </remarks>
  1451. public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
  1452. {
  1453. return;
  1454. }
  1455. /// <summary>
  1456. /// Implements a modification or extension of the client across a contract.
  1457. /// </summary>
  1458. /// <remarks>
  1459. /// Inherited from IContractBehavior
  1460. /// </remarks>
  1461. public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.DispatchRuntime dispatchRuntime)
  1462. {
  1463. return;
  1464. }
  1465. /// <summary>
  1466. /// Implement to confirm that the contract and endpoint can support the contract
  1467. /// behavior.
  1468. /// </summary>
  1469. /// <remarks>
  1470. /// Inherited from IContractBehavior
  1471. /// </remarks>
  1472. public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
  1473. {
  1474. return;
  1475. }
  1476. #endregion
  1477. #region IWsdlExportExtension Members
  1478. /// <summary>
  1479. /// Implementation for IWsdlExportExtension.ExportContract. The default implementation
  1480. /// does nothing. Can be overriden in the derived class for specific behavior.
  1481. /// </summary>
  1482. /// <param name="exporter">The WsdlExporter that exports the contract information.</param>
  1483. /// <param name="context">Provides mappings from exported WSDL elements to the contract description.</param>
  1484. public virtual void ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
  1485. {
  1486. return;
  1487. }
  1488. /// <summary>
  1489. /// Implements IWsdlExportExtensions.ExportEndpoint. The default implementation does the following,
  1490. /// For every Trust contract found,
  1491. /// 1. It includes the appropriate trust namespace in the WSDL.
  1492. /// 2. Imports the appropriate Trust schema and all dependent schemas.
  1493. /// 3. Fixes the Messages of each operation to it appropriate WS-Trust equivalent.
  1494. /// Trust Contract exposed by the Framework takes a System.ServiceModel.Channels.Message in and
  1495. /// returns a System.ServiceModel.Channels.Message out. But Trust messages expects and RST and
  1496. /// returns an RSTR/RSTRC. This method fixes the message names with the appropriate WS-Trust
  1497. /// messages.
  1498. /// </summary>
  1499. /// <param name="exporter">The WsdlExporter that exports the contract information.</param>
  1500. /// <param name="context">Provides mappings from exported WSDL elements to the endpoint description.</param>
  1501. /// <exception cref="ArgumentNullException">The input argument 'exporter' or 'context' is null.</exception>
  1502. public virtual void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
  1503. {
  1504. if (exporter == null)
  1505. {
  1506. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
  1507. }
  1508. if (context == null)
  1509. {
  1510. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  1511. }
  1512. if (context.WsdlPort == null)
  1513. {
  1514. throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID3146));
  1515. }
  1516. if (context.WsdlPort.Service == null)
  1517. {
  1518. throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID3147));
  1519. }
  1520. if (context.WsdlPort.Service.ServiceDescription == null)
  1521. {
  1522. throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID3148));
  1523. }
  1524. System.Web.Services.Description.ServiceDescription serviceDescription = context.WsdlPort.Service.ServiceDescription;
  1525. // Iterate throught the Ports and for each of our contracts fix the input and output messages
  1526. // of the contract and import the required schemas.
  1527. foreach (PortType portType in serviceDescription.PortTypes)
  1528. {
  1529. if (StringComparer.Ordinal.Equals(portType.Name, WSTrustServiceContractConstants.Contracts.IWSTrustFeb2005Sync))
  1530. {
  1531. IncludeNamespace(context, WSTrustFeb2005Constants.Prefix, WSTrustFeb2005Constants.NamespaceURI);
  1532. ImportSchema(exporter, context, WSTrustFeb2005Constants.NamespaceURI);
  1533. FixMessageElement(
  1534. serviceDescription,
  1535. portType,
  1536. context,
  1537. WSTrustServiceContractConstants.Operations.TrustFeb2005Cancel,
  1538. new XmlQualifiedName(
  1539. WSTrustFeb2005Constants.ElementNames.RequestSecurityToken,
  1540. WSTrustFeb2005Constants.NamespaceURI),
  1541. new XmlQualifiedName(WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse,
  1542. WSTrustFeb2005Constants.NamespaceURI));
  1543. FixMessageElement(
  1544. serviceDescription,
  1545. portType,
  1546. context,
  1547. WSTrustServiceContractConstants.Operations.TrustFeb2005Issue,
  1548. new XmlQualifiedName(
  1549. WSTrustFeb2005Constants.ElementNames.RequestSecurityToken,
  1550. WSTrustFeb2005Constants.NamespaceURI),
  1551. new XmlQualifiedName(
  1552. WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse,
  1553. WSTrustFeb2005Constants.NamespaceURI));
  1554. FixMessageElement(
  1555. serviceDescription,
  1556. portType,
  1557. context,
  1558. WSTrustServiceContractConstants.Operations.TrustFeb2005Renew,
  1559. new XmlQualifiedName(
  1560. WSTrustFeb2005Constants.ElementNames.RequestSecurityToken,
  1561. WSTrustFeb2005Constants.NamespaceURI),
  1562. new XmlQualifiedName(
  1563. WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse,
  1564. WSTrustFeb2005Constants.NamespaceURI));
  1565. FixMessageElement(
  1566. serviceDescription,
  1567. portType,
  1568. context,
  1569. WSTrustServiceContractConstants.Operations.TrustFeb2005Validate,
  1570. new XmlQualifiedName(
  1571. WSTrustFeb2005Constants.ElementNames.RequestSecurityToken,
  1572. WSTrustFeb2005Constants.NamespaceURI),
  1573. new XmlQualifiedName(
  1574. WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse,
  1575. WSTrustFeb2005Constants.NamespaceURI));
  1576. }
  1577. else if (StringComparer.OrdinalIgnoreCase.Equals(portType.Name, WSTrustServiceContractConstants.Contracts.IWSTrust13Sync))
  1578. {
  1579. IncludeNamespace(context, WSTrust13Constants.Prefix, WSTrust13Constants.NamespaceURI);
  1580. ImportSchema(exporter, context, WSTrust13Constants.NamespaceURI);
  1581. FixMessageElement(
  1582. serviceDescription,
  1583. portType,
  1584. context,
  1585. WSTrustServiceContractConstants.Operations.Trust13Cancel,
  1586. new XmlQualifiedName(
  1587. WSTrust13Constants.ElementNames.RequestSecurityToken,
  1588. WSTrust13Constants.NamespaceURI),
  1589. new XmlQualifiedName(
  1590. WSTrust13Constants.ElementNames.RequestSecurityTokenResponseCollection,
  1591. WSTrust13Constants.NamespaceURI));
  1592. FixMessageElement(
  1593. serviceDescription,
  1594. portType,
  1595. context,
  1596. WSTrustServiceContractConstants.Operations.Trust13Issue,
  1597. new XmlQualifiedName(
  1598. WSTrust13Constants.ElementNames.RequestSecurityToken,
  1599. WSTrust13Constants.NamespaceURI),
  1600. new XmlQualifiedName(
  1601. WSTrust13Constants.ElementNames.RequestSecurityTokenResponseCollection,
  1602. WSTrust13Constants.NamespaceURI));
  1603. FixMessageElement(
  1604. serviceDescription,
  1605. portType,
  1606. context,
  1607. WSTrustServiceContractConstants.Operations.Trust13Renew,
  1608. new XmlQualifiedName(
  1609. WSTrust13Constants.ElementNames.RequestSecurityToken,
  1610. WSTrust13Constants.NamespaceURI),
  1611. new XmlQualifiedName(
  1612. WSTrust13Constants.ElementNames.RequestSecurityTokenResponseCollection,
  1613. WSTrust13Constants.NamespaceURI));
  1614. FixMessageElement(
  1615. serviceDescription,
  1616. portType,
  1617. context,
  1618. WSTrustServiceContractConstants.Operations.Trust13Validate,
  1619. new XmlQualifiedName(
  1620. WSTrust13Constants.ElementNames.RequestSecurityToken,
  1621. WSTrust13Constants.NamespaceURI),
  1622. new XmlQualifiedName(
  1623. WSTrust13Constants.ElementNames.RequestSecurityTokenResponseCollection,
  1624. WSTrust13Constants.NamespaceURI));
  1625. }
  1626. else if (StringComparer.OrdinalIgnoreCase.Equals(portType.Name, WSTrustServiceContractConstants.Contracts.IWSTrustFeb2005Async))
  1627. {
  1628. IncludeNamespace(context, WSTrustFeb2005Constants.Prefix, WSTrustFeb2005Constants.NamespaceURI);
  1629. ImportSchema(exporter, context, WSTrustFeb2005Constants.NamespaceURI);
  1630. FixMessageElement(
  1631. serviceDescription,
  1632. portType,
  1633. context,
  1634. WSTrustServiceContractConstants.Operations.TrustFeb2005CancelAsync,
  1635. new XmlQualifiedName(
  1636. WSTrustFeb2005Constants.ElementNames.RequestSecurityToken,
  1637. WSTrustFeb2005Constants.NamespaceURI),
  1638. new XmlQualifiedName(
  1639. WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse,
  1640. WSTrustFeb2005Constants.NamespaceURI));
  1641. FixMessageElement(
  1642. serviceDescription,
  1643. portType,
  1644. context,
  1645. WSTrustServiceContractConstants.Operations.TrustFeb2005IssueAsync,
  1646. new XmlQualifiedName(
  1647. WSTrustFeb2005Constants.ElementNames.RequestSecurityToken,
  1648. WSTrustFeb2005Constants.NamespaceURI),
  1649. new XmlQualifiedName(
  1650. WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse,
  1651. WSTrustFeb2005Constants.NamespaceURI));
  1652. FixMessageElement(
  1653. serviceDescription,
  1654. portType,
  1655. context,
  1656. WSTrustServiceContractConstants.Operations.TrustFeb2005RenewAsync,
  1657. new XmlQualifiedName(
  1658. WSTrustFeb2005Constants.ElementNames.RequestSecurityToken,
  1659. WSTrustFeb2005Constants.NamespaceURI),
  1660. new XmlQualifiedName(
  1661. WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse,
  1662. WSTrustFeb2005Constants.NamespaceURI));
  1663. FixMessageElement(
  1664. serviceDescription,
  1665. portType,
  1666. context,
  1667. WSTrustServiceContractConstants.Operations.TrustFeb2005ValidateAsync,
  1668. new XmlQualifiedName(
  1669. WSTrustFeb2005Constants.ElementNames.RequestSecurityToken,
  1670. WSTrustFeb2005Constants.NamespaceURI),
  1671. new XmlQualifiedName(
  1672. WSTrustFeb2005Constants.ElementNames.RequestSecurityTokenResponse,
  1673. WSTrustFeb2005Constants.NamespaceURI));
  1674. }
  1675. else if (StringComparer.OrdinalIgnoreCase.Equals(portType.Name, WSTrustServiceContractConstants.Contracts.IWSTrust13Async))
  1676. {
  1677. IncludeNamespace(context, WSTrust13Constants.Prefix, WSTrust13Constants.NamespaceURI);
  1678. ImportSchema(exporter, context, WSTrust13Constants.NamespaceURI);
  1679. FixMessageElement(
  1680. serviceDescription,
  1681. portType,
  1682. context,
  1683. WSTrustServiceContractConstants.Operations.Trust13CancelAsync,
  1684. new XmlQualifiedName(
  1685. WSTrust13Constants.ElementNames.RequestSecurityToken,
  1686. WSTrust13Constants.NamespaceURI),
  1687. new XmlQualifiedName(
  1688. WSTrust13Constants.ElementNames.RequestSecurityTokenResponseCollection,
  1689. WSTrust13Constants.NamespaceURI));
  1690. FixMessageElement(
  1691. serviceDescription,
  1692. portType,
  1693. context,
  1694. WSTrustServiceContractConstants.Operations.Trust13IssueAsync,
  1695. new XmlQualifiedName(
  1696. WSTrust13Constants.ElementNames.RequestSecurityToken,
  1697. WSTrust13Constants.NamespaceURI),
  1698. new XmlQualifiedName(
  1699. WSTrust13Constants.ElementNames.RequestSecurityTokenResponseCollection,
  1700. WSTrust13Constants.NamespaceURI));
  1701. FixMessageElement(
  1702. serviceDescription,
  1703. portType,
  1704. context,
  1705. WSTrustServiceContractConstants.Operations.Trust13RenewAsync,
  1706. new XmlQualifiedName(
  1707. WSTrust13Constants.ElementNames.RequestSecurityToken,
  1708. WSTrust13Constants.NamespaceURI),
  1709. new XmlQualifiedName(
  1710. WSTrust13Constants.ElementNames.RequestSecurityTokenResponseCollection,
  1711. WSTrust13Constants.NamespaceURI));
  1712. FixMessageElement(
  1713. serviceDescription,
  1714. portType,
  1715. context,
  1716. WSTrustServiceContractConstants.Operations.Trust13ValidateAsync,
  1717. new XmlQualifiedName(
  1718. WSTrust13Constants.ElementNames.RequestSecurityToken,
  1719. WSTrust13Constants.NamespaceURI),
  1720. new XmlQualifiedName(
  1721. WSTrust13Constants.ElementNames.RequestSecurityTokenResponseCollection,
  1722. WSTrust13Constants.NamespaceURI));
  1723. }
  1724. }
  1725. }
  1726. #endregion
  1727. /// <summary>
  1728. /// Adds the required WS-Trust namespaces to the WSDL if not already present.
  1729. /// </summary>
  1730. /// <param name="context">Provides mappings from exported WSDL elements to the endpoint description.</param>
  1731. /// <param name="prefix">The prefix of the namespace to be included.</param>
  1732. /// <param name="ns">Namespace to be included.</param>
  1733. /// <exception cref="ArgumentException">Either 'prefix' or 'ns' is null or empty string.</exception>
  1734. /// <exception cref="ArgumentNullException">The 'context' parameter is null.</exception>
  1735. protected virtual void IncludeNamespace(WsdlEndpointConversionContext context, string prefix, string ns)
  1736. {
  1737. if (context == null)
  1738. {
  1739. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  1740. }
  1741. if (String.IsNullOrEmpty(prefix))
  1742. {
  1743. throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("prefix");
  1744. }
  1745. if (String.IsNullOrEmpty(ns))
  1746. {
  1747. throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("ns");
  1748. }
  1749. bool alreadyPresent = false;
  1750. XmlQualifiedName[] namespaces = context.WsdlBinding.ServiceDescription.Namespaces.ToArray();
  1751. for (int i = 0; i < namespaces.Length; ++i)
  1752. {
  1753. if (StringComparer.Ordinal.Equals(namespaces[i].Namespace, ns))
  1754. {
  1755. alreadyPresent = true;
  1756. break;
  1757. }
  1758. }
  1759. if (!alreadyPresent)
  1760. {
  1761. context.WsdlBinding.ServiceDescription.Namespaces.Add(prefix, ns);
  1762. }
  1763. }
  1764. /// <summary>
  1765. /// Imports all the required schema if not already present in the WSDL.
  1766. /// The default implementation will import the following schemas,
  1767. /// (a) WS-Trust Feb 2005.
  1768. /// (b) WS-Trust 1.3
  1769. /// Derived classes can override this method to import other schemas.
  1770. /// </summary>
  1771. /// <param name="exporter">The WsdlExporter that exports the contract information.</param>
  1772. /// <param name="context">Provides mappings from exported WSDL elements to the endpoint description.</param>
  1773. /// <param name="ns">The current WS-Trust namespace for which the schemas are imported.</param>
  1774. /// <exception cref="ArgumentNullException">The parameter 'exporter' or 'context' is null.</exception>
  1775. /// <exception cref="ArgumentException">The parameter 'ns' is either null or String.Empty.</exception>
  1776. /// <exception cref="InvalidOperationException">The namespace 'ns' is not a recognized WS-Trust namespace.</exception>
  1777. protected virtual void ImportSchema(WsdlExporter exporter, WsdlEndpointConversionContext context, string ns)
  1778. {
  1779. if (exporter == null)
  1780. {
  1781. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
  1782. }
  1783. if (context == null)
  1784. {
  1785. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  1786. }
  1787. if (String.IsNullOrEmpty(ns))
  1788. {
  1789. throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("ns");
  1790. }
  1791. foreach (XmlSchema xmlSchema in context.WsdlPort.Service.ServiceDescription.Types.Schemas)
  1792. {
  1793. foreach (XmlSchemaObject include in xmlSchema.Includes)
  1794. {
  1795. XmlSchemaImport schemaImport = include as XmlSchemaImport;
  1796. if ((schemaImport != null) && StringComparer.Ordinal.Equals(schemaImport.Namespace, ns))
  1797. {
  1798. // The schema is already imported. Just return.
  1799. return;
  1800. }
  1801. }
  1802. }
  1803. XmlSchema schema = GetXmlSchema(exporter, ns);
  1804. if (schema == null)
  1805. {
  1806. throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID3004, ns));
  1807. }
  1808. XmlSchema importedSchema = null;
  1809. if (context.WsdlPort.Service.ServiceDescription.Types.Schemas.Count == 0)
  1810. {
  1811. importedSchema = new XmlSchema();
  1812. context.WsdlPort.Service.ServiceDescription.Types.Schemas.Add(importedSchema);
  1813. }
  1814. else
  1815. {
  1816. importedSchema = context.WsdlPort.Service.ServiceDescription.Types.Schemas[0];
  1817. }
  1818. XmlSchemaImport import = new XmlSchemaImport();
  1819. import.Namespace = ns;
  1820. exporter.GeneratedXmlSchemas.Add(schema);
  1821. importedSchema.Includes.Add(import);
  1822. }
  1823. /// <summary>
  1824. /// For a given namespace this method looks up the WsdlExporter to see if an XmlSchema has been cached and returns that.
  1825. /// Else it loads the schema for that given namespace and returns the loaded XmlSchema.
  1826. /// </summary>
  1827. /// <param name="exporter">The WsdlExporter that exports the contract information.</param>
  1828. /// <param name="ns">The namespace for which the schema is to be obtained.</param>
  1829. /// <exception cref="ArgumentNullException">The parameter 'exporter' is null.</exception>
  1830. /// <exception cref="ArgumentException">The parameter 'ns' is either null or String.Empty.</exception>
  1831. /// <exception cref="InvalidOperationException">The namespace 'ns' is not a recognized WS-Trust namespace.</exception>
  1832. static XmlSchema GetXmlSchema(WsdlExporter exporter, string ns)
  1833. {
  1834. if (exporter == null)
  1835. {
  1836. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("exporter");
  1837. }
  1838. if (String.IsNullOrEmpty(ns))
  1839. {
  1840. throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("ns");
  1841. }
  1842. ICollection schemas = exporter.GeneratedXmlSchemas.Schemas(ns);
  1843. if ((schemas != null) && (schemas.Count > 0))
  1844. {
  1845. foreach (XmlSchema s in schemas)
  1846. {
  1847. return s;
  1848. }
  1849. }
  1850. string xmlSchema = null;
  1851. switch (ns)
  1852. {
  1853. case WSTrustFeb2005Constants.NamespaceURI:
  1854. xmlSchema = WSTrustFeb2005Constants.Schema;
  1855. break;
  1856. case WSTrust13Constants.NamespaceURI:
  1857. xmlSchema = WSTrust13Constants.Schema;
  1858. break;
  1859. default:
  1860. throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID5004, ns));
  1861. }
  1862. return XmlSchema.Read(new StringReader(xmlSchema), null);
  1863. }
  1864. /// <summary>
  1865. /// During WSDL generation, the method fixes a given operation message element to refer to the
  1866. /// RST and RSTR elements of the appropriate WS-Trust version.
  1867. /// </summary>
  1868. /// <param name="serviceDescription">The ServiceDescription that has the current state of the exported
  1869. /// WSDL.</param>
  1870. /// <param name="portType">The WSDL PortType whose messages are to be fixed.</param>
  1871. /// <param name="context">Provides mappings from exported WSDL elements to the endpoint description.</param>
  1872. /// <param name="operationName">The operation name inside the PortType.</param>
  1873. /// <param name="inputMessageElement">The XmlQualifiedName of the input message element.</param>
  1874. /// <param name="outputMessageElement">The XmlQualifiedName of the output message element.</param>
  1875. /// <exception cref="ArgumentNullException">The parameter 'serviceDescription', 'portType', 'inputMessageType'
  1876. /// or 'outputMessageType' is null.</exception>
  1877. /// <exception cref="ArgumentException">The parameter 'operationName' is null or Empty.</exception>
  1878. /// <remarks>
  1879. /// Trust Contract exposed by the Framework takes a System.ServiceModel.Channels.Message in and
  1880. /// returns a System.ServiceModel.Channels.Message out. But Trust messages expects and RST and
  1881. /// returns an RSTR/RSTRC. This method fixes the message elements with the appropriate WS-Trust
  1882. /// messages specified by the XmlQualified names 'inputMessageElement' and 'outputMessageElement'.
  1883. /// </remarks>
  1884. protected virtual void FixMessageElement(
  1885. System.Web.Services.Description.ServiceDescription serviceDescription,
  1886. PortType portType,
  1887. WsdlEndpointConversionContext context,
  1888. string operationName,
  1889. XmlQualifiedName inputMessageElement,
  1890. XmlQualifiedName outputMessageElement)
  1891. {
  1892. if (serviceDescription == null)
  1893. {
  1894. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceDescription");
  1895. }
  1896. if (portType == null)
  1897. {
  1898. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("portType");
  1899. }
  1900. if (context == null)
  1901. {
  1902. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  1903. }
  1904. if (String.IsNullOrEmpty(operationName))
  1905. {
  1906. throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("operationName");
  1907. }
  1908. if (inputMessageElement == null)
  1909. {
  1910. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("inputMessageElement");
  1911. }
  1912. if (outputMessageElement == null)
  1913. {
  1914. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("outputMessageElement");
  1915. }
  1916. Operation operation = null;
  1917. System.Web.Services.Description.Message inputMessage = null;
  1918. System.Web.Services.Description.Message outputMessage = null;
  1919. foreach (Operation op in portType.Operations)
  1920. {
  1921. if (StringComparer.Ordinal.Equals(op.Name, operationName))
  1922. {
  1923. operation = op;
  1924. // Find the correspinding message in the messages collection.
  1925. foreach (System.Web.Services.Description.Message message in serviceDescription.Messages)
  1926. {
  1927. if (StringComparer.Ordinal.Equals(message.Name, op.Messages.Input.Message.Name))
  1928. {
  1929. if (message.Parts.Count != 1)
  1930. {
  1931. throw DiagnosticUtility.ThrowHelperInvalidOperation(
  1932. SR.GetString(SR.ID3144, portType.Name, op.Name, message.Name, message.Parts.Count));
  1933. }
  1934. inputMessage = message;
  1935. }
  1936. else if (StringComparer.Ordinal.Equals(message.Name, op.Messages.Output.Message.Name))
  1937. {
  1938. if (message.Parts.Count != 1)
  1939. {
  1940. throw DiagnosticUtility.ThrowHelperInvalidOperation(
  1941. SR.GetString(SR.ID3144, portType.Name, op.Name, message.Name, message.Parts.Count));
  1942. }
  1943. outputMessage = message;
  1944. }
  1945. if ((inputMessage != null) && (outputMessage != null))
  1946. {
  1947. break;
  1948. }
  1949. }
  1950. }
  1951. if (operation != null)
  1952. {
  1953. break;
  1954. }
  1955. }
  1956. if (operation == null)
  1957. {
  1958. // This operation is missing. This might be due to another Behavior that has modified the WSDL as
  1959. // well. Ignore this and return.
  1960. return;
  1961. }
  1962. if (inputMessage == null)
  1963. {
  1964. throw DiagnosticUtility.ThrowHelperInvalidOperation(
  1965. SR.GetString(SR.ID3149, portType.Name, portType.Namespaces, operationName));
  1966. }
  1967. if (outputMessage == null)
  1968. {
  1969. throw DiagnosticUtility.ThrowHelperInvalidOperation(
  1970. SR.GetString(SR.ID3150, portType.Name, portType.Namespaces, operationName));
  1971. }
  1972. inputMessage.Parts[0].Element = inputMessageElement;
  1973. outputMessage.Parts[0].Element = outputMessageElement;
  1974. inputMessage.Parts[0].Type = null;
  1975. outputMessage.Parts[0].Type = null;
  1976. }
  1977. }
  1978. }