2 // ServiceCredentialsElement.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.ObjectModel;
33 using System.ComponentModel;
34 using System.Configuration;
36 using System.Net.Security;
37 using System.Reflection;
38 using System.Security.Cryptography.X509Certificates;
39 using System.Security.Principal;
40 using System.IdentityModel.Claims;
41 using System.IdentityModel.Policy;
42 using System.IdentityModel.Selectors;
43 using System.IdentityModel.Tokens;
44 using System.ServiceModel;
45 using System.ServiceModel.Channels;
46 using System.ServiceModel.Description;
47 using System.ServiceModel.Diagnostics;
48 using System.ServiceModel.Dispatcher;
49 using System.ServiceModel.MsmqIntegration;
50 using System.ServiceModel.PeerResolvers;
51 using System.ServiceModel.Security;
52 using System.Runtime.Serialization;
54 using System.Web.Security;
57 namespace System.ServiceModel.Configuration
59 public sealed class ServiceCredentialsElement
60 : BehaviorExtensionElement
62 public ServiceCredentialsElement () {
68 public override Type BehaviorType {
69 get { return typeof (ServiceCredentials); }
72 [ConfigurationProperty ("clientCertificate",
73 Options = ConfigurationPropertyOptions.None)]
74 public X509InitiatorCertificateServiceElement ClientCertificate {
75 get { return (X509InitiatorCertificateServiceElement) base ["clientCertificate"]; }
78 [ConfigurationProperty ("issuedTokenAuthentication",
79 Options = ConfigurationPropertyOptions.None)]
80 public IssuedTokenServiceElement IssuedTokenAuthentication {
81 get { return (IssuedTokenServiceElement) base ["issuedTokenAuthentication"]; }
84 [ConfigurationProperty ("peer",
85 Options = ConfigurationPropertyOptions.None)]
86 public PeerCredentialElement Peer {
87 get { return (PeerCredentialElement) base ["peer"]; }
90 protected override ConfigurationPropertyCollection Properties {
91 get { return base.Properties; }
94 [ConfigurationProperty ("secureConversationAuthentication",
95 Options = ConfigurationPropertyOptions.None)]
96 public SecureConversationServiceElement SecureConversationAuthentication {
97 get { return (SecureConversationServiceElement) base ["secureConversationAuthentication"]; }
100 [ConfigurationProperty ("serviceCertificate",
101 Options = ConfigurationPropertyOptions.None)]
102 public X509RecipientCertificateServiceElement ServiceCertificate {
103 get { return (X509RecipientCertificateServiceElement) base ["serviceCertificate"]; }
106 [StringValidator (MinLength = 0,
107 MaxLength = int.MaxValue,
108 InvalidCharacters = null)]
109 [ConfigurationProperty ("type",
110 Options = ConfigurationPropertyOptions.None,
113 get { return (string) base ["type"]; }
114 set { base ["type"] = value; }
117 [ConfigurationProperty ("userNameAuthentication",
118 Options = ConfigurationPropertyOptions.None)]
119 public UserNameServiceElement UserNameAuthentication {
120 get { return (UserNameServiceElement) base ["userNameAuthentication"]; }
123 [ConfigurationProperty ("windowsAuthentication",
124 Options = ConfigurationPropertyOptions.None)]
125 public WindowsServiceElement WindowsAuthentication {
126 get { return (WindowsServiceElement) base ["windowsAuthentication"]; }
130 protected internal override object CreateBehavior ()
132 var sb = new ServiceCredentials ();
135 foreach (AllowedAudienceUriElement ae in IssuedTokenAuthentication.AllowedAudienceUris)
136 sb.IssuedTokenAuthentication.AllowedAudienceUris.Add (ae.AllowedAudienceUri);
137 sb.IssuedTokenAuthentication.AllowUntrustedRsaIssuers = IssuedTokenAuthentication.AllowUntrustedRsaIssuers;
138 sb.IssuedTokenAuthentication.AudienceUriMode = IssuedTokenAuthentication.AudienceUriMode;
140 if (!String.IsNullOrEmpty (IssuedTokenAuthentication.CustomCertificateValidatorType))
141 sb.IssuedTokenAuthentication.CustomCertificateValidator = (X509CertificateValidator) CreateInstance (IssuedTokenAuthentication.CustomCertificateValidatorType);
142 sb.IssuedTokenAuthentication.CertificateValidationMode = IssuedTokenAuthentication.CertificateValidationMode;
143 sb.IssuedTokenAuthentication.RevocationMode = IssuedTokenAuthentication.RevocationMode;
144 sb.IssuedTokenAuthentication.TrustedStoreLocation = IssuedTokenAuthentication.TrustedStoreLocation;
145 foreach (X509CertificateTrustedIssuerElement ce in IssuedTokenAuthentication.KnownCertificates)
146 sb.IssuedTokenAuthentication.KnownCertificates.Add (GetCertificate (ce.StoreLocation, ce.StoreName, ce.X509FindType, ce.FindValue));
148 sb.IssuedTokenAuthentication.SamlSerializer = (SamlSerializer) CreateInstance (IssuedTokenAuthentication.SamlSerializerType);
152 if (!String.IsNullOrEmpty (Peer.Certificate.FindValue))
153 sb.Peer.SetCertificate (Peer.Certificate.StoreLocation, Peer.Certificate.StoreName, Peer.Certificate.X509FindType, Peer.Certificate.FindValue);
154 // sb.Peer.MeshPassword = /* cannot fill it here */
155 sb.Peer.MessageSenderAuthentication.CustomCertificateValidator = (X509CertificateValidator) CreateInstance (Peer.MessageSenderAuthentication.CustomCertificateValidatorType);
156 sb.Peer.MessageSenderAuthentication.CertificateValidationMode = Peer.MessageSenderAuthentication.CertificateValidationMode;
157 sb.Peer.MessageSenderAuthentication.RevocationMode = Peer.MessageSenderAuthentication.RevocationMode;
158 sb.Peer.MessageSenderAuthentication.TrustedStoreLocation = Peer.MessageSenderAuthentication.TrustedStoreLocation;
159 sb.Peer.PeerAuthentication.CustomCertificateValidator = (X509CertificateValidator) CreateInstance (Peer.PeerAuthentication.CustomCertificateValidatorType);
160 sb.Peer.PeerAuthentication.CertificateValidationMode = Peer.PeerAuthentication.CertificateValidationMode;
161 sb.Peer.PeerAuthentication.RevocationMode = Peer.PeerAuthentication.RevocationMode;
162 sb.Peer.PeerAuthentication.TrustedStoreLocation = Peer.PeerAuthentication.TrustedStoreLocation;
165 sb.SecureConversationAuthentication.SecurityStateEncoder = (SecurityStateEncoder) CreateInstance (SecureConversationAuthentication.SecurityStateEncoderType);
168 if (!String.IsNullOrEmpty (ServiceCertificate.FindValue))
169 sb.ServiceCertificate.SetCertificate (ServiceCertificate.StoreLocation, ServiceCertificate.StoreName, ServiceCertificate.X509FindType, ServiceCertificate.FindValue);
172 sb.UserNameAuthentication.CachedLogonTokenLifetime = UserNameAuthentication.CachedLogonTokenLifetime;
173 sb.UserNameAuthentication.CacheLogonTokens = UserNameAuthentication.CacheLogonTokens;
174 sb.UserNameAuthentication.CustomUserNamePasswordValidator = (UserNamePasswordValidator) CreateInstance (UserNameAuthentication.CustomUserNamePasswordValidatorType);
175 sb.UserNameAuthentication.IncludeWindowsGroups = UserNameAuthentication.IncludeWindowsGroups;
176 sb.UserNameAuthentication.MaxCachedLogonTokens = UserNameAuthentication.MaxCachedLogonTokens;
177 sb.UserNameAuthentication.MembershipProvider = (MembershipProvider) CreateInstance (UserNameAuthentication.MembershipProviderName);
178 sb.UserNameAuthentication.UserNamePasswordValidationMode = UserNameAuthentication.UserNamePasswordValidationMode;
181 sb.WindowsAuthentication.AllowAnonymousLogons = WindowsAuthentication.AllowAnonymousLogons;
182 sb.WindowsAuthentication.IncludeWindowsGroups = WindowsAuthentication.IncludeWindowsGroups;
187 X509Certificate2 GetCertificate (StoreLocation storeLocation, StoreName storeName, X509FindType findType, object findValue)
189 throw new NotImplementedException ();
192 object CreateInstance (string typeName)
194 return String.IsNullOrEmpty (typeName) ? null : Activator.CreateInstance (System.Type.GetType (typeName, true));