Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomCredentials : ClientCredentials
- {
- public CustomCredentials()
- { }
- protected CustomCredentials(CustomCredentials cc)
- : base(cc)
- { }
- public override System.IdentityModel.Selectors.SecurityTokenManager CreateSecurityTokenManager()
- {
- return new CustomSecurityTokenManager(this);
- }
- protected override ClientCredentials CloneCore()
- {
- return new CustomCredentials(this);
- }
- }
- public class CustomSecurityTokenManager : ClientCredentialsSecurityTokenManager
- {
- public CustomSecurityTokenManager(CustomCredentials cred)
- : base(cred)
- { }
- public override System.IdentityModel.Selectors.SecurityTokenSerializer CreateSecurityTokenSerializer(System.IdentityModel.Selectors.SecurityTokenVersion version)
- {
- return new CustomTokenSerializer(System.ServiceModel.Security.SecurityVersion.WSSecurity11);
- }
- }
- public class CustomTokenSerializer : WSSecurityTokenSerializer
- {
- public CustomTokenSerializer(SecurityVersion sv)
- : base(sv)
- { }
- protected override void WriteTokenCore(System.Xml.XmlWriter writer, SecurityToken token)
- {
- UserNameSecurityToken userToken = token as UserNameSecurityToken;
- string tokennamespace = "o";
- DateTime created = DateTime.UtcNow;
- string createdStr = created.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
- string phrase = Guid.NewGuid().ToString();
- var nonce = GetSHA1String(phrase);
- string hashedPassword = GetSHA1String(userToken.Password);
- string pwd = GetPasswordDigest(nonce, createdStr, hashedPassword);
- var ws2004Prefix = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-";
- var u = (XNamespace)(ws2004Prefix + "wssecurity-utility-1.0.xsd");
- var o = (XNamespace)(ws2004Prefix + "wssecurity-secext-1.0.xsd");
- var pwdTextType = (ws2004Prefix + "username-token-profile-1.0#PasswordDigest");
- var base64Type = (ws2004Prefix + "soap-message-security-1.0#Base64Binary");
- //var xDoc = new XDocument(
- // new XElement("root",
- // new XAttribute(XNamespace.Xmlns + "o", o),
- // new XElement(o + "UsernameToken",
- // new XAttribute(u + "Id", token.Id),
- // new XAttribute(XNamespace.Xmlns + "u", u),
- // new XElement(o + "Username", userToken.UserName),
- // new XElement(o + "Password", new XAttribute("Type", pwdTextType), pwd),
- // new XElement(o + "TestField", new XAttribute("Test", pwdTextType), "testField"),
- // new XElement(o + "Nonce", new XAttribute("EncodingType", base64Type), nonce),
- // new XElement(u + "Created", createdStr))));
- //xDoc.Root.Element(o + "UsernameToken").WriteTo(writer);
- string test = (string.Format(
- "<{0}:UsernameToken u:Id=\"" + token.Id +
- "\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd \">" +
- "<{0}:Username>" + userToken.UserName + "</{0}:Username>" +
- "<{0}:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">" +
- pwd + "</{0}:Password>" +
- "<{0}:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary \">" +
- nonce + "</{0}:Nonce>" +
- "<u:Created>" + createdStr + "</u:Created></{0}:UsernameToken>", tokennamespace));
- writer.WriteRaw(test);
- }
- protected string GetSHA1String(string phrase)
- {
- SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider();
- byte[] hashedDataBytes = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(phrase));
- return Convert.ToBase64String(hashedDataBytes);
- }
- public static string GetPasswordDigest(string nonce, string created, string pass)
- {
- byte[] bNonce = Convert.FromBase64String(nonce);
- byte[] bCreated = Encoding.UTF8.GetBytes(created);
- byte[] bPass = Encoding.UTF8.GetBytes(pass);
- byte[] bAll = new byte[bNonce.Length + bCreated.Length + bPass.Length];
- Buffer.BlockCopy(bNonce, 0, bAll, 0, bNonce.Length);
- Buffer.BlockCopy(bCreated, 0, bAll, bNonce.Length, bCreated.Length);
- Buffer.BlockCopy(bPass, 0, bAll, bNonce.Length + bCreated.Length, bPass.Length);
- return Sha1Base64Digest(bAll);
- }
- public static String Sha1Base64Digest(byte[] phrase)
- {
- SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider();
- byte[] hashedDataBytes = sha1Hasher.ComputeHash(phrase);
- return Convert.ToBase64String(hashedDataBytes);
- }
- }
- public class SENT_100
- {
- public static void SendToSeap()
- {
- try
- {
- string smplXML = sampleXML;
- byte[] bytes = Encoding.UTF8.GetBytes(smplXML);
- string base64 = Convert.ToBase64String(bytes);
- msg = Convert.ToBase64String(byteArray);
- CustomBinding binding = new CustomBinding();
- var security = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
- security.IncludeTimestamp = false;
- security.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256;
- security.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
- var encoding = new TextMessageEncodingBindingElement();
- encoding.MessageVersion = MessageVersion.Soap11;
- var transport = new HttpsTransportBindingElement();
- //transport.MaxReceivedMessageSize = 20000000; // 20 megs
- binding.Elements.Add(security);
- binding.Elements.Add(encoding);
- binding.Elements.Add(transport);
- AcceptDocumentRequest acceptDocumentRequest = new AcceptDocumentRequest();
- acceptDocumentRequest.document = new documentType();
- acceptDocumentRequest.document.content = new contentType();
- acceptDocumentRequest.document.content.filename = "test.xml";
- acceptDocumentRequest.document.content.mime = mimeType.applicationxml;
- acceptDocumentRequest.document.content.Value = bytes;
- acceptDocumentRequest.document.targetSystems = new systemType[] { systemType.SENT };
- AcceptDocumentResponse acceptDocumentResponse;
- var endpoint = new EndpointAddress("https://wstest.puesc.gov.pl/seap_wsChannel/DocumentHandlingPort?wsdl");
- DocumentHandlingPortClient client = new DocumentHandlingPortClient(binding, endpoint);
- client.ChannelFactory.Endpoint.Behaviors.Remove<System.ServiceModel.Description.ClientCredentials>();
- client.ChannelFactory.Endpoint.Behaviors.Add(new CustomCredentials());
- client.ClientCredentials.UserName.UserName = username;
- client.ClientCredentials.UserName.Password = password;
- acceptDocumentResponse = client.AcceptDocument(acceptDocumentRequest);
- client.Close();
- }
- catch (Exception ex)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement