5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 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.
29 using System.Collections.Generic;
30 using System.IdentityModel.Claims;
31 using System.Security.Cryptography;
32 using System.Security.Cryptography.X509Certificates;
36 namespace System.ServiceModel
38 public abstract class EndpointIdentity
41 IEqualityComparer<Claim> comparer;
43 protected EndpointIdentity ()
47 public Claim IdentityClaim {
51 public static EndpointIdentity CreateDnsIdentity (string dnsName)
53 return new DnsEndpointIdentity (dnsName);
56 public static EndpointIdentity CreateIdentity (Claim identity)
59 throw new ArgumentNullException ();
61 if (identity.ClaimType == ClaimTypes.Dns)
62 return CreateDnsIdentity ((string) identity.Resource);
63 else if (identity.ClaimType == ClaimTypes.Rsa) {
64 if (identity.Resource is string)
65 return CreateRsaIdentity ((string) identity.Resource);
66 else if (identity.Resource is X509Certificate2)
67 return CreateRsaIdentity ((X509Certificate2) identity.Resource);
69 else if (identity.ClaimType == ClaimTypes.Thumbprint)
70 return CreateX509CertificateIdentity ((X509Certificate2) identity.Resource);
71 else if (identity.ClaimType == ClaimTypes.Spn)
72 return CreateSpnIdentity ((string) identity.Resource);
73 else if (identity.ClaimType == ClaimTypes.Upn)
74 return CreateSpnIdentity ((string) identity.Resource);
76 throw new NotSupportedException (String.Format ("Claim type '{0}' cannot be used to create an endpoint identity.", identity.ClaimType));
79 public static EndpointIdentity CreateRsaIdentity (string publicKey)
81 return new RsaEndpointIdentity (publicKey);
84 public static EndpointIdentity CreateRsaIdentity (
85 X509Certificate2 certificate)
87 return new RsaEndpointIdentity (certificate);
90 public static EndpointIdentity CreateSpnIdentity (string spnName)
92 return new SpnEndpointIdentity (spnName);
95 public static EndpointIdentity CreateUpnIdentity (string upnName)
97 return new UpnEndpointIdentity (upnName);
100 public static EndpointIdentity CreateX509CertificateIdentity (
101 X509Certificate2 certificate)
103 return new X509CertificateEndpointIdentity (certificate);
106 public static EndpointIdentity CreateX509CertificateIdentity (
107 X509Certificate2 primaryCertificate,
108 X509Certificate2Collection supportingCertificates)
110 return new X509CertificateEndpointIdentity (primaryCertificate, supportingCertificates);
113 public override bool Equals (object other)
115 EndpointIdentity e = other as EndpointIdentity;
116 return e != null && comparer.Equals (claim, e.claim);
119 public override int GetHashCode ()
121 return comparer.GetHashCode (claim);
124 public override string ToString ()
126 return String.Concat ("identity(", claim, ")");
129 protected void Initialize (Claim claim)
131 Initialize (claim, Claim.DefaultComparer);
134 protected void Initialize (Claim claim, IEqualityComparer<Claim> comparer)
137 throw new ArgumentNullException ("claim");
138 if (comparer == null)
139 throw new ArgumentNullException ("comparer");
141 this.comparer = comparer;