Showing the changes to Source/Libraries/GSF.ServiceModel (11 of 628 files). Show all
Unified · Split
| @@ -0,0 +1,277 @@ | |||
| 1 | //******************************************************************************************************* | ||
| 2 | // SecureDataServiceHostFactory.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Tennessee Valley Authority, 2011 | ||
| 5 | // No copyright is claimed pursuant to 17 USC § 105. All Other Rights Reserved. | ||
| 6 | // | ||
| 7 | // This software is made freely available under the TVA Open Source Agreement (see below). | ||
| 8 | // | ||
| 9 | // Code Modification History: | ||
| 10 | // ----------------------------------------------------------------------------------------------------- | ||
| 11 | // 06/09/2011 - Pinal C. Patel | ||
| 12 | // Generated original version of source code. | ||
| 13 | // | ||
| 14 | //******************************************************************************************************* | ||
| 15 | |||
| 16 | #region [ TVA Open Source Agreement ] | ||
| 17 | /* | ||
| 18 | |||
| 19 | THIS OPEN SOURCE AGREEMENT ("AGREEMENT") DEFINES THE RIGHTS OF USE,REPRODUCTION, DISTRIBUTION, | ||
| 20 | MODIFICATION AND REDISTRIBUTION OF CERTAIN COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE | ||
| 21 | TENNESSEE VALLEY AUTHORITY, A CORPORATE AGENCY AND INSTRUMENTALITY OF THE UNITED STATES GOVERNMENT | ||
| 22 | ("GOVERNMENT AGENCY"). GOVERNMENT AGENCY IS AN INTENDED THIRD-PARTY BENEFICIARY OF ALL SUBSEQUENT | ||
| 23 | DISTRIBUTIONS OR REDISTRIBUTIONS OF THE SUBJECT SOFTWARE. ANYONE WHO USES, REPRODUCES, DISTRIBUTES, | ||
| 24 | MODIFIES OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED HEREIN, OR ANY PART THEREOF, IS, BY THAT | ||
| 25 | ACTION, ACCEPTING IN FULL THE RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT. | ||
| 26 | |||
| 27 | Original Software Designation: openPDC | ||
| 28 | Original Software Title: The GSF Open Source Phasor Data Concentrator | ||
| 29 | User Registration Requested. Please Visit https://naspi.tva.com/Registration/ | ||
| 30 | Point of Contact for Original Software: J. Ritchie Carroll <mailto:jrcarrol@tva.gov> | ||
| 31 | |||
| 32 | 1. DEFINITIONS | ||
| 33 | |||
| 34 | A. "Contributor" means Government Agency, as the developer of the Original Software, and any entity | ||
| 35 | that makes a Modification. | ||
| 36 | |||
| 37 | B. "Covered Patents" mean patent claims licensable by a Contributor that are necessarily infringed by | ||
| 38 | the use or sale of its Modification alone or when combined with the Subject Software. | ||
| 39 | |||
| 40 | C. "Display" means the showing of a copy of the Subject Software, either directly or by means of an | ||
| 41 | image, or any other device. | ||
| 42 | |||
| 43 | D. "Distribution" means conveyance or transfer of the Subject Software, regardless of means, to | ||
| 44 | another. | ||
| 45 | |||
| 46 | E. "Larger Work" means computer software that combines Subject Software, or portions thereof, with | ||
| 47 | software separate from the Subject Software that is not governed by the terms of this Agreement. | ||
| 48 | |||
| 49 | F. "Modification" means any alteration of, including addition to or deletion from, the substance or | ||
| 50 | structure of either the Original Software or Subject Software, and includes derivative works, as that | ||
| 51 | term is defined in the Copyright Statute, 17 USC § 101. However, the act of including Subject Software | ||
| 52 | as part of a Larger Work does not in and of itself constitute a Modification. | ||
| 53 | |||
| 54 | G. "Original Software" means the computer software first released under this Agreement by Government | ||
| 55 | Agency entitled openPDC, including source code, object code and accompanying documentation, if any. | ||
| 56 | |||
| 57 | H. "Recipient" means anyone who acquires the Subject Software under this Agreement, including all | ||
| 58 | Contributors. | ||
| 59 | |||
| 60 | I. "Redistribution" means Distribution of the Subject Software after a Modification has been made. | ||
| 61 | |||
| 62 | J. "Reproduction" means the making of a counterpart, image or copy of the Subject Software. | ||
| 63 | |||
| 64 | K. "Sale" means the exchange of the Subject Software for money or equivalent value. | ||
| 65 | |||
| 66 | L. "Subject Software" means the Original Software, Modifications, or any respective parts thereof. | ||
| 67 | |||
| 68 | M. "Use" means the application or employment of the Subject Software for any purpose. | ||
| 69 | |||
| 70 | 2. GRANT OF RIGHTS | ||
| 71 | |||
| 72 | A. Under Non-Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor, | ||
| 73 | with respect to its own contribution to the Subject Software, hereby grants to each Recipient a | ||
| 74 | non-exclusive, world-wide, royalty-free license to engage in the following activities pertaining to | ||
| 75 | the Subject Software: | ||
| 76 | |||
| 77 | 1. Use | ||
| 78 | |||
| 79 | 2. Distribution | ||
| 80 | |||
| 81 | 3. Reproduction | ||
| 82 | |||
| 83 | 4. Modification | ||
| 84 | |||
| 85 | 5. Redistribution | ||
| 86 | |||
| 87 | 6. Display | ||
| 88 | |||
| 89 | B. Under Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor, with | ||
| 90 | respect to its own contribution to the Subject Software, hereby grants to each Recipient under Covered | ||
| 91 | Patents a non-exclusive, world-wide, royalty-free license to engage in the following activities | ||
| 92 | pertaining to the Subject Software: | ||
| 93 | |||
| 94 | 1. Use | ||
| 95 | |||
| 96 | 2. Distribution | ||
| 97 | |||
| 98 | 3. Reproduction | ||
| 99 | |||
| 100 | 4. Sale | ||
| 101 | |||
| 102 | 5. Offer for Sale | ||
| 103 | |||
| 104 | C. The rights granted under Paragraph B. also apply to the combination of a Contributor's Modification | ||
| 105 | and the Subject Software if, at the time the Modification is added by the Contributor, the addition of | ||
| 106 | such Modification causes the combination to be covered by the Covered Patents. It does not apply to | ||
| 107 | any other combinations that include a Modification. | ||
| 108 | |||
| 109 | D. The rights granted in Paragraphs A. and B. allow the Recipient to sublicense those same rights. | ||
| 110 | Such sublicense must be under the same terms and conditions of this Agreement. | ||
| 111 | |||
| 112 | 3. OBLIGATIONS OF RECIPIENT | ||
| 113 | |||
| 114 | A. Distribution or Redistribution of the Subject Software must be made under this Agreement except for | ||
| 115 | additions covered under paragraph 3H. | ||
| 116 | |||
| 117 | 1. Whenever a Recipient distributes or redistributes the Subject Software, a copy of this Agreement | ||
| 118 | must be included with each copy of the Subject Software; and | ||
| 119 | |||
| 120 | 2. If Recipient distributes or redistributes the Subject Software in any form other than source code, | ||
| 121 | Recipient must also make the source code freely available, and must provide with each copy of the | ||
| 122 | Subject Software information on how to obtain the source code in a reasonable manner on or through a | ||
| 123 | medium customarily used for software exchange. | ||
| 124 | |||
| 125 | B. Each Recipient must ensure that the following copyright notice appears prominently in the Subject | ||
| 126 | Software: | ||
| 127 | |||
| 128 | No copyright is claimed pursuant to 17 USC § 105. All Other Rights Reserved. | ||
| 129 | |||
| 130 | C. Each Contributor must characterize its alteration of the Subject Software as a Modification and | ||
| 131 | must identify itself as the originator of its Modification in a manner that reasonably allows | ||
| 132 | subsequent Recipients to identify the originator of the Modification. In fulfillment of these | ||
| 133 | requirements, Contributor must include a file (e.g., a change log file) that describes the alterations | ||
| 134 | made and the date of the alterations, identifies Contributor as originator of the alterations, and | ||
| 135 | consents to characterization of the alterations as a Modification, for example, by including a | ||
| 136 | statement that the Modification is derived, directly or indirectly, from Original Software provided by | ||
| 137 | Government Agency. Once consent is granted, it may not thereafter be revoked. | ||
| 138 | |||
| 139 | D. A Contributor may add its own copyright notice to the Subject Software. Once a copyright notice has | ||
| 140 | been added to the Subject Software, a Recipient may not remove it without the express permission of | ||
| 141 | the Contributor who added the notice. | ||
| 142 | |||
| 143 | E. A Recipient may not make any representation in the Subject Software or in any promotional, | ||
| 144 | advertising or other material that may be construed as an endorsement by Government Agency or by any | ||
| 145 | prior Recipient of any product or service provided by Recipient, or that may seek to obtain commercial | ||
| 146 | advantage by the fact of Government Agency's or a prior Recipient's participation in this Agreement. | ||
| 147 | |||
| 148 | F. In an effort to track usage and maintain accurate records of the Subject Software, each Recipient, | ||
| 149 | upon receipt of the Subject Software, is requested to register with Government Agency by visiting the | ||
| 150 | following website: https://naspi.tva.com/Registration/. Recipient's name and personal information | ||
| 151 | shall be used for statistical purposes only. Once a Recipient makes a Modification available, it is | ||
| 152 | requested that the Recipient inform Government Agency at the web site provided above how to access the | ||
| 153 | Modification. | ||
| 154 | |||
| 155 | G. Each Contributor represents that that its Modification does not violate any existing agreements, | ||
| 156 | regulations, statutes or rules, and further that Contributor has sufficient rights to grant the rights | ||
| 157 | conveyed by this Agreement. | ||
| 158 | |||
| 159 | H. A Recipient may choose to offer, and to charge a fee for, warranty, support, indemnity and/or | ||
| 160 | liability obligations to one or more other Recipients of the Subject Software. A Recipient may do so, | ||
| 161 | however, only on its own behalf and not on behalf of Government Agency or any other Recipient. Such a | ||
| 162 | Recipient must make it absolutely clear that any such warranty, support, indemnity and/or liability | ||
| 163 | obligation is offered by that Recipient alone. Further, such Recipient agrees to indemnify Government | ||
| 164 | Agency and every other Recipient for any liability incurred by them as a result of warranty, support, | ||
| 165 | indemnity and/or liability offered by such Recipient. | ||
| 166 | |||
| 167 | I. A Recipient may create a Larger Work by combining Subject Software with separate software not | ||
| 168 | governed by the terms of this agreement and distribute the Larger Work as a single product. In such | ||
| 169 | case, the Recipient must make sure Subject Software, or portions thereof, included in the Larger Work | ||
| 170 | is subject to this Agreement. | ||
| 171 | |||
| 172 | J. Notwithstanding any provisions contained herein, Recipient is hereby put on notice that export of | ||
| 173 | any goods or technical data from the United States may require some form of export license from the | ||
| 174 | U.S. Government. Failure to obtain necessary export licenses may result in criminal liability under | ||
| 175 | U.S. laws. Government Agency neither represents that a license shall not be required nor that, if | ||
| 176 | required, it shall be issued. Nothing granted herein provides any such export license. | ||
| 177 | |||
| 178 | 4. DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION | ||
| 179 | |||
| 180 | A. No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER | ||
| 181 | EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT | ||
| 182 | SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
| 183 | PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR | ||
| 184 | FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE. THIS | ||
| 185 | AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR | ||
| 186 | RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS | ||
| 187 | RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND | ||
| 188 | LIABILITIES REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT | ||
| 189 | "AS IS." | ||
| 190 | |||
| 191 | B. Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST GOVERNMENT AGENCY, ITS | ||
| 192 | AGENTS, EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF RECIPIENT'S USE | ||
| 193 | OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM | ||
| 194 | SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE | ||
| 195 | SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS GOVERNMENT AGENCY, ITS AGENTS, | ||
| 196 | EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY | ||
| 197 | LAW. THE FOREGOING RELEASE AND INDEMNIFICATION SHALL APPLY EVEN IF THE LIABILITIES, DEMANDS, DAMAGES, | ||
| 198 | EXPENSES OR LOSSES ARE CAUSED, OCCASIONED, OR CONTRIBUTED TO BY THE NEGLIGENCE, SOLE OR CONCURRENT, OF | ||
| 199 | GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE | ||
| 200 | IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT. | ||
| 201 | |||
| 202 | 5. GENERAL TERMS | ||
| 203 | |||
| 204 | A. Termination: This Agreement and the rights granted hereunder will terminate automatically if a | ||
| 205 | Recipient fails to comply with these terms and conditions, and fails to cure such noncompliance within | ||
| 206 | thirty (30) days of becoming aware of such noncompliance. Upon termination, a Recipient agrees to | ||
| 207 | immediately cease use and distribution of the Subject Software. All sublicenses to the Subject | ||
| 208 | Software properly granted by the breaching Recipient shall survive any such termination of this | ||
| 209 | Agreement. | ||
| 210 | |||
| 211 | B. Severability: If any provision of this Agreement is invalid or unenforceable under applicable law, | ||
| 212 | it shall not affect the validity or enforceability of the remainder of the terms of this Agreement. | ||
| 213 | |||
| 214 | C. Applicable Law: This Agreement shall be subject to United States federal law only for all purposes, | ||
| 215 | including, but not limited to, determining the validity of this Agreement, the meaning of its | ||
| 216 | provisions and the rights, obligations and remedies of the parties. | ||
| 217 | |||
| 218 | D. Entire Understanding: This Agreement constitutes the entire understanding and agreement of the | ||
| 219 | parties relating to release of the Subject Software and may not be superseded, modified or amended | ||
| 220 | except by further written agreement duly executed by the parties. | ||
| 221 | |||
| 222 | E. Binding Authority: By accepting and using the Subject Software under this Agreement, a Recipient | ||
| 223 | affirms its authority to bind the Recipient to all terms and conditions of this Agreement and that | ||
| 224 | Recipient hereby agrees to all terms and conditions herein. | ||
| 225 | |||
| 226 | F. Point of Contact: Any Recipient contact with Government Agency is to be directed to the designated | ||
| 227 | representative as follows: J. Ritchie Carroll <mailto:jrcarrol@tva.gov>. | ||
| 228 | |||
| 229 | */ | ||
| 230 | #endregion | ||
| 231 | |||
| 232 | using System; | ||
| 233 | using System.Collections.Generic; | ||
| 234 | using System.Data.Services; | ||
| 235 | using System.IdentityModel.Policy; | ||
| 236 | using System.ServiceModel; | ||
| 237 | using System.ServiceModel.Description; | ||
| 238 | |||
| 239 | namespace GSF.ServiceModel.Activation | ||
| 240 | { | ||
| 241 | /// <summary> | ||
| 242 | /// A service host factory for WCF Data Services that enables role-based security using <see cref="SecurityPolicy"/>. | ||
| 243 | /// </summary> | ||
| 244 | /// <see cref="SecurityPolicy"/> | ||
| 245 | public class SecureDataServiceHostFactory : DataServiceHostFactory | ||
| 246 | { | ||
| 247 | #region [ Methods ] | ||
| 248 | |||
| 249 | /// <summary> | ||
| 250 | /// Creates a new <see cref="DataServiceHost"/> from the URI. | ||
| 251 | /// </summary> | ||
| 252 | /// <param name="serviceType">Specifies the type of WCF service to host.</param> | ||
| 253 | /// <param name="baseAddresses">An array of base addresses for the service.</param> | ||
| 254 | /// <returns>New <see cref="DataServiceHost"/>.</returns> | ||
| 255 | protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) | ||
| 256 | { | ||
| 257 | // Create data service host. | ||
| 258 | ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses); | ||
| 259 | |||
| 260 | // Enable security on the data service. | ||
| 261 | ServiceAuthorizationBehavior serviceBehavior = host.Description.Behaviors.Find<ServiceAuthorizationBehavior>(); | ||
| 262 | if (serviceBehavior == null) | ||
| 263 | { | ||
| 264 | serviceBehavior = new ServiceAuthorizationBehavior(); | ||
| 265 | host.Description.Behaviors.Add(serviceBehavior); | ||
| 266 | } | ||
| 267 | serviceBehavior.PrincipalPermissionMode = PrincipalPermissionMode.Custom; | ||
| 268 | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(); | ||
| 269 | policies.Add((IAuthorizationPolicy)Activator.CreateInstance(typeof(SecurityPolicy))); | ||
| 270 | serviceBehavior.ExternalAuthorizationPolicies = policies.AsReadOnly(); | ||
| 271 | |||
| 272 | return host; | ||
| 273 | } | ||
| 274 | |||
| 275 | #endregion | ||
| 276 | } | ||
| 277 | } | ||
| @@ -0,0 +1,432 @@ | |||
| 1 | //******************************************************************************************************* | ||
| 2 | // SecureServiceHostFactory.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Tennessee Valley Authority, 2011 | ||
| 5 | // No copyright is claimed pursuant to 17 USC § 105. All Other Rights Reserved. | ||
| 6 | // | ||
| 7 | // This software is made freely available under the TVA Open Source Agreement (see below). | ||
| 8 | // | ||
| 9 | // Code Modification History: | ||
| 10 | // ----------------------------------------------------------------------------------------------------- | ||
| 11 | // 10/06/2011 - Pinal C. Patel | ||
| 12 | // Generated original version of source code. | ||
| 13 | // 10/12/2011 - Pinal C. Patel | ||
| 14 | // Added the ability to specify endpoint binding information. | ||
| 15 | // 11/16/2011 - Pinal C. Patel | ||
| 16 | // Added PublishMetadata and DisableSecurity properties. | ||
| 17 | // | ||
| 18 | //******************************************************************************************************* | ||
| 19 | |||
| 20 | #region [ TVA Open Source Agreement ] | ||
| 21 | /* | ||
| 22 | |||
| 23 | THIS OPEN SOURCE AGREEMENT ("AGREEMENT") DEFINES THE RIGHTS OF USE,REPRODUCTION, DISTRIBUTION, | ||
| 24 | MODIFICATION AND REDISTRIBUTION OF CERTAIN COMPUTER SOFTWARE ORIGINALLY RELEASED BY THE | ||
| 25 | TENNESSEE VALLEY AUTHORITY, A CORPORATE AGENCY AND INSTRUMENTALITY OF THE UNITED STATES GOVERNMENT | ||
| 26 | ("GOVERNMENT AGENCY"). GOVERNMENT AGENCY IS AN INTENDED THIRD-PARTY BENEFICIARY OF ALL SUBSEQUENT | ||
| 27 | DISTRIBUTIONS OR REDISTRIBUTIONS OF THE SUBJECT SOFTWARE. ANYONE WHO USES, REPRODUCES, DISTRIBUTES, | ||
| 28 | MODIFIES OR REDISTRIBUTES THE SUBJECT SOFTWARE, AS DEFINED HEREIN, OR ANY PART THEREOF, IS, BY THAT | ||
| 29 | ACTION, ACCEPTING IN FULL THE RESPONSIBILITIES AND OBLIGATIONS CONTAINED IN THIS AGREEMENT. | ||
| 30 | |||
| 31 | Original Software Designation: openPDC | ||
| 32 | Original Software Title: The GSF Open Source Phasor Data Concentrator | ||
| 33 | User Registration Requested. Please Visit https://naspi.tva.com/Registration/ | ||
| 34 | Point of Contact for Original Software: J. Ritchie Carroll <mailto:jrcarrol@tva.gov> | ||
| 35 | |||
| 36 | 1. DEFINITIONS | ||
| 37 | |||
| 38 | A. "Contributor" means Government Agency, as the developer of the Original Software, and any entity | ||
| 39 | that makes a Modification. | ||
| 40 | |||
| 41 | B. "Covered Patents" mean patent claims licensable by a Contributor that are necessarily infringed by | ||
| 42 | the use or sale of its Modification alone or when combined with the Subject Software. | ||
| 43 | |||
| 44 | C. "Display" means the showing of a copy of the Subject Software, either directly or by means of an | ||
| 45 | image, or any other device. | ||
| 46 | |||
| 47 | D. "Distribution" means conveyance or transfer of the Subject Software, regardless of means, to | ||
| 48 | another. | ||
| 49 | |||
| 50 | E. "Larger Work" means computer software that combines Subject Software, or portions thereof, with | ||
| 51 | software separate from the Subject Software that is not governed by the terms of this Agreement. | ||
| 52 | |||
| 53 | F. "Modification" means any alteration of, including addition to or deletion from, the substance or | ||
| 54 | structure of either the Original Software or Subject Software, and includes derivative works, as that | ||
| 55 | term is defined in the Copyright Statute, 17 USC § 101. However, the act of including Subject Software | ||
| 56 | as part of a Larger Work does not in and of itself constitute a Modification. | ||
| 57 | |||
| 58 | G. "Original Software" means the computer software first released under this Agreement by Government | ||
| 59 | Agency entitled openPDC, including source code, object code and accompanying documentation, if any. | ||
| 60 | |||
| 61 | H. "Recipient" means anyone who acquires the Subject Software under this Agreement, including all | ||
| 62 | Contributors. | ||
| 63 | |||
| 64 | I. "Redistribution" means Distribution of the Subject Software after a Modification has been made. | ||
| 65 | |||
| 66 | J. "Reproduction" means the making of a counterpart, image or copy of the Subject Software. | ||
| 67 | |||
| 68 | K. "Sale" means the exchange of the Subject Software for money or equivalent value. | ||
| 69 | |||
| 70 | L. "Subject Software" means the Original Software, Modifications, or any respective parts thereof. | ||
| 71 | |||
| 72 | M. "Use" means the application or employment of the Subject Software for any purpose. | ||
| 73 | |||
| 74 | 2. GRANT OF RIGHTS | ||
| 75 | |||
| 76 | A. Under Non-Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor, | ||
| 77 | with respect to its own contribution to the Subject Software, hereby grants to each Recipient a | ||
| 78 | non-exclusive, world-wide, royalty-free license to engage in the following activities pertaining to | ||
| 79 | the Subject Software: | ||
| 80 | |||
| 81 | 1. Use | ||
| 82 | |||
| 83 | 2. Distribution | ||
| 84 | |||
| 85 | 3. Reproduction | ||
| 86 | |||
| 87 | 4. Modification | ||
| 88 | |||
| 89 | 5. Redistribution | ||
| 90 | |||
| 91 | 6. Display | ||
| 92 | |||
| 93 | B. Under Patent Rights: Subject to the terms and conditions of this Agreement, each Contributor, with | ||
| 94 | respect to its own contribution to the Subject Software, hereby grants to each Recipient under Covered | ||
| 95 | Patents a non-exclusive, world-wide, royalty-free license to engage in the following activities | ||
| 96 | pertaining to the Subject Software: | ||
| 97 | |||
| 98 | 1. Use | ||
| 99 | |||
| 100 | 2. Distribution | ||
| 101 | |||
| 102 | 3. Reproduction | ||
| 103 | |||
| 104 | 4. Sale | ||
| 105 | |||
| 106 | 5. Offer for Sale | ||
| 107 | |||
| 108 | C. The rights granted under Paragraph B. also apply to the combination of a Contributor's Modification | ||
| 109 | and the Subject Software if, at the time the Modification is added by the Contributor, the addition of | ||
| 110 | such Modification causes the combination to be covered by the Covered Patents. It does not apply to | ||
| 111 | any other combinations that include a Modification. | ||
| 112 | |||
| 113 | D. The rights granted in Paragraphs A. and B. allow the Recipient to sublicense those same rights. | ||
| 114 | Such sublicense must be under the same terms and conditions of this Agreement. | ||
| 115 | |||
| 116 | 3. OBLIGATIONS OF RECIPIENT | ||
| 117 | |||
| 118 | A. Distribution or Redistribution of the Subject Software must be made under this Agreement except for | ||
| 119 | additions covered under paragraph 3H. | ||
| 120 | |||
| 121 | 1. Whenever a Recipient distributes or redistributes the Subject Software, a copy of this Agreement | ||
| 122 | must be included with each copy of the Subject Software; and | ||
| 123 | |||
| 124 | 2. If Recipient distributes or redistributes the Subject Software in any form other than source code, | ||
| 125 | Recipient must also make the source code freely available, and must provide with each copy of the | ||
| 126 | Subject Software information on how to obtain the source code in a reasonable manner on or through a | ||
| 127 | medium customarily used for software exchange. | ||
| 128 | |||
| 129 | B. Each Recipient must ensure that the following copyright notice appears prominently in the Subject | ||
| 130 | Software: | ||
| 131 | |||
| 132 | No copyright is claimed pursuant to 17 USC § 105. All Other Rights Reserved. | ||
| 133 | |||
| 134 | C. Each Contributor must characterize its alteration of the Subject Software as a Modification and | ||
| 135 | must identify itself as the originator of its Modification in a manner that reasonably allows | ||
| 136 | subsequent Recipients to identify the originator of the Modification. In fulfillment of these | ||
| 137 | requirements, Contributor must include a file (e.g., a change log file) that describes the alterations | ||
| 138 | made and the date of the alterations, identifies Contributor as originator of the alterations, and | ||
| 139 | consents to characterization of the alterations as a Modification, for example, by including a | ||
| 140 | statement that the Modification is derived, directly or indirectly, from Original Software provided by | ||
| 141 | Government Agency. Once consent is granted, it may not thereafter be revoked. | ||
| 142 | |||
| 143 | D. A Contributor may add its own copyright notice to the Subject Software. Once a copyright notice has | ||
| 144 | been added to the Subject Software, a Recipient may not remove it without the express permission of | ||
| 145 | the Contributor who added the notice. | ||
| 146 | |||
| 147 | E. A Recipient may not make any representation in the Subject Software or in any promotional, | ||
| 148 | advertising or other material that may be construed as an endorsement by Government Agency or by any | ||
| 149 | prior Recipient of any product or service provided by Recipient, or that may seek to obtain commercial | ||
| 150 | advantage by the fact of Government Agency's or a prior Recipient's participation in this Agreement. | ||
| 151 | |||
| 152 | F. In an effort to track usage and maintain accurate records of the Subject Software, each Recipient, | ||
| 153 | upon receipt of the Subject Software, is requested to register with Government Agency by visiting the | ||
| 154 | following website: https://naspi.tva.com/Registration/. Recipient's name and personal information | ||
| 155 | shall be used for statistical purposes only. Once a Recipient makes a Modification available, it is | ||
| 156 | requested that the Recipient inform Government Agency at the web site provided above how to access the | ||
| 157 | Modification. | ||
| 158 | |||
| 159 | G. Each Contributor represents that that its Modification does not violate any existing agreements, | ||
| 160 | regulations, statutes or rules, and further that Contributor has sufficient rights to grant the rights | ||
| 161 | conveyed by this Agreement. | ||
| 162 | |||
| 163 | H. A Recipient may choose to offer, and to charge a fee for, warranty, support, indemnity and/or | ||
| 164 | liability obligations to one or more other Recipients of the Subject Software. A Recipient may do so, | ||
| 165 | however, only on its own behalf and not on behalf of Government Agency or any other Recipient. Such a | ||
| 166 | Recipient must make it absolutely clear that any such warranty, support, indemnity and/or liability | ||
| 167 | obligation is offered by that Recipient alone. Further, such Recipient agrees to indemnify Government | ||
| 168 | Agency and every other Recipient for any liability incurred by them as a result of warranty, support, | ||
| 169 | indemnity and/or liability offered by such Recipient. | ||
| 170 | |||
| 171 | I. A Recipient may create a Larger Work by combining Subject Software with separate software not | ||
| 172 | governed by the terms of this agreement and distribute the Larger Work as a single product. In such | ||
| 173 | case, the Recipient must make sure Subject Software, or portions thereof, included in the Larger Work | ||
| 174 | is subject to this Agreement. | ||
| 175 | |||
| 176 | J. Notwithstanding any provisions contained herein, Recipient is hereby put on notice that export of | ||
| 177 | any goods or technical data from the United States may require some form of export license from the | ||
| 178 | U.S. Government. Failure to obtain necessary export licenses may result in criminal liability under | ||
| 179 | U.S. laws. Government Agency neither represents that a license shall not be required nor that, if | ||
| 180 | required, it shall be issued. Nothing granted herein provides any such export license. | ||
| 181 | |||
| 182 | 4. DISCLAIMER OF WARRANTIES AND LIABILITIES; WAIVER AND INDEMNIFICATION | ||
| 183 | |||
| 184 | A. No Warranty: THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY KIND, EITHER | ||
| 185 | EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY THAT THE SUBJECT | ||
| 186 | SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
| 187 | PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL BE ERROR | ||
| 188 | FREE, OR ANY WARRANTY THAT DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE. THIS | ||
| 189 | AGREEMENT DOES NOT, IN ANY MANNER, CONSTITUTE AN ENDORSEMENT BY GOVERNMENT AGENCY OR ANY PRIOR | ||
| 190 | RECIPIENT OF ANY RESULTS, RESULTING DESIGNS, HARDWARE, SOFTWARE PRODUCTS OR ANY OTHER APPLICATIONS | ||
| 191 | RESULTING FROM USE OF THE SUBJECT SOFTWARE. FURTHER, GOVERNMENT AGENCY DISCLAIMS ALL WARRANTIES AND | ||
| 192 | LIABILITIES REGARDING THIRD-PARTY SOFTWARE, IF PRESENT IN THE ORIGINAL SOFTWARE, AND DISTRIBUTES IT | ||
| 193 | "AS IS." | ||
| 194 | |||
| 195 | B. Waiver and Indemnity: RECIPIENT AGREES TO WAIVE ANY AND ALL CLAIMS AGAINST GOVERNMENT AGENCY, ITS | ||
| 196 | AGENTS, EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT. IF RECIPIENT'S USE | ||
| 197 | OF THE SUBJECT SOFTWARE RESULTS IN ANY LIABILITIES, DEMANDS, DAMAGES, EXPENSES OR LOSSES ARISING FROM | ||
| 198 | SUCH USE, INCLUDING ANY DAMAGES FROM PRODUCTS BASED ON, OR RESULTING FROM, RECIPIENT'S USE OF THE | ||
| 199 | SUBJECT SOFTWARE, RECIPIENT SHALL INDEMNIFY AND HOLD HARMLESS GOVERNMENT AGENCY, ITS AGENTS, | ||
| 200 | EMPLOYEES, CONTRACTORS AND SUBCONTRACTORS, AS WELL AS ANY PRIOR RECIPIENT, TO THE EXTENT PERMITTED BY | ||
| 201 | LAW. THE FOREGOING RELEASE AND INDEMNIFICATION SHALL APPLY EVEN IF THE LIABILITIES, DEMANDS, DAMAGES, | ||
| 202 | EXPENSES OR LOSSES ARE CAUSED, OCCASIONED, OR CONTRIBUTED TO BY THE NEGLIGENCE, SOLE OR CONCURRENT, OF | ||
| 203 | GOVERNMENT AGENCY OR ANY PRIOR RECIPIENT. RECIPIENT'S SOLE REMEDY FOR ANY SUCH MATTER SHALL BE THE | ||
| 204 | IMMEDIATE, UNILATERAL TERMINATION OF THIS AGREEMENT. | ||
| 205 | |||
| 206 | 5. GENERAL TERMS | ||
| 207 | |||
| 208 | A. Termination: This Agreement and the rights granted hereunder will terminate automatically if a | ||
| 209 | Recipient fails to comply with these terms and conditions, and fails to cure such noncompliance within | ||
| 210 | thirty (30) days of becoming aware of such noncompliance. Upon termination, a Recipient agrees to | ||
| 211 | immediately cease use and distribution of the Subject Software. All sublicenses to the Subject | ||
| 212 | Software properly granted by the breaching Recipient shall survive any such termination of this | ||
| 213 | Agreement. | ||
| 214 | |||
| 215 | B. Severability: If any provision of this Agreement is invalid or unenforceable under applicable law, | ||
| 216 | it shall not affect the validity or enforceability of the remainder of the terms of this Agreement. | ||
| 217 | |||
| 218 | C. Applicable Law: This Agreement shall be subject to United States federal law only for all purposes, | ||
| 219 | including, but not limited to, determining the validity of this Agreement, the meaning of its | ||
| 220 | provisions and the rights, obligations and remedies of the parties. | ||
| 221 | |||
| 222 | D. Entire Understanding: This Agreement constitutes the entire understanding and agreement of the | ||
| 223 | parties relating to release of the Subject Software and may not be superseded, modified or amended | ||
| 224 | except by further written agreement duly executed by the parties. | ||
| 225 | |||
| 226 | E. Binding Authority: By accepting and using the Subject Software under this Agreement, a Recipient | ||
| 227 | affirms its authority to bind the Recipient to all terms and conditions of this Agreement and that | ||
| 228 | Recipient hereby agrees to all terms and conditions herein. | ||
| 229 | |||
| 230 | F. Point of Contact: Any Recipient contact with Government Agency is to be directed to the designated | ||
| 231 | representative as follows: J. Ritchie Carroll <mailto:jrcarrol@tva.gov>. | ||
| 232 | |||
| 233 | */ | ||
| 234 | #endregion | ||
| 235 | |||
| 236 | using System; | ||
| 237 | using System.Collections.Generic; | ||
| 238 | using System.IdentityModel.Policy; | ||
| 239 | using System.Net; | ||
| 240 | using System.ServiceModel; | ||
| 241 | using System.ServiceModel.Activation; | ||
| 242 | using System.ServiceModel.Channels; | ||
| 243 | using System.ServiceModel.Description; | ||
| 244 | |||
| 245 | namespace GSF.ServiceModel.Activation | ||
| 246 | { | ||
| 247 | /// <summary> | ||
| 248 | /// A service host factory for WCF Services that enables role-based security using <see cref="SecurityPolicy"/>. | ||
| 249 | /// </summary> | ||
| 250 | /// <see cref="SecurityPolicy"/> | ||
| 251 | public class SecureServiceHostFactory : ServiceHostFactory | ||
| 252 | { | ||
| 253 | #region [ Members ] | ||
| 254 | |||
| 255 | // Fields | ||
| 256 | private string m_protocol; | ||
| 257 | private string m_address; | ||
| 258 | private bool m_publishMetadata; | ||
| 259 | private bool m_disableSecurity; | ||
| 260 | |||
| 261 | #endregion | ||
| 262 | |||
| 263 | #region [ Constructors ] | ||
| 264 | |||
| 265 | /// <summary> | ||
| 266 | /// Initializes a new instance of the <see cref="SecureServiceHostFactory"/> class. | ||
| 267 | /// </summary> | ||
| 268 | public SecureServiceHostFactory() | ||
| 269 | : this(string.Empty) | ||
| 270 | { | ||
| 271 | } | ||
| 272 | |||
| 273 | /// <summary> | ||
| 274 | /// Initializes a new instance of the <see cref="SecureServiceHostFactory"/> class. | ||
| 275 | /// </summary> | ||
| 276 | /// <param name="protocol">Protocol used by the service.</param> | ||
| 277 | public SecureServiceHostFactory(string protocol) | ||
| 278 | : this(protocol, string.Empty) | ||
| 279 | { | ||
| 280 | } | ||
| 281 | |||
| 282 | /// <summary> | ||
| 283 | /// Initializes a new instance of the <see cref="SecureServiceHostFactory"/> class. | ||
| 284 | /// </summary> | ||
| 285 | /// <param name="protocol">Protocol used by the service.</param> | ||
| 286 | /// <param name="address">Address of the service.</param> | ||
| 287 | public SecureServiceHostFactory(string protocol, string address) | ||
| 288 | { | ||
| 289 | m_protocol = protocol; | ||
| 290 | m_address = address; | ||
| 291 | m_publishMetadata = true; | ||
| 292 | } | ||
| 293 | |||
| 294 | #endregion | ||
| 295 | |||
| 296 | #region [ Properties ] | ||
| 297 | |||
| 298 | /// <summary> | ||
| 299 | /// Gets or sets a boolean value that indicates whether service metadata is to be published. | ||
| 300 | /// </summary> | ||
| 301 | public bool PublishMetadata | ||
| 302 | { | ||
| 303 | get | ||
| 304 | { | ||
| 305 | return m_publishMetadata; | ||
| 306 | } | ||
| 307 | set | ||
| 308 | { | ||
| 309 | m_publishMetadata = value; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | /// <summary> | ||
| 314 | /// Gets or sets a boolean value that indicates whether security on the service is to be disabled. | ||
| 315 | /// </summary> | ||
| 316 | public bool DisableSecurity | ||
| 317 | { | ||
| 318 | get | ||
| 319 | { | ||
| 320 | return m_disableSecurity; | ||
| 321 | } | ||
| 322 | set | ||
| 323 | { | ||
| 324 | m_disableSecurity = value; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | #endregion | ||
| 329 | |||
| 330 | #region [ Methods ] | ||
| 331 | |||
| 332 | /// <summary> | ||
| 333 | /// Creates a new <see cref="ServiceHost"/> from the URI. | ||
| 334 | /// </summary> | ||
| 335 | /// <param name="serviceType">Specifies the type of WCF service to host.</param> | ||
| 336 | /// <param name="baseAddresses">An array of base addresses for the service.</param> | ||
| 337 | /// <returns>New <see cref="ServiceHost"/>.</returns> | ||
| 338 | protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) | ||
| 339 | { | ||
| 340 | // Check security requirement. | ||
| 341 | bool integratedSecurity = (Service.GetAuthenticationSchemes(baseAddresses[0]) & AuthenticationSchemes.Anonymous) != AuthenticationSchemes.Anonymous; | ||
| 342 | |||
| 343 | // Create service host. | ||
| 344 | ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses); | ||
| 345 | |||
| 346 | // Enable metadata publishing. | ||
| 347 | if (m_publishMetadata) | ||
| 348 | { | ||
| 349 | ServiceMetadataBehavior metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); | ||
| 350 | if (metadataBehavior == null) | ||
| 351 | { | ||
| 352 | metadataBehavior = new ServiceMetadataBehavior(); | ||
| 353 | host.Description.Behaviors.Add(metadataBehavior); | ||
| 354 | } | ||
| 355 | metadataBehavior.HttpGetEnabled = true; | ||
| 356 | } | ||
| 357 | |||
| 358 | // Enable security on the service. | ||
| 359 | if (!m_disableSecurity) | ||
| 360 | { | ||
| 361 | ServiceAuthorizationBehavior authorizationBehavior = host.Description.Behaviors.Find<ServiceAuthorizationBehavior>(); | ||
| 362 | if (authorizationBehavior == null) | ||
| 363 | { | ||
| 364 | authorizationBehavior = new ServiceAuthorizationBehavior(); | ||
| 365 | host.Description.Behaviors.Add(authorizationBehavior); | ||
| 366 | } | ||
| 367 | authorizationBehavior.PrincipalPermissionMode = PrincipalPermissionMode.Custom; | ||
| 368 | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(); | ||
| 369 | policies.Add((IAuthorizationPolicy)Activator.CreateInstance(typeof(SecurityPolicy))); | ||
| 370 | authorizationBehavior.ExternalAuthorizationPolicies = policies.AsReadOnly(); | ||
| 371 | } | ||
| 372 | |||
| 373 | // Create endpoint and configure security. | ||
| 374 | host.AddDefaultEndpoints(); | ||
| 375 | if (string.IsNullOrEmpty(m_protocol)) | ||
| 376 | { | ||
| 377 | // Use the default endpoint. | ||
| 378 | foreach (ServiceEndpoint endpoint in host.Description.Endpoints) | ||
| 379 | { | ||
| 380 | BasicHttpBinding basicBinding = endpoint.Binding as BasicHttpBinding; | ||
| 381 | if (basicBinding != null) | ||
| 382 | { | ||
| 383 | // Default endpoint uses BasicHttpBinding. | ||
| 384 | if (integratedSecurity) | ||
| 385 | { | ||
| 386 | // Enable security. | ||
| 387 | basicBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; | ||
| 388 | basicBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; | ||
| 389 | } | ||
| 390 | else | ||
| 391 | { | ||
| 392 | // Disable sercurity. | ||
| 393 | basicBinding.Security.Mode = BasicHttpSecurityMode.None; | ||
| 394 | basicBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | } | ||
| 398 | } | ||
| 399 | else | ||
| 400 | { | ||
| 401 | // Create endpoint using the specifics. | ||
| 402 | host.Description.Endpoints.Clear(); | ||
| 403 | |||
| 404 | Binding serviceBinding; | ||
| 405 | ServiceEndpoint serviceEndpoint; | ||
| 406 | serviceBinding = Service.CreateServiceBinding(ref m_protocol, integratedSecurity); | ||
| 407 | if (serviceBinding != null) | ||
| 408 | { | ||
| 409 | // Binding created for the endpoint. | ||
| 410 | Type contract = Service.GetServiceContract(serviceType); | ||
| 411 | if (!string.IsNullOrEmpty(m_address)) | ||
| 412 | serviceEndpoint = host.AddServiceEndpoint(contract, serviceBinding, m_address); | ||
| 413 | else | ||
| 414 | serviceEndpoint = host.AddServiceEndpoint(contract, serviceBinding, string.Empty); | ||
| 415 | |||
| 416 | // Special handling for REST endpoint. | ||
| 417 | if (serviceBinding is WebHttpBinding) | ||
| 418 | { | ||
| 419 | WebHttpBehavior restBehavior = new WebHttpBehavior(); | ||
| 420 | restBehavior.HelpEnabled = m_publishMetadata; | ||
| 421 | |||
| 422 | serviceEndpoint.Behaviors.Add(restBehavior); | ||
| 423 | } | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | return host; | ||
| 428 | } | ||
| 429 | |||
| 430 | #endregion | ||
| 431 | } | ||
| 432 | } | ||
| @@ -0,0 +1,108 @@ | |||
| 1 | <?xml version="1.0" encoding="utf-8"?> | ||
| 2 | <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| 3 | <PropertyGroup> | ||
| 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
| 5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
| 6 | <ProductVersion>8.0.30703</ProductVersion> | ||
| 7 | <SchemaVersion>2.0</SchemaVersion> | ||
| 8 | <ProjectGuid>{0622C318-5032-4D56-BB71-5CE64B502FB0}</ProjectGuid> | ||
| 9 | <OutputType>Library</OutputType> | ||
| 10 | <AppDesignerFolder>Properties</AppDesignerFolder> | ||
| 11 | <RootNamespace>GSF.ServiceModel</RootNamespace> | ||
| 12 | <AssemblyName>GSF.ServiceModel</AssemblyName> | ||
| 13 | <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
| 14 | <FileAlignment>512</FileAlignment> | ||
| 15 | <SccProjectName>SAK</SccProjectName> | ||
| 16 | <SccLocalPath>SAK</SccLocalPath> | ||
| 17 | <SccAuxPath>SAK</SccAuxPath> | ||
| 18 | <SccProvider>SAK</SccProvider> | ||
| 19 | <TargetFrameworkProfile /> | ||
| 20 | </PropertyGroup> | ||
| 21 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
| 22 | <DebugSymbols>true</DebugSymbols> | ||
| 23 | <DebugType>full</DebugType> | ||
| 24 | <Optimize>false</Optimize> | ||
| 25 | <OutputPath>..\..\..\Build\Output\Debug\Libraries\</OutputPath> | ||
| 26 | <DefineConstants>DEBUG;TRACE</DefineConstants> | ||
| 27 | <ErrorReport>prompt</ErrorReport> | ||
| 28 | <WarningLevel>4</WarningLevel> | ||
| 29 | <DocumentationFile>..\..\..\Build\Output\Debug\Libraries\GSF.ServiceModel.xml</DocumentationFile> | ||
| 30 | <UseVSHostingProcess>false</UseVSHostingProcess> | ||
| 31 | <Prefer32Bit>false</Prefer32Bit> | ||
| 32 | </PropertyGroup> | ||
| 33 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
| 34 | <DebugType>pdbonly</DebugType> | ||
| 35 | <Optimize>true</Optimize> | ||
| 36 | <OutputPath>..\..\..\Build\Output\Release\Libraries\</OutputPath> | ||
| 37 | <DefineConstants>TRACE</DefineConstants> | ||
| 38 | <ErrorReport>prompt</ErrorReport> | ||
| 39 | <WarningLevel>4</WarningLevel> | ||
| 40 | <DocumentationFile>..\..\..\Build\Output\Release\Libraries\GSF.ServiceModel.xml</DocumentationFile> | ||
| 41 | <UseVSHostingProcess>false</UseVSHostingProcess> | ||
| 42 | <Prefer32Bit>false</Prefer32Bit> | ||
| 43 | </PropertyGroup> | ||
| 44 | <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Mono|AnyCPU'"> | ||
| 45 | <OutputPath>..\..\..\Build\Output\Mono\Libraries\</OutputPath> | ||
| 46 | <DefineConstants>TRACE;MONO</DefineConstants> | ||
| 47 | <DocumentationFile>..\..\..\Build\Output\Mono\Libraries\GSF.ServiceModel.xml</DocumentationFile> | ||
| 48 | <Optimize>true</Optimize> | ||
| 49 | <DebugType>pdbonly</DebugType> | ||
| 50 | <PlatformTarget>AnyCPU</PlatformTarget> | ||
| 51 | <CodeAnalysisLogFile>..\..\..\Build\Output\Release\Libraries\TVA.ServiceModel.dll.CodeAnalysisLog.xml</CodeAnalysisLogFile> | ||
| 52 | <CodeAnalysisUseTypeNameInSuppression>true</CodeAnalysisUseTypeNameInSuppression> | ||
| 53 | <CodeAnalysisModuleSuppressionsFile>GlobalSuppressions.cs</CodeAnalysisModuleSuppressionsFile> | ||
| 54 | <UseVSHostingProcess>false</UseVSHostingProcess> | ||
| 55 | <ErrorReport>prompt</ErrorReport> | ||
| 56 | <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> | ||
| 57 | <CodeAnalysisRuleSetDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets</CodeAnalysisRuleSetDirectories> | ||
| 58 | <CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets> | ||
| 59 | <CodeAnalysisRuleDirectories>;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules</CodeAnalysisRuleDirectories> | ||
| 60 | <CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules> | ||
| 61 | <Prefer32Bit>false</Prefer32Bit> | ||
| 62 | </PropertyGroup> | ||
| 63 | <ItemGroup> | ||
| 64 | <Reference Include="System" /> | ||
| 65 | <Reference Include="System.Configuration" /> | ||
| 66 | <Reference Include="System.Core" /> | ||
| 67 | <Reference Include="System.Data.Services" /> | ||
| 68 | <Reference Include="System.IdentityModel" /> | ||
| 69 | <Reference Include="System.Runtime.Serialization" /> | ||
| 70 | <Reference Include="System.ServiceModel" /> | ||
| 71 | <Reference Include="System.ServiceModel.Activation" /> | ||
| 72 | <Reference Include="System.ServiceModel.Web" /> | ||
| 73 | <Reference Include="System.Xml.Linq" /> | ||
| 74 | <Reference Include="System.Data.DataSetExtensions" /> | ||
| 75 | <Reference Include="Microsoft.CSharp" /> | ||
| 76 | <Reference Include="System.Data" /> | ||
| 77 | <Reference Include="System.Xml" /> | ||
| 78 | </ItemGroup> | ||
| 79 | <ItemGroup> | ||
| 80 | <Compile Include="Activation\SecureServiceHostFactory.cs" /> | ||
| 81 | <Compile Include="IPolicyRetriever.cs" /> | ||
| 82 | <Compile Include="ISelfHostingService.cs" /> | ||
| 83 | <Compile Include="NamespaceDoc.cs" /> | ||
| 84 | <Compile Include="Properties\AssemblyInfo.cs" /> | ||
| 85 | <Compile Include="Activation\SecureDataServiceHostFactory.cs" /> | ||
| 86 | <Compile Include="SecurityPolicy.cs" /> | ||
| 87 | <Compile Include="SelfHostingService.cs" /> | ||
| 88 | <Compile Include="Service.cs" /> | ||
| 89 | </ItemGroup> | ||
| 90 | <ItemGroup> | ||
| 91 | <ProjectReference Include="..\GSF.Core\GSF.Core.csproj"> | ||
| 92 | <Project>{FF3FCBA6-F01A-4EC2-BC3F-6BA832AFCF88}</Project> | ||
| 93 | <Name>GSF.Core</Name> | ||
| 94 | </ProjectReference> | ||
| 95 | <ProjectReference Include="..\GSF.Security\GSF.Security.csproj"> | ||
| 96 | <Project>{4060E729-EFFD-4E50-A0D5-99C0990EF7DA}</Project> | ||
| 97 | <Name>GSF.Security</Name> | ||
| 98 | </ProjectReference> | ||
| 99 | </ItemGroup> | ||
| 100 | <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
| 101 | <!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
| 102 | Other similar extension points exist, see Microsoft.Common.targets. | ||
| 103 | <Target Name="BeforeBuild"> | ||
| 104 | </Target> | ||
| 105 | <Target Name="AfterBuild"> | ||
| 106 | </Target> | ||
| 107 | --> | ||
| 108 | </Project> | ||
| @@ -0,0 +1,73 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // IPolicyRetriever.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 08/02/2012 - J. Ritchie Carroll | ||
| 20 | // Generated original version of source code. | ||
| 21 | // 10/8/2012 - Danyelle Gilliam | ||
| 22 | // Modified Header | ||
| 23 | // | ||
| 24 | //****************************************************************************************************** | ||
| 25 | |||
| 26 | |||
| 27 | #region [ Contributor License Agreements ] | ||
| 28 | |||
| 29 | //****************************************************************************************************** | ||
| 30 | // | ||
| 31 | // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. | ||
| 32 | // | ||
| 33 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 34 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 35 | // | ||
| 36 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 37 | // | ||
| 38 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 39 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 40 | // License for the specific language governing permissions and limitations. | ||
| 41 | // | ||
| 42 | //****************************************************************************************************** | ||
| 43 | |||
| 44 | #endregion | ||
| 45 | |||
| 46 | using System.IO; | ||
| 47 | using System.ServiceModel; | ||
| 48 | using System.ServiceModel.Web; | ||
| 49 | |||
| 50 | namespace GSF.ServiceModel | ||
| 51 | { | ||
| 52 | /// <summary> | ||
| 53 | /// Defines a web service interface to allow policy retrieval for cross-domain access from | ||
| 54 | /// Silverlight and Flash browser controls. | ||
| 55 | /// </summary> | ||
| 56 | [ServiceContract] | ||
| 57 | public interface IPolicyRetriever | ||
| 58 | { | ||
| 59 | /// <summary> | ||
| 60 | /// Gets policy stream for Silverlight applications. | ||
| 61 | /// </summary> | ||
| 62 | /// <returns>Stream containing clientaccesspolicy.xml.</returns> | ||
| 63 | [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")] | ||
| 64 | Stream GetSilverlightPolicy(); | ||
| 65 | |||
| 66 | /// <summary> | ||
| 67 | /// Gets policy stream for Flash applications. | ||
| 68 | /// </summary> | ||
| 69 | /// <returns>Stream containing crossdomain.xml.</returns> | ||
| 70 | [OperationContract, WebGet(UriTemplate = "/crossdomain.xml")] | ||
| 71 | Stream GetFlashPolicy(); | ||
| 72 | } | ||
| 73 | } | ||
| @@ -0,0 +1,108 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // ISelfHostingService.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 08/21/2009 - Pinal C. Patel | ||
| 20 | // Generated original version of source code. | ||
| 21 | // 09/15/2009 - Stephen C. Wills | ||
| 22 | // Added new header and license agreement. | ||
| 23 | // 05/28/2010 - Pinal C. Patel | ||
| 24 | // Added an endpoint for web service help. | ||
| 25 | // 10/08/2010 - Pinal C. Patel | ||
| 26 | // Removed REST web service help endpoint since a similar feature is now part of WCF 4.0. | ||
| 27 | // 10/14/2010 - Pinal C. Patel | ||
| 28 | // Made changes for hosting flexibility and enabling security: | ||
| 29 | // Deleted DataFlow since access restriction can now be imposed by enabling security. | ||
| 30 | // Added SecurityPolicy and PublishMetadata. | ||
| 31 | // Renamed ServiceUri to Endpoints and ServiceContract to Contract. | ||
| 32 | // 10/8/2012 - Danyelle Gilliam | ||
| 33 | // Modified Header | ||
| 34 | // | ||
| 35 | //****************************************************************************************************** | ||
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | using GSF.Adapters; | ||
| 41 | using GSF.Configuration; | ||
| 42 | using System; | ||
| 43 | using System.IdentityModel.Policy; | ||
| 44 | using System.ServiceModel; | ||
| 45 | |||
| 46 | namespace GSF.ServiceModel | ||
| 47 | { | ||
| 48 | /// <summary> | ||
| 49 | /// Defines a web service that can send and receive data over REST (Representational State Transfer) interface. | ||
| 50 | /// </summary> | ||
| 51 | [ServiceContract()] | ||
| 52 | public interface ISelfHostingService : IAdapter, ISupportLifecycle, IPersistSettings | ||
| 53 | { | ||
| 54 | #region [ Members ] | ||
| 55 | |||
| 56 | /// <summary> | ||
| 57 | /// Occurs when the <see cref="ServiceHost"/> has been created with the specified <see cref="Endpoints"/>. | ||
| 58 | /// </summary> | ||
| 59 | event EventHandler ServiceHostCreated; | ||
| 60 | |||
| 61 | /// <summary> | ||
| 62 | /// Occurs when the <see cref="ServiceHost"/> can process requests via all of its endpoints. | ||
| 63 | /// </summary> | ||
| 64 | event EventHandler ServiceHostStarted; | ||
| 65 | |||
| 66 | /// <summary> | ||
| 67 | /// Occurs when an <see cref="Exception"/> is encountered when processing a request. | ||
| 68 | /// </summary> | ||
| 69 | event EventHandler<EventArgs<Exception>> ServiceProcessException; | ||
| 70 | |||
| 71 | #endregion | ||
| 72 | |||
| 73 | #region [ Properties ] | ||
| 74 | |||
| 75 | /// <summary> | ||
| 76 | /// Gets or sets a semicolon delimited list of URIs where the web service can be accessed. | ||
| 77 | /// </summary> | ||
| 78 | string Endpoints { get; set; } | ||
| 79 | |||
| 80 | /// <summary> | ||
| 81 | /// Gets or sets the <see cref="Type.FullName"/> of the contract interface implemented by the web service. | ||
| 82 | /// </summary> | ||
| 83 | string Contract { get; set; } | ||
| 84 | |||
| 85 | /// <summary> | ||
| 86 | /// Gets or sets a boolean value that indicates whether the <see cref="ServiceHost"/> will use the current instance of the web service for processing | ||
| 87 | /// requests or base the web service instance creation on <see cref="InstanceContextMode"/> specified in its <see cref="ServiceBehaviorAttribute"/>. | ||
| 88 | /// </summary> | ||
| 89 | bool Singleton { get; set; } | ||
| 90 | |||
| 91 | /// <summary> | ||
| 92 | /// Gets or sets the <see cref="Type.FullName"/> of <see cref="IAuthorizationPolicy"/> to be used for securing all web service <see cref="Endpoints"/>. | ||
| 93 | /// </summary> | ||
| 94 | string SecurityPolicy { get; set; } | ||
| 95 | |||
| 96 | /// <summary> | ||
| 97 | /// Gets or sets a boolean value that indicates whether web service metadata is to made available at all web service <see cref="Endpoints"/>. | ||
| 98 | /// </summary> | ||
| 99 | bool PublishMetadata { get; set; } | ||
| 100 | |||
| 101 | /// <summary> | ||
| 102 | /// Gets the <see cref="ServiceHost"/> hosting the web service. | ||
| 103 | /// </summary> | ||
| 104 | ServiceHost ServiceHost { get; } | ||
| 105 | |||
| 106 | #endregion | ||
| 107 | } | ||
| 108 | } | ||
| @@ -0,0 +1,39 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // NamespaceDoc.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 11/19/2009 - Pinal C. Patel | ||
| 20 | // Generated original version of source code. | ||
| 21 | // 10/8/2012 - Danyelle Gilliam | ||
| 22 | // Modified Header | ||
| 23 | // | ||
| 24 | //****************************************************************************************************** | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | using System.Runtime.CompilerServices; | ||
| 29 | |||
| 30 | namespace GSF.ServiceModel | ||
| 31 | { | ||
| 32 | /// <summary> | ||
| 33 | /// Contains classes and interfaces that provide the basic infrastructure for writting web services. | ||
| 34 | /// </summary> | ||
| 35 | [CompilerGenerated()] | ||
| 36 | class NamespaceDoc | ||
| 37 | { | ||
| 38 | } | ||
| 39 | } | ||
| @@ -0,0 +1,26 @@ | |||
| 1 | using System; | ||
| 2 | using System.Reflection; | ||
| 3 | using System.Runtime.InteropServices; | ||
| 4 | |||
| 5 | // Assembly identity attributes. | ||
| 6 | [assembly: AssemblyVersion("4.0.4.15")] | ||
| 7 | |||
| 8 | // Informational attributes. | ||
| 9 | [assembly: AssemblyCompany("TVA")] | ||
| 10 | [assembly: AssemblyCopyright("No copyright is claimed pursuant to 17 USC § 105. All Other Rights Reserved.")] | ||
| 11 | [assembly: AssemblyProduct("TVA Code Library")] | ||
| 12 | |||
| 13 | // Assembly manifest attributes. | ||
| 14 | #if DEBUG | ||
| 15 | [assembly: AssemblyConfiguration("Debug Build")] | ||
| 16 | #else | ||
| 17 | [assembly: AssemblyConfiguration("Release Build")] | ||
| 18 | #endif | ||
| 19 | [assembly: AssemblyDefaultAlias("TVA.ServiceModel")] | ||
| 20 | [assembly: AssemblyDescription("Library of base WCF service with self-hosting capability, and WCF Authorization Policy and Host Factories for implementing role-based security.")] | ||
| 21 | [assembly: AssemblyTitle("TVA.ServiceModel")] | ||
| 22 | |||
| 23 | // Other configuration attributes. | ||
| 24 | [assembly: CLSCompliant(true)] | ||
| 25 | [assembly: ComVisible(false)] | ||
| 26 | [assembly: Guid("57b04fab-3c0f-4d28-ad88-47e4222ebbe0")] | ||
| @@ -0,0 +1,295 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // SecurityPolicy.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 04/22/2010 - Pinal C. Patel | ||
| 20 | // Generated original version of source code. | ||
| 21 | // 05/27/2010 - Pinal C. Patel | ||
| 22 | // Added usage example to code comments. | ||
| 23 | // Modified Evaluate() to make use of IncludedResources and ExcludedResources config file settings. | ||
| 24 | // 06/02/2010 - Pinal C. Patel | ||
| 25 | // Added authentication check to Evaluate() method. | ||
| 26 | // 08/11/2010 - Pinal C. Patel | ||
| 27 | // Made key methods virtual for extensibility. | ||
| 28 | // 10/14/2010 - Pinal C. Patel | ||
| 29 | // Modified GetResourceName() to return path and query for consistency across WCF hosting | ||
| 30 | // environments (i.e. ASP.NET, WAS, etc.). | ||
| 31 | // 10/8/2012 - Danyelle Gilliam | ||
| 32 | // Modified Header | ||
| 33 | // | ||
| 34 | //****************************************************************************************************** | ||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | using GSF.Security; | ||
| 39 | using System; | ||
| 40 | using System.Collections.Generic; | ||
| 41 | using System.IdentityModel.Claims; | ||
| 42 | using System.IdentityModel.Policy; | ||
| 43 | using System.Security; | ||
| 44 | using System.Security.Principal; | ||
| 45 | using System.ServiceModel; | ||
| 46 | using System.Threading; | ||
| 47 | |||
| 48 | namespace GSF.ServiceModel | ||
| 49 | { | ||
| 50 | /// <summary> | ||
| 51 | /// Represents an <see cref="IAuthorizationPolicy">authorization policy</see> that can be used by WCF services for enabling role-based security. | ||
| 52 | /// </summary> | ||
| 53 | /// <example> | ||
| 54 | /// Common config file entries: | ||
| 55 | /// <code> | ||
| 56 | /// <![CDATA[ | ||
| 57 | /// <?xml version="1.0"?> | ||
| 58 | /// <configuration> | ||
| 59 | /// <configSections> | ||
| 60 | /// <section name="categorizedSettings" type="GSF.Configuration.CategorizedSettingsSection, GSF.Core" /> | ||
| 61 | /// </configSections> | ||
| 62 | /// <categorizedSettings> | ||
| 63 | /// <securityProvider> | ||
| 64 | /// <add name="ApplicationName" value="" description="Name of the application being secured as defined in the backend security datastore." | ||
| 65 | /// encrypted="false" /> | ||
| 66 | /// <add name="ConnectionString" value="" description="Connection string to be used for connection to the backend security datastore." | ||
| 67 | /// encrypted="false" /> | ||
| 68 | /// <add name="ProviderType" value="GSF.Security.LdapSecurityProvider, GSF.Security" | ||
| 69 | /// description="The type to be used for enforcing security." encrypted="false" /> | ||
| 70 | /// <add name="IncludedResources" value="*/*.*=*" description="Semicolon delimited list of resources to be secured along with role names." | ||
| 71 | /// encrypted="false" /> | ||
| 72 | /// <add name="ExcludedResources" value="*/SecurityService.svc*" | ||
| 73 | /// description="Semicolon delimited list of resources to be excluded from being secured." | ||
| 74 | /// encrypted="false" /> | ||
| 75 | /// <add name="NotificationSmtpServer" value="localhost" description="SMTP server to be used for sending out email notification messages." | ||
| 76 | /// encrypted="false" /> | ||
| 77 | /// <add name="NotificationSenderEmail" value="sender@company.com" description="Email address of the sender of email notification messages." | ||
| 78 | /// encrypted="false" /> | ||
| 79 | /// </securityProvider> | ||
| 80 | /// <activeDirectory> | ||
| 81 | /// <add name="PrivilegedDomain" value="" description="Domain of privileged domain user account." | ||
| 82 | /// encrypted="false" /> | ||
| 83 | /// <add name="PrivilegedUserName" value="" description="Username of privileged domain user account." | ||
| 84 | /// encrypted="false" /> | ||
| 85 | /// <add name="PrivilegedPassword" value="" description="Password of privileged domain user account." | ||
| 86 | /// encrypted="true" /> | ||
| 87 | /// </activeDirectory> | ||
| 88 | /// </categorizedSettings> | ||
| 89 | /// </configuration> | ||
| 90 | /// ]]> | ||
| 91 | /// </code> | ||
| 92 | /// Internal WCF service configuration: | ||
| 93 | /// <code> | ||
| 94 | /// <![CDATA[ | ||
| 95 | /// <?xml version="1.0"?> | ||
| 96 | /// <configuration> | ||
| 97 | /// <system.serviceModel> | ||
| 98 | /// <services> | ||
| 99 | /// <service name="WcfService1.Service1" behaviorConfiguration="serviceBehavior"> | ||
| 100 | /// <endpoint address="" contract="WcfService1.IService1" binding="webHttpBinding" | ||
| 101 | /// bindingConfiguration="endpointBinding" behaviorConfiguration="endpointBehavior" /> | ||
| 102 | /// </service> | ||
| 103 | /// </services> | ||
| 104 | /// <behaviors> | ||
| 105 | /// <endpointBehaviors> | ||
| 106 | /// <behavior name="endpointBehavior"> | ||
| 107 | /// <webHttp/> | ||
| 108 | /// </behavior> | ||
| 109 | /// </endpointBehaviors> | ||
| 110 | /// <serviceBehaviors> | ||
| 111 | /// <behavior name="serviceBehavior"> | ||
| 112 | /// <serviceAuthorization principalPermissionMode="Custom"> | ||
| 113 | /// <authorizationPolicies> | ||
| 114 | /// <add policyType="GSF.ServiceModel.SecurityPolicy, GSF.ServiceModel" /> | ||
| 115 | /// </authorizationPolicies> | ||
| 116 | /// </serviceAuthorization> | ||
| 117 | /// </behavior> | ||
| 118 | /// </serviceBehaviors> | ||
| 119 | /// </behaviors> | ||
| 120 | /// <bindings> | ||
| 121 | /// <webHttpBinding> | ||
| 122 | /// <binding name="endpointBinding"> | ||
| 123 | /// <security mode="TransportCredentialOnly"> | ||
| 124 | /// <transport clientCredentialType="Windows"/> | ||
| 125 | /// </security> | ||
| 126 | /// </binding> | ||
| 127 | /// </webHttpBinding> | ||
| 128 | /// </bindings> | ||
| 129 | /// <serviceHostingEnvironment aspNetCompatibilityEnabled="false" /> | ||
| 130 | /// </system.serviceModel> | ||
| 131 | /// </configuration> | ||
| 132 | /// ]]> | ||
| 133 | /// </code> | ||
| 134 | /// External WCF service configuration: | ||
| 135 | /// <code> | ||
| 136 | /// <![CDATA[ | ||
| 137 | /// <?xml version="1.0"?> | ||
| 138 | /// <configuration> | ||
| 139 | /// <system.web> | ||
| 140 | /// <httpModules> | ||
| 141 | /// <add name="SecurityModule" type="GSF.ServiceModel.SecurityModule, GSF.ServiceModel" /> | ||
| 142 | /// </httpModules> | ||
| 143 | /// </system.web> | ||
| 144 | /// <system.serviceModel> | ||
| 145 | /// <services> | ||
| 146 | /// <service name="WcfService1.Service1" behaviorConfiguration="serviceBehavior"> | ||
| 147 | /// <endpoint address="" contract="WcfService1.IService1" binding="webHttpBinding" | ||
| 148 | /// bindingConfiguration="endpointBinding" behaviorConfiguration="endpointBehavior"/> | ||
| 149 | /// </service> | ||
| 150 | /// </services> | ||
| 151 | /// <behaviors> | ||
| 152 | /// <endpointBehaviors> | ||
| 153 | /// <behavior name="endpointBehavior"> | ||
| 154 | /// <webHttp/> | ||
| 155 | /// </behavior> | ||
| 156 | /// </endpointBehaviors> | ||
| 157 | /// <serviceBehaviors> | ||
| 158 | /// <behavior name="serviceBehavior"> | ||
| 159 | /// <serviceAuthorization principalPermissionMode="Custom"> | ||
| 160 | /// <authorizationPolicies> | ||
| 161 | /// <add policyType="GSF.ServiceModel.SecurityPolicy, GSF.ServiceModel" /> | ||
| 162 | /// </authorizationPolicies> | ||
| 163 | /// </serviceAuthorization> | ||
| 164 | /// </behavior> | ||
| 165 | /// </serviceBehaviors> | ||
| 166 | /// </behaviors> | ||
| 167 | /// <bindings> | ||
| 168 | /// <webHttpBinding> | ||
| 169 | /// <binding name="endpointBinding"> | ||
| 170 | /// <security mode="None" /> | ||
| 171 | /// </binding> | ||
| 172 | /// </webHttpBinding> | ||
| 173 | /// </bindings> | ||
| 174 | /// <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> | ||
| 175 | /// </system.serviceModel> | ||
| 176 | /// </configuration> | ||
| 177 | /// ]]> | ||
| 178 | /// </code> | ||
| 179 | /// </example> | ||
| 180 | /// <seealso cref="ISecurityProvider"/> | ||
| 181 | public class SecurityPolicy : IAuthorizationPolicy | ||
| 182 | { | ||
| 183 | #region [ Members ] | ||
| 184 | |||
| 185 | // Fields | ||
| 186 | private Guid m_id; | ||
| 187 | |||
| 188 | #endregion | ||
| 189 | |||
| 190 | #region [ Constructors ] | ||
| 191 | |||
| 192 | /// <summary> | ||
| 193 | /// Initializes a new instance of the <see cref="SecurityPolicy"/> class. | ||
| 194 | /// </summary> | ||
| 195 | public SecurityPolicy() | ||
| 196 | { | ||
| 197 | m_id = Guid.NewGuid(); | ||
| 198 | } | ||
| 199 | |||
| 200 | #endregion | ||
| 201 | |||
| 202 | #region [ Properties ] | ||
| 203 | |||
| 204 | /// <summary> | ||
| 205 | /// Gets the identifier of this <see cref="SecurityPolicy"/> instance. | ||
| 206 | /// </summary> | ||
| 207 | public string Id | ||
| 208 | { | ||
| 209 | get { return m_id.ToString(); } | ||
| 210 | } | ||
| 211 | |||
| 212 | /// <summary> | ||
| 213 | /// Gets a claim set that represents the issuer of this <see cref="SecurityPolicy"/>. | ||
| 214 | /// </summary> | ||
| 215 | public ClaimSet Issuer | ||
| 216 | { | ||
| 217 | get { return ClaimSet.System; } | ||
| 218 | } | ||
| 219 | |||
| 220 | #endregion | ||
| 221 | |||
| 222 | #region [ Methods ] | ||
| 223 | |||
| 224 | /// <summary> | ||
| 225 | /// Evaluates the <paramref name="evaluationContext"/> and initializes security. | ||
| 226 | /// </summary> | ||
| 227 | /// <param name="evaluationContext">An <see cref="EvaluationContext"/> object.</param> | ||
| 228 | /// <param name="state">Custom state of the <see cref="SecurityPolicy"/>.</param> | ||
| 229 | /// <returns></returns> | ||
| 230 | public virtual bool Evaluate(EvaluationContext evaluationContext, ref object state) | ||
| 231 | { | ||
| 232 | // In order for this to work properly security on the binding must be configured to use windows security. | ||
| 233 | // When this is done the caller's windows identity is available to us here and can be used to derive from | ||
| 234 | // it the security principal that can be used by WCF service code downstream for implementing security. | ||
| 235 | object property; | ||
| 236 | if (evaluationContext.Properties.TryGetValue("Identities", out property)) | ||
| 237 | { | ||
| 238 | // Extract and assign the caller's windows identity to current thread if available. | ||
| 239 | IList<IIdentity> identities = property as List<IIdentity>; | ||
| 240 | |||
| 241 | if ((object)identities == null) | ||
| 242 | throw new SecurityException(string.Format("Null Dereterence Exception: '{0}'", Thread.CurrentPrincipal.Identity.ToString())); | ||
| 243 | |||
| 244 | foreach (IIdentity identity in identities) | ||
| 245 | { | ||
| 246 | if (identity is WindowsIdentity) | ||
| 247 | { | ||
| 248 | Thread.CurrentPrincipal = new WindowsPrincipal((WindowsIdentity)identity); | ||
| 249 | |||
| 250 | break; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | string resource = GetResourceName(); | ||
| 256 | if (SecurityProviderUtility.IsResourceSecurable(resource)) | ||
| 257 | { | ||
| 258 | // Initialize the security principal from caller's windows identity if uninitialized. | ||
| 259 | if (SecurityProviderCache.CurrentProvider == null) | ||
| 260 | SecurityProviderCache.CurrentProvider = SecurityProviderUtility.CreateProvider(string.Empty); | ||
| 261 | |||
| 262 | // Setup the principal to be attached to the thread on which WCF service will execute. | ||
| 263 | evaluationContext.Properties["Principal"] = Thread.CurrentPrincipal; | ||
| 264 | |||
| 265 | // Verify that the current thread principal has been authenticated. | ||
| 266 | if (!Thread.CurrentPrincipal.Identity.IsAuthenticated) | ||
| 267 | throw new SecurityException(string.Format("Authentication failed for user '{0}'", Thread.CurrentPrincipal.Identity.Name)); | ||
| 268 | |||
| 269 | // Perform a top-level permission check on the resource being accessed. | ||
| 270 | if (!SecurityProviderUtility.IsResourceAccessible(resource)) | ||
| 271 | throw new SecurityException(string.Format("Access to '{0}' is denied", resource)); | ||
| 272 | |||
| 273 | return true; | ||
| 274 | } | ||
| 275 | else | ||
| 276 | { | ||
| 277 | // Setup the principal to be attached to the thread on which WCF service will execute. | ||
| 278 | evaluationContext.Properties["Principal"] = Thread.CurrentPrincipal; | ||
| 279 | |||
| 280 | return true; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | /// <summary> | ||
| 285 | /// Gets the name of resource being accessed. | ||
| 286 | /// </summary> | ||
| 287 | /// <returns><see cref="OperationContext.IncomingMessageHeaders"/>.<see cref="System.ServiceModel.Channels.MessageHeaders.To"/>.<see cref="Uri.PathAndQuery"/> property value.</returns> | ||
| 288 | protected virtual string GetResourceName() | ||
| 289 | { | ||
| 290 | return OperationContext.Current.IncomingMessageHeaders.To.PathAndQuery; | ||
| 291 | } | ||
| 292 | |||
| 293 | #endregion | ||
| 294 | } | ||
| 295 | } | ||
| @@ -0,0 +1,759 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // SelfHostingService.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 08/27/2009 - Pinal C. Patel | ||
| 20 | // Generated original version of source code. | ||
| 21 | // 09/02/2009 - Pinal C. Patel | ||
| 22 | // Modified configuration of the default WebHttpBinding to enable receiving of large payloads. | ||
| 23 | // 09/15/2009 - Stephen C. Wills | ||
| 24 | // Added new header and license agreement. | ||
| 25 | // 11/27/2009 - Pinal C. Patel | ||
| 26 | // Fixed bug in the initialization of service contract name. | ||
| 27 | // 03/30/2010 - Pinal C. Patel | ||
| 28 | // Updated CanRead and CanWrite to not include Enabled in its evaluation. | ||
| 29 | // 05/28/2010 - Pinal C. Patel | ||
| 30 | // Added an endpoint for web service help. | ||
| 31 | // 06/21/2010 - Pinal C. Patel | ||
| 32 | // Added Singleton property for added control over the hosting process. | ||
| 33 | // 10/08/2010 - Pinal C. Patel | ||
| 34 | // Removed REST web service help endpoint since a similar feature is now part of WCF 4.0. | ||
| 35 | // 10/14/2010 - Pinal C. Patel | ||
| 36 | // Made changes for hosting flexibility and enabling security: | ||
| 37 | // Deleted DataFlow since access restriction can now be imposed by enabling security. | ||
| 38 | // Added SecurityPolicy and PublishMetadata. | ||
| 39 | // Renamed ServiceUri to Endpoints and ServiceContract to Contract. | ||
| 40 | // 10/26/2010 - Pinal C. Patel | ||
| 41 | // Modified the implementation of ISupportLifecycle.Enabled property. | ||
| 42 | // 10/29/2010 - Pinal C. Patel | ||
| 43 | // Modified CreateServiceBinding() to explicitly disable security on created binding if specified. | ||
| 44 | // 11/19/2010 - Pinal C. Patel | ||
| 45 | // Changed to inherit from Adapter to take advantage of app domain isolation through AdapterLoader. | ||
| 46 | // 12/09/2010 - Pinal C. Patel | ||
| 47 | // Modified GetUnusedPort() to use GSF.Security.Cryptography.Random instead of System.Random to | ||
| 48 | // unsure unique numbers. | ||
| 49 | // 11/29/2011 - Pinal C. Patel | ||
| 50 | // Modified InitializeServiceHost() to not use a random port number for the service host address | ||
| 51 | // so that setting up security on the address is possible. | ||
| 52 | // 08/02/2012 - J. Ritchie Carroll | ||
| 53 | // Added cross-domain access support for Silverlight and Flash application using self hosted | ||
| 54 | // web services (enables access to clientaccesspolicy.xml and crossdomain.xml). | ||
| 55 | // 10/8/2012 - Danyelle Gilliam | ||
| 56 | // Modified Header | ||
| 57 | // | ||
| 58 | //****************************************************************************************************** | ||
| 59 | |||
| 60 | |||
| 61 | #region [ Contributor License Agreements ] | ||
| 62 | |||
| 63 | //****************************************************************************************************** | ||
| 64 | // | ||
| 65 | // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. | ||
| 66 | // | ||
| 67 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 68 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 69 | // | ||
| 70 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 71 | // | ||
| 72 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 73 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 74 | // License for the specific language governing permissions and limitations. | ||
| 75 | // | ||
| 76 | //****************************************************************************************************** | ||
| 77 | |||
| 78 | #endregion | ||
| 79 | |||
| 80 | using GSF.Adapters; | ||
| 81 | using GSF.Configuration; | ||
| 82 | using System; | ||
| 83 | using System.Collections.Generic; | ||
| 84 | using System.IdentityModel.Policy; | ||
| 85 | using System.IO; | ||
| 86 | using System.Linq; | ||
| 87 | using System.Net; | ||
| 88 | using System.Net.NetworkInformation; | ||
| 89 | using System.ServiceModel; | ||
| 90 | using System.ServiceModel.Channels; | ||
| 91 | using System.ServiceModel.Description; | ||
| 92 | using System.ServiceModel.Web; | ||
| 93 | using System.Text; | ||
| 94 | using System.Text.RegularExpressions; | ||
| 95 | |||
| 96 | namespace GSF.ServiceModel | ||
| 97 | { | ||
| 98 | /// <summary> | ||
| 99 | /// A base class for web service that can send and receive data over REST (Representational State Transfer) interface. | ||
| 100 | /// </summary> | ||
| 101 | /// <example> | ||
| 102 | /// This example shows how to create a WCF service derived from <see cref="SelfHostingService"/> that is capable of hosting itself: | ||
| 103 | /// <code> | ||
| 104 | /// using System.ServiceModel; | ||
| 105 | /// using System.ServiceModel.Web; | ||
| 106 | /// using GSF.ServiceModel; | ||
| 107 | /// | ||
| 108 | /// namespace Services | ||
| 109 | /// { | ||
| 110 | /// [ServiceContract()] | ||
| 111 | /// public interface IService : ISelfHostingService | ||
| 112 | /// { | ||
| 113 | /// [OperationContract(), WebGet(UriTemplate = "/hello/{name}")] | ||
| 114 | /// string Hello(string name); | ||
| 115 | /// } | ||
| 116 | /// | ||
| 117 | /// public class Service : SelfHostingService, IService | ||
| 118 | /// { | ||
| 119 | /// public string Hello(string name) | ||
| 120 | /// { | ||
| 121 | /// return string.Format("Hello {0}!", name); | ||
| 122 | /// } | ||
| 123 | /// } | ||
| 124 | /// } | ||
| 125 | /// </code> | ||
| 126 | /// This example shows how to activate a WCF service derived from <see cref="SelfHostingService"/> that is capable of hosting itself: | ||
| 127 | /// <code> | ||
| 128 | /// using System; | ||
| 129 | /// using System.ServiceModel; | ||
| 130 | /// using System.ServiceModel.Description; | ||
| 131 | /// using System.ServiceModel.Web; | ||
| 132 | /// using Services; | ||
| 133 | /// using GSF.ServiceModel; | ||
| 134 | /// | ||
| 135 | /// class Program | ||
| 136 | /// { | ||
| 137 | /// static void Main(string[] args) | ||
| 138 | /// { | ||
| 139 | /// // Initialize web service. | ||
| 140 | /// Service service = new Service(); | ||
| 141 | /// service.PublishMetadata = true; | ||
| 142 | /// service.Endpoints = "http.soap11://localhost:4500/soap; http.rest://localhost:4500/rest"; | ||
| 143 | /// service.Initialize(); | ||
| 144 | /// | ||
| 145 | /// // Show web service status. | ||
| 146 | /// if (service.ServiceHost.State == CommunicationState.Opened) | ||
| 147 | /// { | ||
| 148 | /// Console.WriteLine("\r\n{0} is running:", service.GetType().Name); | ||
| 149 | /// foreach (ServiceEndpoint endpoint in service.ServiceHost.Description.Endpoints) | ||
| 150 | /// { | ||
| 151 | /// Console.WriteLine("- {0} ({1})", endpoint.Address, endpoint.Binding.GetType().Name); | ||
| 152 | /// } | ||
| 153 | /// Console.Write("\r\nPress Enter key to stop..."); | ||
| 154 | /// } | ||
| 155 | /// else | ||
| 156 | /// { | ||
| 157 | /// Console.WriteLine("\r\n{0} could not be started", service.GetType().Name); | ||
| 158 | /// } | ||
| 159 | /// | ||
| 160 | /// // Shutdown. | ||
| 161 | /// Console.ReadLine(); | ||
| 162 | /// service.Dispose(); | ||
| 163 | /// } | ||
| 164 | /// } | ||
| 165 | /// </code> | ||
| 166 | /// This example shows how to host a WCF service derived from <see cref="SelfHostingService"/> inside ASP.NET: | ||
| 167 | /// <code> | ||
| 168 | /// <![CDATA[ | ||
| 169 | /// <?xml version="1.0"?> | ||
| 170 | /// <configuration> | ||
| 171 | /// <system.serviceModel> | ||
| 172 | /// <services> | ||
| 173 | /// <service name="Services.Service"> | ||
| 174 | /// <endpoint address="soap" contract="Services.IService" binding="basicHttpBinding"/> | ||
| 175 | /// <endpoint address="rest" contract="Services.IService" binding="webHttpBinding" behaviorConfiguration="restBehavior"/> | ||
| 176 | /// </service> | ||
| 177 | /// </services> | ||
| 178 | /// <behaviors> | ||
| 179 | /// <serviceBehaviors> | ||
| 180 | /// <behavior> | ||
| 181 | /// <serviceMetadata httpGetEnabled="true"/> | ||
| 182 | /// </behavior> | ||
| 183 | /// </serviceBehaviors> | ||
| 184 | /// <endpointBehaviors> | ||
| 185 | /// <behavior name="restBehavior"> | ||
| 186 | /// <webHttp helpEnabled="true"/> | ||
| 187 | /// </behavior> | ||
| 188 | /// </endpointBehaviors> | ||
| 189 | /// </behaviors> | ||
| 190 | /// <serviceHostingEnvironment multipleSiteBindingsEnabled="true"> | ||
| 191 | /// <serviceActivations> | ||
| 192 | /// <add relativeAddress="Service.svc" service="Services.Service, Services"/> | ||
| 193 | /// </serviceActivations> | ||
| 194 | /// </serviceHostingEnvironment> | ||
| 195 | /// </system.serviceModel> | ||
| 196 | /// </configuration> | ||
| 197 | /// ]]> | ||
| 198 | /// </code> | ||
| 199 | /// </example> | ||
| 200 | public class SelfHostingService : Adapter, ISelfHostingService, IPolicyRetriever | ||
| 201 | { | ||
| 202 | #region [ Members ] | ||
| 203 | |||
| 204 | // Events | ||
| 205 | |||
| 206 | /// <summary> | ||
| 207 | /// Occurs when the <see cref="ServiceHost"/> has been created with the specified <see cref="Endpoints"/>. | ||
| 208 | /// </summary> | ||
| 209 | /// <remarks> | ||
| 210 | /// When <see cref="ServiceHostCreated"/> event is fired, changes like adding new endpoints can be made to the <see cref="ServiceHost"/>. | ||
| 211 | /// </remarks> | ||
| 212 | public event EventHandler ServiceHostCreated; | ||
| 213 | |||
| 214 | /// <summary> | ||
| 215 | /// Occurs when the <see cref="ServiceHost"/> has can process requests via all of its endpoints. | ||
| 216 | /// </summary> | ||
| 217 | public event EventHandler ServiceHostStarted; | ||
| 218 | |||
| 219 | /// <summary> | ||
| 220 | /// Occurs when an <see cref="Exception"/> is encountered when processing a request. | ||
| 221 | /// </summary> | ||
| 222 | /// <remarks> | ||
| 223 | /// <see cref="EventArgs{T}.Argument"/> is the exception encountered when processing a request. | ||
| 224 | /// </remarks> | ||
| 225 | public event EventHandler<EventArgs<Exception>> ServiceProcessException; | ||
| 226 | |||
| 227 | // Fields | ||
| 228 | private string m_endpoints; | ||
| 229 | private string m_contract; | ||
| 230 | private bool m_singleton; | ||
| 231 | private string m_securityPolicy; | ||
| 232 | private bool m_publishMetadata; | ||
| 233 | private bool m_allowCrossDomainAccess; | ||
| 234 | private string m_allowedDomainList; | ||
| 235 | private bool m_disposed; | ||
| 236 | private bool m_initialized; | ||
| 237 | private ServiceHost m_serviceHost; | ||
| 238 | |||
| 239 | #endregion | ||
| 240 | |||
| 241 | #region [ Constructors ] | ||
| 242 | |||
| 243 | /// <summary> | ||
| 244 | /// Initializes a new instance of the web service. | ||
| 245 | /// </summary> | ||
| 246 | protected SelfHostingService() | ||
| 247 | : base() | ||
| 248 | { | ||
| 249 | Type type = this.GetType(); | ||
| 250 | m_contract = type.Namespace + ".I" + type.Name + ", " + type.AssemblyQualifiedName.Split(',')[1].Trim(); | ||
| 251 | m_allowCrossDomainAccess = false; | ||
| 252 | m_allowedDomainList = "*"; | ||
| 253 | } | ||
| 254 | |||
| 255 | /// <summary> | ||
| 256 | /// Releases the unmanaged resources before the web service is reclaimed by <see cref="GC"/>. | ||
| 257 | /// </summary> | ||
| 258 | ~SelfHostingService() | ||
| 259 | { | ||
| 260 | Dispose(false); | ||
| 261 | } | ||
| 262 | |||
| 263 | #endregion | ||
| 264 | |||
| 265 | #region [ Properties ] | ||
| 266 | |||
| 267 | /// <summary> | ||
| 268 | /// Gets or sets a boolean value that indicates whether the web service is currently enabled. | ||
| 269 | /// </summary> | ||
| 270 | public override bool Enabled | ||
| 271 | { | ||
| 272 | get | ||
| 273 | { | ||
| 274 | return (m_serviceHost != null && m_serviceHost.State == CommunicationState.Opened); | ||
| 275 | } | ||
| 276 | set | ||
| 277 | { | ||
| 278 | if (value && !Enabled) | ||
| 279 | { | ||
| 280 | // Enable | ||
| 281 | if (!m_initialized) | ||
| 282 | Initialize(); | ||
| 283 | else | ||
| 284 | InitializeServiceHost(); | ||
| 285 | } | ||
| 286 | else if (!value && Enabled) | ||
| 287 | { | ||
| 288 | // Disable | ||
| 289 | m_serviceHost.Close(); | ||
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | /// <summary> | ||
| 295 | /// Gets or sets a semicolon delimited list of URIs where the web service can be accessed. | ||
| 296 | /// </summary> | ||
| 297 | /// <remarks> | ||
| 298 | /// Set <see cref="Endpoints"/> to a null or empty string to disable web service hosting. Refer to <see cref="Service.CreateServiceBinding"/> | ||
| 299 | /// for a list of supported URI formats. | ||
| 300 | /// </remarks> | ||
| 301 | public string Endpoints | ||
| 302 | { | ||
| 303 | get | ||
| 304 | { | ||
| 305 | return m_endpoints; | ||
| 306 | } | ||
| 307 | set | ||
| 308 | { | ||
| 309 | m_endpoints = value; | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | /// <summary> | ||
| 314 | /// Gets or sets the <see cref="Type.FullName"/> of the contract interface implemented by the web service. | ||
| 315 | /// </summary> | ||
| 316 | /// <exception cref="ArgumentNullException">The value being assigned is a null or empty string.</exception> | ||
| 317 | public string Contract | ||
| 318 | { | ||
| 319 | get | ||
| 320 | { | ||
| 321 | return m_contract; | ||
| 322 | } | ||
| 323 | set | ||
| 324 | { | ||
| 325 | if (string.IsNullOrEmpty(value)) | ||
| 326 | throw new ArgumentNullException("value"); | ||
| 327 | |||
| 328 | m_contract = value; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | /// <summary> | ||
| 333 | /// Gets or sets a boolean value that indicates whether the <see cref="ServiceHost"/> will use the current instance of the web service for processing | ||
| 334 | /// requests or base the web service instance creation on <see cref="InstanceContextMode"/> specified in its <see cref="ServiceBehaviorAttribute"/>. | ||
| 335 | /// </summary> | ||
| 336 | public bool Singleton | ||
| 337 | { | ||
| 338 | get | ||
| 339 | { | ||
| 340 | return m_singleton; | ||
| 341 | } | ||
| 342 | set | ||
| 343 | { | ||
| 344 | m_singleton = value; | ||
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | /// <summary> | ||
| 349 | /// Gets or sets the <see cref="Type.FullName"/> of <see cref="IAuthorizationPolicy"/> to be used for securing all web service <see cref="Endpoints"/>. | ||
| 350 | /// </summary> | ||
| 351 | public string SecurityPolicy | ||
| 352 | { | ||
| 353 | get | ||
| 354 | { | ||
| 355 | return m_securityPolicy; | ||
| 356 | } | ||
| 357 | set | ||
| 358 | { | ||
| 359 | m_securityPolicy = value; | ||
| 360 | } | ||
| 361 | } | ||
| 362 | |||
| 363 | /// <summary> | ||
| 364 | /// Gets or sets a boolean value that indicates whether web service metadata is to made available at all web service <see cref="Endpoints"/>. | ||
| 365 | /// </summary> | ||
| 366 | public bool PublishMetadata | ||
| 367 | { | ||
| 368 | get | ||
| 369 | { | ||
| 370 | return m_publishMetadata; | ||
| 371 | } | ||
| 372 | set | ||
| 373 | { | ||
| 374 | m_publishMetadata = value; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | /// <summary> | ||
| 379 | /// Gets or sets flag that indicates if web services will enable cross-domain access for Silverlight and Flash applications. | ||
| 380 | /// </summary> | ||
| 381 | public bool AllowCrossDomainAccess | ||
| 382 | { | ||
| 383 | get | ||
| 384 | { | ||
| 385 | return m_allowCrossDomainAccess; | ||
| 386 | } | ||
| 387 | set | ||
| 388 | { | ||
| 389 | m_allowCrossDomainAccess = value; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | |||
| 393 | /// <summary> | ||
| 394 | /// Gets or sets comma separated list of allowed domains when <see cref="AllowCrossDomainAccess"/> is <c>true</c>. Use * for domain wildcards, e.g., *.consoto.com. | ||
| 395 | /// </summary> | ||
| 396 | /// <remarks> | ||
| 397 | /// To allow all domains just set this property to "*". | ||
| 398 | /// </remarks> | ||
| 399 | public string AllowedDomainList | ||
| 400 | { | ||
| 401 | get | ||
| 402 | { | ||
| 403 | return m_allowedDomainList; | ||
| 404 | } | ||
| 405 | set | ||
| 406 | { | ||
| 407 | m_allowedDomainList = value; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | /// <summary> | ||
| 412 | /// Gets the <see cref="WebServiceHost"/> hosting the web service. | ||
| 413 | /// </summary> | ||
| 414 | /// <remarks> | ||
| 415 | /// By default, the <see cref="ServiceHost"/> only has <see cref="WebHttpBinding"/> endpoint at the <see cref="Endpoints"/>. | ||
| 416 | /// Additional endpoints can be added to the <see cref="ServiceHost"/> when <see cref="ServiceHostCreated"/> event is fired. | ||
| 417 | /// </remarks> | ||
| 418 | public ServiceHost ServiceHost | ||
| 419 | { | ||
| 420 | get | ||
| 421 | { | ||
| 422 | return m_serviceHost; | ||
| 423 | } | ||
| 424 | } | ||
| 425 | |||
| 426 | #endregion | ||
| 427 | |||
| 428 | #region [ Methods ] | ||
| 429 | |||
| 430 | /// <summary> | ||
| 431 | /// Initializes the web service. | ||
| 432 | /// </summary> | ||
| 433 | public override void Initialize() | ||
| 434 | { | ||
| 435 | base.Initialize(); | ||
| 436 | if (!m_initialized) | ||
| 437 | { | ||
| 438 | InitializeServiceHost(); // Initialize the service host. | ||
| 439 | m_initialized = true; // Initialize only once. | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | /// <summary> | ||
| 444 | /// Saves web service settings to the config file if the <see cref="Adapter.PersistSettings"/> property is set to true. | ||
| 445 | /// </summary> | ||
| 446 | public override void SaveSettings() | ||
| 447 | { | ||
| 448 | base.SaveSettings(); | ||
| 449 | |||
| 450 | if (PersistSettings) | ||
| 451 | { | ||
| 452 | // Save settings under the specified category. | ||
| 453 | ConfigurationFile config = ConfigurationFile.Current; | ||
| 454 | CategorizedSettingsElementCollection settings = config.Settings[SettingsCategory]; | ||
| 455 | |||
| 456 | settings["Endpoints", true].Update(m_endpoints); | ||
| 457 | settings["Contract", true].Update(m_contract); | ||
| 458 | settings["Singleton", true].Update(m_singleton); | ||
| 459 | settings["SecurityPolicy", true].Update(m_securityPolicy); | ||
| 460 | settings["PublishMetadata", true].Update(m_publishMetadata); | ||
| 461 | settings["AllowCrossDomainAccess", true].Update(m_allowCrossDomainAccess); | ||
| 462 | settings["AllowedDomainList", true].Update(m_allowedDomainList); | ||
| 463 | |||
| 464 | config.Save(); | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | /// <summary> | ||
| 469 | /// Loads saved web service settings from the config file if the <see cref="Adapter.PersistSettings"/> property is set to true. | ||
| 470 | /// </summary> | ||
| 471 | public override void LoadSettings() | ||
| 472 | { | ||
| 473 | base.LoadSettings(); | ||
| 474 | |||
| 475 | if (PersistSettings) | ||
| 476 | { | ||
| 477 | // Load settings from the specified category. | ||
| 478 | ConfigurationFile config = ConfigurationFile.Current; | ||
| 479 | CategorizedSettingsElementCollection settings = config.Settings[SettingsCategory]; | ||
| 480 | |||
| 481 | settings.Add("Endpoints", m_endpoints, "Semicolon delimited list of URIs where the web service can be accessed."); | ||
| 482 | settings.Add("Contract", m_contract, "Assembly qualified name of the contract interface implemented by the web service."); | ||
| 483 | settings.Add("Singleton", m_singleton, "True if the web service is singleton; otherwise False."); | ||
| 484 | settings.Add("SecurityPolicy", m_securityPolicy, "Assembly qualified name of the authorization policy to be used for securing the web service."); | ||
| 485 | settings.Add("PublishMetadata", m_publishMetadata, "True if the web service metadata is to be published at all the endpoints; otherwise False."); | ||
| 486 | settings.Add("AllowCrossDomainAccess", m_allowCrossDomainAccess, "True to allow Silverlight and Flash cross-domain access to the web service."); | ||
| 487 | settings.Add("AllowedDomainList", m_allowedDomainList, "Comma separated list of domain names for Silverlight and Flash cross-domain access to use when allowCrossDomainAccess is true. Use * for domain wildcards, e.g., *.consoto.com."); | ||
| 488 | |||
| 489 | Endpoints = settings["Endpoints"].ValueAs(m_endpoints); | ||
| 490 | Contract = settings["Contract"].ValueAs(m_contract); | ||
| 491 | Singleton = settings["Singleton"].ValueAs(m_singleton); | ||
| 492 | SecurityPolicy = settings["SecurityPolicy"].ValueAs(m_securityPolicy); | ||
| 493 | PublishMetadata = settings["PublishMetadata"].ValueAs(m_publishMetadata); | ||
| 494 | AllowCrossDomainAccess = settings["AllowCrossDomainAccess"].ValueAs(m_allowCrossDomainAccess); | ||
| 495 | AllowedDomainList = settings["AllowedDomainList"].ValueAs(m_allowedDomainList); | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | /// <summary> | ||
| 500 | /// Get an unused port number. | ||
| 501 | /// </summary> | ||
| 502 | /// <returns>An <see cref="Int32"/> value.</returns> | ||
| 503 | protected virtual int GetUnusedPort() | ||
| 504 | { | ||
| 505 | int randomPort = GSF.Security.Cryptography.Random.Int32Between(1024, 65535); | ||
| 506 | IPEndPoint[] tcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners(); | ||
| 507 | while (true) | ||
| 508 | { | ||
| 509 | if (tcpListeners.FirstOrDefault(ep => ep.Port == randomPort) != null) | ||
| 510 | // Port in use - pick another one. | ||
| 511 | randomPort++; | ||
| 512 | else | ||
| 513 | // Port is not in use - use this. | ||
| 514 | return randomPort; | ||
| 515 | } | ||
| 516 | } | ||
| 517 | |||
| 518 | /// <summary> | ||
| 519 | /// Gets an address where the <see cref="ServiceHost"/> will host the service. | ||
| 520 | /// </summary> | ||
| 521 | /// <returns>A <see cref="String"/> value.</returns> | ||
| 522 | protected virtual string GetServiceAddress() | ||
| 523 | { | ||
| 524 | // Regex matches: | ||
| 525 | // [http | ftp][.<protocol>]://localhost[:<port>][/<path>] | ||
| 526 | string regex = @"(\w+\.*\w*\://)(?<host>\w+\:*\d*)"; | ||
| 527 | |||
| 528 | // Look through all of the specified endpoints. | ||
| 529 | foreach (string endpoint in m_endpoints.Split(';')) | ||
| 530 | { | ||
| 531 | // Use endpoint host information including port number for service address. | ||
| 532 | Match match = Regex.Match(endpoint, regex, RegexOptions.IgnoreCase); | ||
| 533 | if (match.Success && match.Groups["host"].Success) | ||
| 534 | return string.Format("http://{0}", match.Groups["host"]); | ||
| 535 | } | ||
| 536 | |||
| 537 | // Return an empty string if endpoints are not in a valid format. | ||
| 538 | return string.Empty; | ||
| 539 | } | ||
| 540 | |||
| 541 | /// <summary> | ||
| 542 | /// Initializes the <see cref="ServiceHost"/>. | ||
| 543 | /// </summary> | ||
| 544 | protected virtual void InitializeServiceHost() | ||
| 545 | { | ||
| 546 | if (!string.IsNullOrEmpty(m_endpoints)) | ||
| 547 | { | ||
| 548 | // Initialize service host. | ||
| 549 | string serviceUri = GetServiceAddress(); | ||
| 550 | |||
| 551 | if (m_singleton) | ||
| 552 | m_serviceHost = new ServiceHost(this, new Uri(serviceUri)); | ||
| 553 | else | ||
| 554 | m_serviceHost = new ServiceHost(this.GetType(), new Uri(serviceUri)); | ||
| 555 | |||
| 556 | // Enable metadata publishing. | ||
| 557 | if (m_publishMetadata) | ||
| 558 | { | ||
| 559 | ServiceMetadataBehavior serviceBehavior = m_serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>(); | ||
| 560 | |||
| 561 | if (serviceBehavior == null) | ||
| 562 | { | ||
| 563 | serviceBehavior = new ServiceMetadataBehavior(); | ||
| 564 | m_serviceHost.Description.Behaviors.Add(serviceBehavior); | ||
| 565 | } | ||
| 566 | |||
| 567 | serviceBehavior.HttpGetEnabled = true; | ||
| 568 | } | ||
| 569 | |||
| 570 | // Enable security on the service. | ||
| 571 | if (!string.IsNullOrEmpty(m_securityPolicy)) | ||
| 572 | { | ||
| 573 | ServiceAuthorizationBehavior serviceBehavior = m_serviceHost.Description.Behaviors.Find<ServiceAuthorizationBehavior>(); | ||
| 574 | |||
| 575 | if (serviceBehavior == null) | ||
| 576 | { | ||
| 577 | serviceBehavior = new ServiceAuthorizationBehavior(); | ||
| 578 | m_serviceHost.Description.Behaviors.Add(serviceBehavior); | ||
| 579 | } | ||
| 580 | |||
| 581 | serviceBehavior.PrincipalPermissionMode = PrincipalPermissionMode.Custom; | ||
| 582 | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(); | ||
| 583 | policies.Add((IAuthorizationPolicy)Activator.CreateInstance(System.Type.GetType(m_securityPolicy))); | ||
| 584 | serviceBehavior.ExternalAuthorizationPolicies = policies.AsReadOnly(); | ||
| 585 | } | ||
| 586 | |||
| 587 | // Add specified service endpoints. | ||
| 588 | string serviceAddress; | ||
| 589 | string[] serviceAddresses; | ||
| 590 | Binding serviceBinding; | ||
| 591 | ServiceEndpoint serviceEndpoint; | ||
| 592 | serviceAddresses = m_endpoints.Split(';'); | ||
| 593 | |||
| 594 | for (int i = 0; i < serviceAddresses.Length; i++) | ||
| 595 | { | ||
| 596 | serviceAddress = serviceAddresses[i].Trim(); | ||
| 597 | serviceBinding = Service.CreateServiceBinding(ref serviceAddress, !string.IsNullOrEmpty(m_securityPolicy)); | ||
| 598 | if (serviceBinding != null) | ||
| 599 | { | ||
| 600 | serviceEndpoint = m_serviceHost.AddServiceEndpoint(System.Type.GetType(m_contract), serviceBinding, serviceAddress); | ||
| 601 | |||
| 602 | if (serviceBinding is WebHttpBinding) | ||
| 603 | { | ||
| 604 | // Special handling for REST endpoint. | ||
| 605 | WebHttpBehavior restBehavior = new WebHttpBehavior(); | ||
| 606 | |||
| 607 | #if !MONO | ||
| 608 | if (m_publishMetadata) | ||
| 609 | restBehavior.HelpEnabled = true; | ||
| 610 | #endif | ||
| 611 | |||
| 612 | serviceEndpoint.Behaviors.Add(restBehavior); | ||
| 613 | } | ||
| 614 | else if (m_publishMetadata) | ||
| 615 | { | ||
| 616 | // Add endpoint for service metadata. | ||
| 617 | if (serviceAddress.StartsWith("http://")) | ||
| 618 | m_serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), serviceAddress + "/mex"); | ||
| 619 | else if (serviceAddress.StartsWith("net.tcp://")) | ||
| 620 | m_serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(), serviceAddress + "/mex"); | ||
| 621 | else if (serviceAddress.StartsWith("net.pipe://")) | ||
| 622 | m_serviceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexNamedPipeBinding(), serviceAddress + "/mex"); | ||
| 623 | } | ||
| 624 | } | ||
| 625 | } | ||
| 626 | |||
| 627 | // Enable cross domain access. | ||
| 628 | if (m_allowCrossDomainAccess) | ||
| 629 | m_serviceHost.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); | ||
| 630 | |||
| 631 | // Allow for customization. | ||
| 632 | OnServiceHostCreated(); | ||
| 633 | |||
| 634 | // Start the service. | ||
| 635 | m_serviceHost.Open(); | ||
| 636 | OnServiceHostStarted(); | ||
| 637 | } | ||
| 638 | } | ||
| 639 | |||
| 640 | // Converts a string of data to a UTF8 stream using "application/xml" as the content type | ||
| 641 | private Stream StringToStream(string result) | ||
| 642 | { | ||
| 643 | WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml"; | ||
| 644 | return new MemoryStream(Encoding.UTF8.GetBytes(result)); | ||
| 645 | } | ||
| 646 | |||
| 647 | /// <summary> | ||
| 648 | /// Gets policy stream for Silverlight applications. | ||
| 649 | /// </summary> | ||
| 650 | /// <returns>Stream containing clientaccesspolicy.xml.</returns> | ||
| 651 | public Stream GetSilverlightPolicy() | ||
| 652 | { | ||
| 653 | if (m_allowCrossDomainAccess) | ||
| 654 | { | ||
| 655 | const string result = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + | ||
| 656 | "<access-policy>\r\n" + | ||
| 657 | " <cross-domain-access>\r\n" + | ||
| 658 | " <policy>\r\n" + | ||
| 659 | " <allow-from http-request-headers=\"*\">\r\n" + | ||
| 660 | " <domain uri=\"{0}\"/>\r\n" + | ||
| 661 | " </allow-from>\r\n" + | ||
| 662 | " <grant-to>\r\n" + | ||
| 663 | " <resource path=\"/\" include-subpaths=\"true\"/>\r\n" + | ||
| 664 | " </grant-to>\r\n" + | ||
| 665 | " </policy>\r\n" + | ||
| 666 | " </cross-domain-access>\r\n" + | ||
| 667 | "</access-policy>"; | ||
| 668 | |||
| 669 | return StringToStream(string.Format(result, m_allowedDomainList)); | ||
| 670 | } | ||
| 671 | |||
| 672 | return StringToStream(""); | ||
| 673 | } | ||
| 674 | |||
| 675 | /// <summary> | ||
| 676 | /// Gets policy stream for Flash applications. | ||
| 677 | /// </summary> | ||
| 678 | /// <returns>Stream containing crossdomain.xml.</returns> | ||
| 679 | public Stream GetFlashPolicy() | ||
| 680 | { | ||
| 681 | if (m_allowCrossDomainAccess) | ||
| 682 | { | ||
| 683 | const string result = "<?xml version=\"1.0\"?>\r\n" + | ||
| 684 | "<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">\r\n" + | ||
| 685 | "<cross-domain-policy>\r\n{0}</cross-domain-policy>"; | ||
| 686 | |||
| 687 | const string allowAccessTag = " <allow-access-from domain=\"{0}\" to-ports=\"*\" />\r\n"; | ||
| 688 | |||
| 689 | StringBuilder allowedDomains = new StringBuilder(); | ||
| 690 | string[] domains = m_allowedDomainList.ToNonNullString("*").Split(','); | ||
| 691 | |||
| 692 | foreach (string domain in domains) | ||
| 693 | { | ||
| 694 | allowedDomains.AppendFormat(allowAccessTag, domain); | ||
| 695 | } | ||
| 696 | |||
| 697 | return StringToStream(string.Format(result, allowedDomains)); | ||
| 698 | } | ||
| 699 | |||
| 700 | return StringToStream(""); | ||
| 701 | } | ||
| 702 | |||
| 703 | /// <summary> | ||
| 704 | /// Releases the unmanaged resources used by the web service and optionally releases the managed resources. | ||
| 705 | /// </summary> | ||
| 706 | /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param> | ||
| 707 | protected override void Dispose(bool disposing) | ||
| 708 | { | ||
| 709 | if (!m_disposed) | ||
| 710 | { | ||
| 711 | try | ||
| 712 | { | ||
| 713 | // This will be done regardless of whether the object is finalized or disposed. | ||
| 714 | if (disposing) | ||
| 715 | { | ||
| 716 | // This will be done only when the object is disposed by calling Dispose(). | ||
| 717 | if (m_serviceHost != null) | ||
| 718 | m_serviceHost.Close(); | ||
| 719 | } | ||
| 720 | } | ||
| 721 | finally | ||
| 722 | { | ||
| 723 | m_disposed = true; // Prevent duplicate dispose. | ||
| 724 | base.Dispose(disposing); // Call base class Dispose(). | ||
| 725 | } | ||
| 726 | } | ||
| 727 | } | ||
| 728 | |||
| 729 | /// <summary> | ||
| 730 | /// Raises the <see cref="ServiceHostCreated"/> event. | ||
| 731 | /// </summary> | ||
| 732 | protected virtual void OnServiceHostCreated() | ||
| 733 | { | ||
| 734 | if (ServiceHostCreated != null) | ||
| 735 | ServiceHostCreated(this, EventArgs.Empty); | ||
| 736 | } | ||
| 737 | |||
| 738 | /// <summary> | ||
| 739 | /// Raises the <see cref="ServiceHostStarted"/> event. | ||
| 740 | /// </summary> | ||
| 741 | protected virtual void OnServiceHostStarted() | ||
| 742 | { | ||
| 743 | if (ServiceHostStarted != null) | ||
| 744 | ServiceHostStarted(this, EventArgs.Empty); | ||
| 745 | } | ||
| 746 | |||
| 747 | /// <summary> | ||
| 748 | /// Raises the <see cref="ServiceProcessException"/> event. | ||
| 749 | /// </summary> | ||
| 750 | /// <param name="exception"><see cref="Exception"/> to sent to <see cref="ServiceProcessException"/> event.</param> | ||
| 751 | protected virtual void OnServiceProcessException(Exception exception) | ||
| 752 | { | ||
| 753 | if (ServiceProcessException != null) | ||
| 754 | ServiceProcessException(this, new EventArgs<Exception>(exception)); | ||
| 755 | } | ||
| 756 | |||
| 757 | #endregion | ||
| 758 | } | ||
| 759 | } | ||
| @@ -0,0 +1,300 @@ | |||
| 1 | //****************************************************************************************************** | ||
| 2 | // Service.cs - Gbtc | ||
| 3 | // | ||
| 4 | // Copyright © 2010, Grid Protection Alliance. All Rights Reserved. | ||
| 5 | // | ||
| 6 | // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See | ||
| 7 | // the NOTICE file distributed with this work for additional information regarding copyright ownership. | ||
| 8 | // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may | ||
| 9 | // not use this file except in compliance with the License. You may obtain a copy of the License at: | ||
| 10 | // | ||
| 11 | // http://www.opensource.org/licenses/eclipse-1.0.php | ||
| 12 | // | ||
| 13 | // Unless agreed to in writing, the subject software distributed under the License is distributed on an | ||
| 14 | // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the | ||
| 15 | // License for the specific language governing permissions and limitations. | ||
| 16 | // | ||
| 17 | // Code Modification History: | ||
| 18 | // ---------------------------------------------------------------------------------------------------- | ||
| 19 | // 11/15/2011 - Pinal C. Patel | ||
| 20 | // Generated original version of source code. | ||
| 21 | // 10/8/2012 - Danyelle Gilliam | ||
| 22 | // Modified Header | ||
| 23 | // | ||
| 24 | //****************************************************************************************************** | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | using System; | ||
| 29 | using System.Net; | ||
| 30 | using System.Reflection; | ||
| 31 | using System.ServiceModel; | ||
| 32 | using System.ServiceModel.Channels; | ||
| 33 | |||
| 34 | namespace GSF.ServiceModel | ||
| 35 | { | ||
| 36 | /// <summary> | ||
| 37 | /// Defines helper methods related to WCF-based web services. | ||
| 38 | /// </summary> | ||
| 39 | public static class Service | ||
| 40 | { | ||
| 41 | private const string HostedAspNetEnvironment = "System.ServiceModel.Activation.HostedAspNetEnvironment, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"; | ||
| 42 | |||
| 43 | /// <summary> | ||
| 44 | /// Creates a <see cref="Binding"/> based on the specified <paramref name="address"/>. | ||
| 45 | /// </summary> | ||
| 46 | /// <param name="address">The URI that is used to determine the type of <see cref="Binding"/> to be created.</param> | ||
| 47 | /// <param name="enableSecurity">A boolean value that indicated whether security is to be enabled on the <see cref="Binding"/>.</param> | ||
| 48 | /// <returns>An <see cref="Binding"/> object if a valid <paramref name="address"/> is specified; otherwise null.</returns> | ||
| 49 | /// <remarks> | ||
| 50 | /// This list shows all valid address schemes that can be specified in the <paramref name="address"/>: | ||
| 51 | /// <list type="table"> | ||
| 52 | /// <listheader> | ||
| 53 | /// <term>Address Scheme</term> | ||
| 54 | /// <description>Usage</description> | ||
| 55 | /// </listheader> | ||
| 56 | /// <item> | ||
| 57 | /// <term><b>http://</b> or <b>http.soap11://</b></term> | ||
| 58 | /// <description>An <paramref name="address"/> of <b>http.soap11://localhost:2929</b> will create an <see cref="BasicHttpBinding"/> and update the <paramref name="address"/> to <b>http://localhost:2929</b>.</description> | ||
| 59 | /// </item> | ||
| 60 | /// <item> | ||
| 61 | /// <term><b>http.soap12://</b></term> | ||
| 62 | /// <description>An <paramref name="address"/> of <b>http.soap12://localhost:2929</b> will create an <see cref="WSHttpBinding"/> and update the <paramref name="address"/> to <b>http://localhost:2929</b>.</description> | ||
| 63 | /// </item> | ||
| 64 | /// <item> | ||
| 65 | /// <term><b>http.duplex://</b></term> | ||
| 66 | /// <description>An <paramref name="address"/> of <b>http.duplex://localhost:2929</b> will create an <see cref="WSDualHttpBinding"/> and update the <paramref name="address"/> to <b>http://localhost:2929</b>.</description> | ||
| 67 | /// </item> | ||
| 68 | /// <item> | ||
| 69 | /// <term><b>http.rest://</b></term> | ||
| 70 | /// <description>An <paramref name="address"/> of <b>http.rest://localhost:2929</b> will create an <see cref="WebHttpBinding"/> and update the <paramref name="address"/> to <b>http://localhost:2929</b>.</description> | ||
| 71 | /// </item> | ||
| 72 | /// <item> | ||
| 73 | /// <term><b>net.tcp://</b></term> | ||
| 74 | /// <description>An <paramref name="address"/> of <b>net.tcp://localhost:2929</b> will create an <see cref="NetTcpBinding"/> and leave the <paramref name="address"/> unchanged.</description> | ||
| 75 | /// </item> | ||
| 76 | /// <item> | ||
| 77 | /// <term><b>net.p2p://</b></term> | ||
| 78 | /// <description>An <paramref name="address"/> of <b>net.p2p://localhost:2929</b> will create an <see cref="NetPeerTcpBinding"/> and leave the <paramref name="address"/> unchanged.</description> | ||
| 79 | /// </item> | ||
| 80 | /// <item> | ||
| 81 | /// <term><b>net.pipe://</b></term> | ||
| 82 | /// <description>An <paramref name="address"/> of <b>net.pipe://localhost:2929</b> will create an <see cref="NetNamedPipeBinding"/> and leave the <paramref name="address"/> unchanged.</description> | ||
| 83 | /// </item> | ||
| 84 | /// <item> | ||
| 85 | /// <term><b>net.msmq://</b></term> | ||
| 86 | /// <description>An <paramref name="address"/> of <b>net.msmq://localhost:2929</b> will create an <see cref="NetMsmqBinding"/> and leave the <paramref name="address"/> unchanged.</description> | ||
| 87 | /// </item> | ||
| 88 | /// </list> | ||
| 89 | /// <para> | ||
| 90 | /// The <paramref name="enableSecurity"/> parameter value is ignored Mono deployments since security bindings are not implemented. | ||
| 91 | /// </para> | ||
| 92 | /// </remarks> | ||
| 93 | public static Binding CreateServiceBinding(ref string address, bool enableSecurity) | ||
| 94 | { | ||
| 95 | address = address.Trim(); | ||
| 96 | if (string.IsNullOrEmpty(address)) | ||
| 97 | return null; | ||
| 98 | |||
| 99 | int index = address.IndexOf("://"); | ||
| 100 | string scheme = address.Substring(0, index >= 0 ? index : address.Length); | ||
| 101 | switch (scheme.ToLower()) | ||
| 102 | { | ||
| 103 | case "http": | ||
| 104 | case "http.soap11": | ||
| 105 | // Format address. | ||
| 106 | address = address.Replace("http.soap11", "http"); | ||
| 107 | // Create binding. | ||
| 108 | BasicHttpBinding soap11Binding = new BasicHttpBinding(); | ||
| 109 | |||
| 110 | #if !MONO | ||
| 111 | if (enableSecurity) | ||
| 112 | { | ||
| 113 | // Enable security. | ||
| 114 | soap11Binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; | ||
| 115 | soap11Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; | ||
| 116 | } | ||
| 117 | else | ||
| 118 | { | ||
| 119 | // Disable security. | ||
| 120 | soap11Binding.Security.Mode = BasicHttpSecurityMode.None; | ||
| 121 | soap11Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; | ||
| 122 | } | ||
| 123 | #endif | ||
| 124 | |||
| 125 | return soap11Binding; | ||
| 126 | case "http.soap12": | ||
| 127 | // Format address. | ||
| 128 | address = address.Replace("http.soap12", "http"); | ||
| 129 | // Create binding. | ||
| 130 | WSHttpBinding soap12Binding = new WSHttpBinding(); | ||
| 131 | |||
| 132 | #if !MONO | ||
| 133 | if (enableSecurity) | ||
| 134 | { | ||
| 135 | // Enable security. | ||
| 136 | soap12Binding.Security.Mode = SecurityMode.Transport; | ||
| 137 | soap12Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; | ||
| 138 | } | ||
| 139 | else | ||
| 140 | { | ||
| 141 | // Disable security. | ||
| 142 | soap12Binding.Security.Mode = SecurityMode.None; | ||
| 143 | soap12Binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; | ||
| 144 | } | ||
| 145 | #endif | ||
| 146 | |||
| 147 | return soap12Binding; | ||
| 148 | case "http.duplex": | ||
| 149 | // Format address. | ||
| 150 | address = address.Replace("http.duplex", "http"); | ||
| 151 | // Create binding. | ||
| 152 | WSDualHttpBinding duplexBinding = new WSDualHttpBinding(); | ||
| 153 | |||
| 154 | #if !MONO | ||
| 155 | if (enableSecurity) | ||
| 156 | { | ||
| 157 | // Enable security. | ||
| 158 | duplexBinding.Security.Mode = WSDualHttpSecurityMode.Message; | ||
| 159 | } | ||
| 160 | else | ||
| 161 | { | ||
| 162 | // Disable security. | ||
| 163 | duplexBinding.Security.Mode = WSDualHttpSecurityMode.None; | ||
| 164 | } | ||
| 165 | #endif | ||
| 166 | |||
| 167 | return duplexBinding; | ||
| 168 | case "http.rest": | ||
| 169 | // Format address. | ||
| 170 | address = address.Replace("http.rest", "http"); | ||
| 171 | // Create binding. | ||
| 172 | WebHttpBinding restBinding = new WebHttpBinding(); | ||
| 173 | |||
| 174 | #if !MONO | ||
| 175 | if (enableSecurity) | ||
| 176 | { | ||
| 177 | // Enable security. | ||
| 178 | restBinding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly; | ||
| 179 | restBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; | ||
| 180 | } | ||
| 181 | else | ||
| 182 | { | ||
| 183 | // Disable security. | ||
| 184 | restBinding.Security.Mode = WebHttpSecurityMode.None; | ||
| 185 | restBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; | ||
| 186 | } | ||
| 187 | #endif | ||
| 188 | |||
| 189 | return restBinding; | ||
| 190 | case "net.tcp": | ||
| 191 | // Create binding. | ||
| 192 | NetTcpBinding tcpBinding = new NetTcpBinding(); | ||
| 193 | |||
| 194 | #if !MONO | ||
| 195 | if (enableSecurity) | ||
| 196 | { | ||
| 197 | // Enable security. | ||
| 198 | tcpBinding.Security.Mode = SecurityMode.Transport; | ||
| 199 | tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows; | ||
| 200 | } | ||
| 201 | else | ||
| 202 | { | ||
| 203 | // Disable sercurity. | ||
| 204 | tcpBinding.Security.Mode = SecurityMode.None; | ||
| 205 | tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.None; | ||
| 206 | } | ||
| 207 | #endif | ||
| 208 | |||
| 209 | return tcpBinding; | ||
| 210 | case "net.p2p": | ||
| 211 | // Create binding. | ||
| 212 | NetPeerTcpBinding p2pBinding = new NetPeerTcpBinding(); | ||
| 213 | |||
| 214 | #if !MONO | ||
| 215 | if (enableSecurity) | ||
| 216 | { | ||
| 217 | // Enable security. | ||
| 218 | p2pBinding.Security.Mode = SecurityMode.Transport; | ||
| 219 | } | ||
| 220 | else | ||
| 221 | { | ||
| 222 | // Disable security. | ||
| 223 | p2pBinding.Security.Mode = SecurityMode.None; | ||
| 224 | } | ||
| 225 | #endif | ||
| 226 | |||
| 227 | return p2pBinding; | ||
| 228 | case "net.pipe": | ||
| 229 | // Create binding. | ||
| 230 | NetNamedPipeBinding pipeBinding = new NetNamedPipeBinding(); | ||
| 231 | |||
| 232 | #if !MONO | ||
| 233 | if (enableSecurity) | ||
| 234 | { | ||
| 235 | // Enable security. | ||
| 236 | pipeBinding.Security.Mode = NetNamedPipeSecurityMode.Transport; | ||
| 237 | } | ||
| 238 | else | ||
| 239 | { | ||
| 240 | // Disable security. | ||
| 241 | pipeBinding.Security.Mode = NetNamedPipeSecurityMode.None; | ||
| 242 | } | ||
| 243 | #endif | ||
| 244 | |||
| 245 | return pipeBinding; | ||
| 246 | case "net.msmq": | ||
| 247 | // Create binding. | ||
| 248 | NetMsmqBinding msmqBinding = new NetMsmqBinding(); | ||
| 249 | |||
| 250 | #if !MONO | ||
| 251 | if (enableSecurity) | ||
| 252 | { | ||
| 253 | // Enable security. | ||
| 254 | msmqBinding.Security.Mode = NetMsmqSecurityMode.Transport; | ||
| 255 | } | ||
| 256 | else | ||
| 257 | { | ||
| 258 | // Disable security. | ||
| 259 | msmqBinding.Security.Mode = NetMsmqSecurityMode.None; | ||
| 260 | } | ||
| 261 | #endif | ||
| 262 | |||
| 263 | return msmqBinding; | ||
| 264 | default: | ||
| 265 | return null; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | /// <summary> | ||
| 270 | /// Gets the security setting of the hosting environment (For example: IIS web site or virtual directory). | ||
| 271 | /// </summary> | ||
| 272 | public static AuthenticationSchemes GetAuthenticationSchemes(Uri baseAddress) | ||
| 273 | { | ||
| 274 | Type type = Type.GetType(HostedAspNetEnvironment); | ||
| 275 | object instance = Activator.CreateInstance(type, true); | ||
| 276 | |||
| 277 | return (AuthenticationSchemes)instance.GetType().InvokeMember("GetAuthenticationSchemes", BindingFlags.InvokeMethod, null, instance, new object[] { baseAddress }); | ||
| 278 | } | ||
| 279 | |||
| 280 | /// <summary> | ||
| 281 | /// Gets the contract that the service implements. | ||
| 282 | /// </summary> | ||
| 283 | /// <param name="serviceType"><see cref="Type"/> of the service.</param> | ||
| 284 | /// <returns><see cref="Type"/> of the service contract if found, otherwise null.</returns> | ||
| 285 | public static Type GetServiceContract(Type serviceType) | ||
| 286 | { | ||
| 287 | // Find contract interface by service type name. | ||
| 288 | Type contract = serviceType.GetInterface("I" + serviceType.Name); | ||
| 289 | if (contract == null) | ||
| 290 | { | ||
| 291 | // Fall back to the first interface the service implements. | ||
| 292 | Type[] interfaces = serviceType.GetInterfaces(); | ||
| 293 | if (interfaces.Length > 0) | ||
| 294 | contract = interfaces[0]; | ||
| 295 | } | ||
| 296 | |||
| 297 | return contract; | ||
| 298 | } | ||
| 299 | } | ||
| 300 | } | ||
| @@ -0,0 +1,34 @@ | |||
| 1 | <?xml version="1.0" encoding="utf-8"?> | ||
| 2 | |||
| 3 | <!-- | ||
| 4 | ******************************************************************************************************* | ||
| 5 | ** TVA.ServiceModel.nuspec - Gbtc | ||
| 6 | ** | ||
| 7 | ** Tennessee Valley Authority, 2009 | ||
| 8 | ** No copyright is claimed pursuant to 17 USC § 105. All Other Rights Reserved. | ||
| 9 | ** | ||
| 10 | ** This software is made freely available under the TVA Open Source Agreement (see below). | ||
| 11 | ** | ||
| 12 | ** Code Modification History: | ||
| 13 | ** =================================================================================================== | ||
| 14 | ** 05/31/2011 - Pinal C. Patel | ||
| 15 | ** Generated original version of source code. | ||
| 16 | ** | ||
| 17 | ******************************************************************************************************* | ||
| 18 | --> | ||
| 19 | |||
| 20 | <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> | ||
| 21 | <metadata> | ||
| 22 | <id>TVACodeLibrary.Libraries.ServiceModel</id> | ||
| 23 | <version>$version$</version> | ||
| 24 | <authors>$author$</authors> | ||
| 25 | <description>$description$</description> | ||
| 26 | <projectUrl>http://tvacodelibrary.codeplex.com/</projectUrl> | ||
| 27 | <licenseUrl>http://tvacodelibrary.codeplex.com/license</licenseUrl> | ||
| 28 | <iconUrl>http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=tvacodelibrary&DownloadId=140265&Build=17672</iconUrl> | ||
| 29 | <dependencies> | ||
| 30 | <dependency id="TVACodeLibrary.Libraries.Core" version="" /> | ||
| 31 | <dependency id="TVACodeLibrary.Libraries.Security" version="" /> | ||
| 32 | </dependencies> | ||
| 33 | </metadata> | ||
| 34 | </package> | ||