Advertisement
Guest User

Untitled

a guest
Jun 1st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.85 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Configuration;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6.  
  7.                    
  8. public class Program
  9. {
  10.     public static void Main()
  11.     {
  12.         PasswordDigestRequest pdr= new PasswordDigestRequest();
  13.         string wsseHeader = pdr.CreateHeader();
  14.         Console.WriteLine(wsseHeader);
  15.        
  16.         WebClient wc = new WebClient();
  17.         wc.Headers.Set("Content-Type", "application/xml; charset=UTF-8");
  18.         wc.Headers.Set("cache-control", "no-cache");
  19.         wc.Headers.Set("accept", "application/xml");
  20.         wc.Headers.Set("authorization", "WSSE realm=\"CDP\",  profile=\"UsernameToken\"");
  21.         wc.Headers.Set("x-requestheader", "request ServiceId=\"0081022000008125\"");
  22.         wc.Headers.Set("x-wsse", wsseHeader);
  23.         Console.WriteLine(wc.Headers);
  24.  
  25.         string data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><sms:subscription xmlns:sms=\"urn:oma:xml:rest:sms:1\"> <callbackReference><notifyURL>https://cd45d05c.ngrok.io</notifyURL> <callbackData>123456</callbackData> <notificationFormat>xml</notificationFormat> </callbackReference> <destinationAddress>2404996</destinationAddress> <criteria>sub</criteria> </sms:subscription>";
  26.         string uri = "http://125.60.148.174:8312/1/smsmessaging/inbound/subscriptions";
  27.         string HtmlResult = wc.UploadString(uri, data);
  28.         Console.WriteLine(HtmlResult);
  29.     }
  30.    
  31. }
  32.  
  33.     public class PasswordDigestRequest
  34.     {
  35.         internal string Username { get; set; }
  36.         internal string Password { get; set; }
  37.         internal string Created { get; set; }
  38.  
  39.         ///
  40.         /// Initializes a new instance of the PasswordDigestRequest class.
  41.         ///
  42.         public PasswordDigestRequest()
  43.         {
  44.             Username = "008102";
  45.             Password = "Vanish123";
  46.             Created = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");
  47.         }
  48.  
  49.         ///
  50.         /// Creates the header.
  51.         ///
  52.         internal string CreateHeader()
  53.         {
  54.             string nonceStr = GetNonce();
  55.             string hashedPassword = CreateHashedPassword(nonceStr, Created, Password);
  56.  
  57.             var soapStr = new StringBuilder();
  58.  
  59.             soapStr.Append("UsernameToken Username=\"");
  60.             soapStr.Append(Username);
  61.             soapStr.Append("\",PasswordDigest=\"");
  62.             soapStr.Append(hashedPassword);
  63.             soapStr.Append("\",Nonce=\"");
  64.             soapStr.Append(nonceStr);
  65.             soapStr.Append("\",Created=\"");
  66.             soapStr.Append(Created);
  67.             soapStr.Append("\"");
  68.  
  69.             return soapStr.ToString();
  70.         }
  71.  
  72.         ///
  73.         /// Creates the hashed password.
  74.         ///
  75.         internal static string CreateHashedPassword(string nonce, string created, string password)
  76.         {
  77.             string combinedString = nonce + created + password;
  78.             Console.WriteLine(combinedString);
  79.             //byte[] nonceBytes = Encoding.UTF8.GetBytes(nonce);
  80.             //byte[] createdBytes = Encoding.UTF8.GetBytes(created);
  81.             //byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  82.             //byte[] combined = new byte[createdBytes.Length + nonceBytes.Length + passwordBytes.Length];
  83.             //Buffer.BlockCopy(nonceBytes, 0, combined, 0, nonceBytes.Length);
  84.             //Buffer.BlockCopy(createdBytes, 0, combined, nonceBytes.Length, createdBytes.Length);
  85.             //Buffer.BlockCopy(passwordBytes, 0, combined, nonceBytes.Length + createdBytes.Length, passwordBytes.Length);
  86.  
  87.             return Convert.ToBase64String(SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(combinedString)));
  88.         }
  89.  
  90.         ///
  91.         /// Create a Nonce
  92.         /// returns a random nonce.
  93.         internal static string GetNonce()
  94.         {
  95.             Random rnd = new Random();
  96.             string nonceEnd = rnd.Next(99999).ToString();
  97.             string nonceDate = DateTime.Now.ToString("yyyyMMddHHmmss");
  98.            
  99.             return nonceDate + nonceEnd;
  100.         }
  101.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement