static void Main() { // Get Certificate which identifies the target endpoint. X509Certificate2 cert = this.getCertificate(www.fabrikam.com, X509FindType.FindBySubjectName, StoreLocation.LocalMachine, StoreName.My);
// Creates the target element #region Target EndpointIdentity identity = EndpointIdentity.CreateX509CertificateIdentity(cert); Uri targetUri = new Uri("http://localhost/MyApplication"); EndpointAddress adr = new EndpointAddress(targetUri, identity, new AddressHeader[0]);
StringBuilder sbTarget = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sbTarget)) { adr.WriteTo(AddressingVersion.WSAddressing10, writer); }
XmlDocument docTarget = new XmlDocument(); docTarget.LoadXml(sbTarget.ToString()); #endregion
// Creates the Issuer #region Issuer Uri issuerUri = new Uri("http://www.fabrikam.com/Sts/ UserNameToken.svc/usernamepassword/sts");
EndpointAddress issuerAddre = new EndpointAddress(issuerUri);
StringBuilder sbIssuer = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sbIssuer)) { issuerAddre.WriteTo(AddressingVersion.WSAddressing10, writer); }
XmlDocument docIssuer = new XmlDocument(); docIssuer.LoadXml(sbIssuer.ToString()); #endregion
// Creates the TokenType #region TokenType string tokenType = @"<wst:TokenType xmlns:wst='http://schemas.xmlsoap.org/ws/2005/02/trust'> urn:oasis:names:tc:SAML:1.0:assertion</wst:TokenType>";
XmlDocument docTokenType = new XmlDocument(); docTokenType.LoadXml(tokenType); #endregion
// Creates required and optional Claims #region Claims string claimsEl = "<t:Claims xmlns:t='http://schemas.xmlsoap.org/ws/2005/02/trust'> {0}</t:Claims>";
string requiredClaims = "<wsid:ClaimType Uri='{0}' xmlns:wsid='http://schemas.xmlsoap.org/ws/2005/05/ identity' />";
string optionalClaims = "<wsid:ClaimType Uri='{0}' Optional=xmlns:wsid='http://schemas.xmlsoap.org/ws/ 2005/05/identity' />";
StringBuilder sb = new StringBuilder();
sb.AppendFormat(requiredClaims, ClaimTypes.GivenName); sb.AppendFormat(requiredClaims, ClaimTypes.Surname); sb.AppendFormat(requiredClaims, ClaimTypes.Email);
sb.AppendFormat(optionalClaims, "http://daenet.eu/identity/sampleclaim");
XmlDocument docClaims = new XmlDocument();
docClaims.LoadXml(string.Format(claimsEl, sb.ToString())); #endregion
// Adds token type and claims as parameter Collection<XmlElement> parameters = new Collection<XmlElement>(); parameters.Add(docTokenType.DocumentElement); parameters.Add(docClaims.DocumentElement);
// Creates the policy element. CardSpacePolicyElement polEl = new CardSpacePolicyElement(docTarget.DocumentElement, docIssuer.DocumentElement, parameters, null, 0, false);
// Creates the token GenericXmlSecurityToken token = CardSpaceSelector.GetToken(new CardSpacePolicyElement[] { polEl }, System.ServiceModel.Security. WSSecurityTokenSerializer.DefaultInstance); Console.WriteLine(token.TokenXml.OuterXml); } |