コード例 #1
0
        /// <summary>
        /// add authorization
        /// </summary>
        /// <param name="qcloudSignSource">QCloudSignSource</param>
        /// <param name="request"></param>
        /// <param name="credentialProvider"></param>
        private void CheckSign(IQCloudSignSource qcloudSignSource, Request request, QCloudCredentialProvider credentialProvider)
        {
            // has authorizaiton, notice: using request.Headers, otherwise, error
            if (request.Headers.ContainsKey(CosRequestHeaderKey.AUTHORIZAIION))
            {
                QLog.Debug(TAG, "has add authorizaiton in headers");

                return;
            }

            //has no authorization, but signSourceProvider == null
            if (qcloudSignSource == null)
            {
                QLog.Debug(TAG, "signSourceProvider == null");

                return;
            }

            if (credentialProvider == null)
            {
                throw new ArgumentNullException("credentialsProvider == null");
            }

            CosXmlSigner signer = new CosXmlSigner();

            signer.Sign(request, qcloudSignSource, credentialProvider.GetQCloudCredentialsCompat(request));
        }
コード例 #2
0
        public string GenerateSign(string method, string key, Dictionary <string, string> queryParameters, Dictionary <string, string> headers, long signDurationSecond)
        {
            try
            {
                string signTime = null;

                if (signDurationSecond > 0)
                {
                    long currentTimeSecond = TimeUtils.GetCurrentTime(TimeUnit.Seconds);

                    signTime = String.Format("{0};{1}", currentTimeSecond, currentTimeSecond + signDurationSecond);
                }

                Dictionary <string, string> encodeQuery = null;

                if (queryParameters != null)
                {
                    encodeQuery = new Dictionary <string, string>(queryParameters.Count);

                    foreach (KeyValuePair <string, string> keyValuePair in queryParameters)
                    {
                        if (keyValuePair.Key == null || keyValuePair.Key == "")
                        {
                            continue;
                        }
                        else if (keyValuePair.Value == null)
                        {
                            encodeQuery[URLEncodeUtils.Encode(keyValuePair.Key).ToLower()] = URLEncodeUtils.Encode("");
                        }
                        else
                        {
                            encodeQuery[URLEncodeUtils.Encode(keyValuePair.Key).ToLower()] = URLEncodeUtils.Encode(keyValuePair.Value);
                        }
                    }
                }

                return(CosXmlSigner.GenerateSign(method, key, encodeQuery, headers, signTime, credentialProvider.GetQCloudCredentialsCompat(null)));
            }
            catch (CosClientException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new CosClientException((int)CosClientError.InvalidArgument, ex.Message, ex);
            }
        }