Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.79 KB | None | 0 0
  1. public class CustomCredentials : ClientCredentials
  2.     {
  3.         public CustomCredentials()
  4.         { }
  5.  
  6.         protected CustomCredentials(CustomCredentials cc)
  7.             : base(cc)
  8.         { }
  9.  
  10.         public override System.IdentityModel.Selectors.SecurityTokenManager CreateSecurityTokenManager()
  11.         {
  12.             return new CustomSecurityTokenManager(this);
  13.         }
  14.  
  15.         protected override ClientCredentials CloneCore()
  16.         {
  17.             return new CustomCredentials(this);
  18.         }
  19.     }
  20.  
  21.     public class CustomSecurityTokenManager : ClientCredentialsSecurityTokenManager
  22.     {
  23.         public CustomSecurityTokenManager(CustomCredentials cred)
  24.             : base(cred)
  25.         { }
  26.  
  27.         public override System.IdentityModel.Selectors.SecurityTokenSerializer CreateSecurityTokenSerializer(System.IdentityModel.Selectors.SecurityTokenVersion version)
  28.         {
  29.             return new CustomTokenSerializer(System.ServiceModel.Security.SecurityVersion.WSSecurity11);
  30.         }
  31.     }
  32.  
  33.     public class CustomTokenSerializer : WSSecurityTokenSerializer
  34.     {
  35.         public CustomTokenSerializer(SecurityVersion sv)
  36.             : base(sv)
  37.         { }
  38.  
  39.         protected override void WriteTokenCore(System.Xml.XmlWriter writer, SecurityToken token)
  40.         {
  41.             UserNameSecurityToken userToken = token as UserNameSecurityToken;
  42.  
  43.             string tokennamespace = "o";
  44.  
  45.             DateTime created = DateTime.UtcNow;
  46.             string createdStr = created.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
  47.  
  48.             string phrase = Guid.NewGuid().ToString();
  49.             var nonce = GetSHA1String(phrase);
  50.  
  51.             string hashedPassword = GetSHA1String(userToken.Password);
  52.  
  53.             string pwd = GetPasswordDigest(nonce, createdStr, hashedPassword);
  54.  
  55.             var ws2004Prefix = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-";
  56.             var u = (XNamespace)(ws2004Prefix + "wssecurity-utility-1.0.xsd");
  57.             var o = (XNamespace)(ws2004Prefix + "wssecurity-secext-1.0.xsd");
  58.             var pwdTextType = (ws2004Prefix + "username-token-profile-1.0#PasswordDigest");
  59.             var base64Type = (ws2004Prefix + "soap-message-security-1.0#Base64Binary");
  60.  
  61.             //var xDoc = new XDocument(
  62.             //    new XElement("root",
  63.             //        new XAttribute(XNamespace.Xmlns + "o", o),
  64.             //        new XElement(o + "UsernameToken",
  65.             //            new XAttribute(u + "Id", token.Id),
  66.             //            new XAttribute(XNamespace.Xmlns + "u", u),
  67.             //            new XElement(o + "Username", userToken.UserName),
  68.             //            new XElement(o + "Password", new XAttribute("Type", pwdTextType), pwd),
  69.             //            new XElement(o + "TestField", new XAttribute("Test", pwdTextType), "testField"),
  70.             //            new XElement(o + "Nonce", new XAttribute("EncodingType", base64Type), nonce),
  71.             //            new XElement(u + "Created", createdStr))));
  72.  
  73.             //xDoc.Root.Element(o + "UsernameToken").WriteTo(writer);
  74.  
  75.             string test = (string.Format(
  76.              "<{0}:UsernameToken u:Id=\"" + token.Id +
  77.              "\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd \">" +
  78.              "<{0}:Username>" + userToken.UserName + "</{0}:Username>" +
  79.              "<{0}:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">" +
  80.              pwd + "</{0}:Password>" +
  81.              "<{0}:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary \">" +
  82.              nonce + "</{0}:Nonce>" +
  83.              "<u:Created>" + createdStr + "</u:Created></{0}:UsernameToken>", tokennamespace));
  84.             writer.WriteRaw(test);
  85.         }
  86.  
  87.         protected string GetSHA1String(string phrase)
  88.         {
  89.             SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider();
  90.             byte[] hashedDataBytes = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(phrase));
  91.             return Convert.ToBase64String(hashedDataBytes);
  92.         }
  93.  
  94.         public static string GetPasswordDigest(string nonce, string created, string pass)
  95.         {
  96.             byte[] bNonce = Convert.FromBase64String(nonce);
  97.             byte[] bCreated = Encoding.UTF8.GetBytes(created);
  98.             byte[] bPass = Encoding.UTF8.GetBytes(pass);
  99.             byte[] bAll = new byte[bNonce.Length + bCreated.Length + bPass.Length];
  100.  
  101.             Buffer.BlockCopy(bNonce, 0, bAll, 0, bNonce.Length);
  102.             Buffer.BlockCopy(bCreated, 0, bAll, bNonce.Length, bCreated.Length);
  103.             Buffer.BlockCopy(bPass, 0, bAll, bNonce.Length + bCreated.Length, bPass.Length);
  104.  
  105.             return Sha1Base64Digest(bAll);
  106.         }
  107.  
  108.         public static String Sha1Base64Digest(byte[] phrase)
  109.         {
  110.             SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider();
  111.             byte[] hashedDataBytes = sha1Hasher.ComputeHash(phrase);
  112.             return Convert.ToBase64String(hashedDataBytes);
  113.         }
  114.     }
  115.  
  116.  
  117. public class SENT_100
  118.     {  
  119.         public static void SendToSeap()
  120.         {
  121.             try
  122.             {
  123.                 string smplXML = sampleXML;
  124.                 byte[] bytes = Encoding.UTF8.GetBytes(smplXML);
  125.                 string base64 = Convert.ToBase64String(bytes);
  126.                 msg = Convert.ToBase64String(byteArray);
  127.                 CustomBinding binding = new CustomBinding();
  128.  
  129.                 var security = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
  130.                 security.IncludeTimestamp = false;
  131.                 security.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256;
  132.                 security.MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
  133.  
  134.                 var encoding = new TextMessageEncodingBindingElement();
  135.                 encoding.MessageVersion = MessageVersion.Soap11;
  136.  
  137.                 var transport = new HttpsTransportBindingElement();
  138.                 //transport.MaxReceivedMessageSize = 20000000; // 20 megs
  139.  
  140.                 binding.Elements.Add(security);
  141.                 binding.Elements.Add(encoding);
  142.                 binding.Elements.Add(transport);
  143.  
  144.                 AcceptDocumentRequest acceptDocumentRequest = new AcceptDocumentRequest();
  145.                
  146.                 acceptDocumentRequest.document = new documentType();
  147.                 acceptDocumentRequest.document.content = new contentType();
  148.                 acceptDocumentRequest.document.content.filename = "test.xml";
  149.                 acceptDocumentRequest.document.content.mime = mimeType.applicationxml;
  150.                 acceptDocumentRequest.document.content.Value = bytes;
  151.                 acceptDocumentRequest.document.targetSystems = new systemType[] { systemType.SENT };
  152.                 AcceptDocumentResponse acceptDocumentResponse;
  153.                 var endpoint = new EndpointAddress("https://wstest.puesc.gov.pl/seap_wsChannel/DocumentHandlingPort?wsdl");
  154.                 DocumentHandlingPortClient client = new DocumentHandlingPortClient(binding, endpoint);
  155.                 client.ChannelFactory.Endpoint.Behaviors.Remove<System.ServiceModel.Description.ClientCredentials>();
  156.                 client.ChannelFactory.Endpoint.Behaviors.Add(new CustomCredentials());
  157.  
  158.                 client.ClientCredentials.UserName.UserName = username;
  159.                 client.ClientCredentials.UserName.Password = password;
  160.  
  161.                 acceptDocumentResponse = client.AcceptDocument(acceptDocumentRequest);
  162.                 client.Close();
  163.             }
  164.             catch (Exception ex)
  165.             {
  166.             }
  167.         }
  168.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement