Пример #1
0
        /// <summary>
        /// sorting string by BigEndian, for calculating AmazonMWS signature
        /// </summary>
        /// <param name="inStr">string to be sorted</param>
        /// <returns>sorted string</returns>
        public string GetByteOrder(string inStr)
        {
            ParamComparer pc = new ParamComparer();
            SortedDictionary <string, string> slist = new SortedDictionary <string, string>(pc);

            while (inStr.IndexOf("&", 0) >= 0)
            {
                int i = inStr.IndexOf("&", 0);

                slist.Add(inStr.Substring(0, i), "");
                inStr = inStr.Remove(0, i + 1);
            }

            slist.Add(inStr, "");

            string result = "";

            foreach (KeyValuePair <string, string> item in slist)
            {
                if (result == "")
                {
                    result = (item.Key);
                }
                else
                {
                    result += "&" + (item.Key);
                }
            }

            return(result);
        }
Пример #2
0
    /*
     * Sign a request in the form of a Dictionary of name-value pairs.
     *
     * This method returns a complete URL to use. Modifying the returned URL
     * in any way invalidates the signature and Amazon will reject the requests.
     */
    public string Sign(IDictionary <string, string> request)
    {
        // Use a SortedDictionary to get the parameters in naturual byte order, as
        // required by AWS.
        ParamComparer pc = new ParamComparer();
        SortedDictionary <string, string> sortedMap = new SortedDictionary <string, string>(request, pc);

        // Add the AWSAccessKeyId and Timestamp to the requests.
        sortedMap["AWSAccessKeyId"] = this.akid;
        sortedMap["Timestamp"]      = this.GetTimestamp();
        sortedMap["AssociateTag"]   = this.associateTag;

        // Get the canonical query string
        string canonicalQS = this.ConstructCanonicalQueryString(sortedMap);

        // Derive the bytes needs to be signed.
        StringBuilder builder = new StringBuilder();

        builder.Append(REQUEST_METHOD)
        .Append("\n")
        .Append(this.endPoint)
        .Append("\n")
        .Append(REQUEST_URI)
        .Append("\n")
        .Append(canonicalQS);

        string stringToSign = builder.ToString();

        byte[] toSign = Encoding.UTF8.GetBytes(stringToSign);

        // Compute the signature and convert to Base64.
        byte[] sigBytes  = signer.ComputeHash(toSign);
        string signature = Convert.ToBase64String(sigBytes);

        // now construct the complete URL and return to caller.
        StringBuilder qsBuilder = new StringBuilder();

        qsBuilder.Append("http://")
        .Append(this.endPoint)
        .Append(REQUEST_URI)
        .Append("?")
        .Append(canonicalQS)
        .Append("&Signature=")
        .Append(this.PercentEncodeRfc3986(signature));

        return(qsBuilder.ToString());
    }
Пример #3
0
            public string GetSignedQueryString(string methodName, IDictionary <string, string> request, string requestMethod)
            {
                // Use a SortedDictionary to get the parameters in natural byte order
                ParamComparer pc = new ParamComparer();
                SortedDictionary <string, string> sortedMap = new SortedDictionary <string, string>(request, pc);

                // Add Timestamp to the requests.
                sortedMap["timestamp"] = this.GetTimestamp();

                // Get the canonical query string
                string canonicalQueryString = this.ConstructCanonicalQueryString(sortedMap);

                string signature = GetSignatureUsingCanonicalQueryString(methodName, canonicalQueryString, requestMethod);

                // now construct the complete URL and return to caller.
                StringBuilder qsBuilder = new StringBuilder();

                qsBuilder.Append(canonicalQueryString).Append("&signature=").Append(signature);

                return(qsBuilder.ToString());
            }
Пример #4
0
        public XmlDocument GetAwsResources(string resourceType, string resourceId)
        {
            _logProvider.WriteLog("GetAwsResources", "Start");

            string endpoint = ConfigurationManager.AppSettings["endpoint"];

            ParamComparer pc = new ParamComparer();
            SortedDictionary <string, string> sortedRequestPars =
                new SortedDictionary <string, string>(pc);

            string action = "", resourceName = "", filterName = "", filterValue = "";

            getActionAndId(resourceType, out action, out resourceName, out filterValue, out filterName);
            if (resourceName != "" && filterName != "" && filterValue != "")
            {
                sortedRequestPars.Add(filterName, resourceName);
                sortedRequestPars.Add(filterValue, resourceId);
            }
            sortedRequestPars.Add("Action", action);
            sortedRequestPars.Add("SignatureMethod", "HmacSHA1");
            sortedRequestPars.Add("Version", "2010-11-15");
            sortedRequestPars.Add("SignatureVersion", "2");
            string date = GetEC2Date();

            sortedRequestPars.Add("Timestamp", date);
            sortedRequestPars.Add("AWSAccessKeyId", "AKIAI72AH2JEMPVR3I2Q");

            var    stringToSign = ComposeStringToSign(sortedRequestPars, "GET", endpoint, "/");
            var    signature    = GetAWS3_SHA1AuthorizationValue("AKIAI72AH2JEMPVR3I2Q", "l8SKjLvjHxFv5KVrEX4B3WXTfrkaYBkBrQUmx0yB", stringToSign);
            string queryString  = "https://" + endpoint + "/?" + GetSortedParamsAsString(sortedRequestPars, false) + "&Signature=" + signature;

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(queryString);

            request.Method      = "GET";
            request.ContentType = "application/text";

            string xml = "";

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                xml = new StreamReader(response.GetResponseStream()).ReadToEnd();

                //xml = new StreamReader(response.GetResponseStream()).ReadToEnd();
                _logProvider.WriteLog("GetAwsResources", "RESPONSE " + response.StatusCode);
            }
            catch (Exception exception)
            {
                _logProvider.WriteLog("GetAwsResources", "EXCEPTION " + exception.Message);
                //DialogResult dialogresult = MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                xml = "";
                //Application.ExitThread();
            }

            if (xml != "")
            {
                XmlDocument xmlDoc = RemoveXmlns(xml);

                _logProvider.WriteLog("GetAzureASMResources", "End");
                // writeXMLtoFile(url, xml);

                _documentCache.Add(queryString, xmlDoc);
                return(xmlDoc);
            }
            else
            {
                //XmlNodeList xmlnode = null;
                //return xmlnode;
                XmlDocument xmlDoc = new XmlDocument();

                _logProvider.WriteLog("GetAzureASMResources", "End");
                writeXMLtoFile(queryString, "");
                return(null);
            }
        }
Пример #5
0
        // Start is called before the first frame update
        void Start()
        {
            var listA = new List <int>()
            {
                0, 1, 2
            };
            var listB = new List <int>()
            {
                0, 1, 2
            };
            var listC = new List <int>()
            {
                0, 2, 1
            };

            Debug.Log("listA==listB:" + listA.SequenceEqual(listB)); // true
            Debug.Log("listA==listC:" + listA.SequenceEqual(listC)); // false

            var listD = new List <Param>()
            {
                new Param()
                {
                    a = 0, b = 1
                }, new Param()
                {
                    a = 1, b = 0
                }
            };
            var listE = new List <Param>()
            {
                new Param()
                {
                    a = 0, b = 1
                }, new Param()
                {
                    a = 1, b = 0
                }
            };

            Debug.Log("listD==listE:" + listD.SequenceEqual(listE));// false

            var paramA = new Param()
            {
                a = 0, b = 1
            };
            var paramB = new Param()
            {
                a = 1, b = 0
            };
            var listF = new List <Param>()
            {
                paramA, paramB
            };
            var listG = new List <Param>()
            {
                paramA, paramB
            };
            var listH = new List <Param>()
            {
                paramB, paramA
            };

            Debug.Log("listF==listG:" + listF.SequenceEqual(listG)); // true
            Debug.Log("listF==listH:" + listF.SequenceEqual(listH)); // false

            var listI = new List <Param>()
            {
                new Param()
                {
                    a = 0, b = 1
                }, new Param()
                {
                    a = 1, b = 0
                }
            };
            var listJ = new List <Param>()
            {
                new Param()
                {
                    a = 0, b = 1
                }, new Param()
                {
                    a = 1, b = 0
                }
            };
            var paramComparer = new ParamComparer();

            Debug.Log("listI==listJ:" + listI.SequenceEqual(listJ, paramComparer));// true
        }