Exemplo n.º 1
0
        /// <summary>
        ///   检验是否合法
        /// </summary>
        /// <returns></returns>
        public bool CheckSign(string secretKey, char separator = ';')
        {
            var strTicketParas = GetSignContent(separator);
            var signData       = HMACSHA.EncryptBase64(strTicketParas.ToString(), secretKey);

            return(Sign == signData);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成签名后的字符串
        /// </summary>
        /// <returns></returns>
        public string ToSignData(string secretKey, char separator = ';')
        {
            TimeSpan = DateTime.Now.ToUtcSeconds();

            var encrpStr = GetSignContent(separator);

            Sign = HMACSHA.EncryptBase64(encrpStr.ToString(), secretKey);
            AddTicketProperty("sign", Sign, separator, encrpStr);
            return(encrpStr.ToString());
        }
Exemplo n.º 3
0
        private static string GeneratePostData(AliSmsConfig config, SortedDictionary <string, string> paras)
        {
            var content = string.Join("&",
                                      paras.Select(k =>
                                                   string.Concat(SpecicalUrlEncode(k.Key), "=", SpecicalUrlEncode(k.Value))));

            var preEncryStr = string.Concat("GET&", SpecicalUrlEncode("/"), "&", SpecicalUrlEncode(content));
            var sign        = HMACSHA.EncryptBase64(preEncryStr, string.Concat(config.AppSecret, "&"));

            return(string.Concat("Signature=", SpecicalUrlEncode(sign), "&", content));
        }
Exemplo n.º 4
0
        private string CompulteSign(string appId, string appVersion, string secretKey, string extSignData, char separator)
        {
            var signContent = GetContent(appId, appVersion, separator, true);

            if (!string.IsNullOrEmpty(extSignData))
            {
                signContent.Append("&").Append(extSignData);
            }

            return(HMACSHA.EncryptBase64(signContent.ToString(), secretKey));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 生成签名后的字符串
        /// </summary>
        /// <returns></returns>
        public string ToTicket(string appSource, string appVersion, string secretKey, char separator = ';')
        {
            TimeSpan = DateTime.Now.ToUtcSeconds();
            var encrpStr = GetSignContent(appSource, appVersion, separator, false);

            Sign = HMACSHA.EncryptBase64(encrpStr.ToString(), secretKey);

            var content = GetContent(appSource, appVersion, separator);

            AddTicketProperty("sign", Sign, separator, content, true);

            return(content.ToString());
        }
Exemplo n.º 6
0
        private static string GetCallBack(string key)
        {
            var token = HMACSHA.EncryptBase64(key, OssTokenSecret);

            var callBackStr = new StringBuilder();

            callBackStr.Append("{\"callbackUrl\":").Append("\"").Append(_aliCallBackUrl).Append("?t=").Append(token.UrlEncode()).Append("\",");
            //callBackStr.Append("\"callbackBodyType\":").Append("\"application/json\",");
            //callBackStr.Append("\"callbackBody\":").Append("\"{\"mime_type\":${mimeType},\"size\":${size},\"object\":${object},\"bucket\":${bucket}}\"}");
            callBackStr.Append("\"callbackBody\":").Append("bucket=${bucket}&object=${object}&size=${size}&mimeType=${mimeType}}");

            return(callBackStr.ToString().ToBase64(Encoding.UTF8));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 获取上传参数
        /// </summary>
        /// <param name="bucket_name"></param>
        /// <param name="category"></param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        private static Resp <BucketUploadPara> GetUploadPara(string bucket_name, string category, string userId)
        {
            var osappInfo = AppReqContext.Identity;

            var domain = GetDomainByBucket(bucket_name);

            if (string.IsNullOrEmpty(domain))
            {
                return(new Resp <BucketUploadPara>().WithResp(RespTypes.NoPermission, "没有当前图片空间的上传权限"));
            }

            var patStr = new StringBuilder();

            if (AppInfoHelper.IsDev)
            {
                patStr.Append("test/");
            }

            patStr.Append(osappInfo.tenant_id).Append("/")
            .Append((int)osappInfo.app_type).Append("-").Append(userId).Append("/")
            .Append(category).Append("/")
            .Append(DateTime.Now.ToUtcMilliSeconds());

            var key = patStr.ToString();


            var expiration = DateTime.Now.AddMinutes(3);

            //var callBack = GetCallBack(key);
            var policyConds = new PolicyConditions();

            policyConds.AddConditionItem("bucket", bucket_name);
            policyConds.AddConditionItem(MatchMode.StartWith, PolicyConditions.CondKey, key);
            //policyConds.AddConditionItem("callback", callBack);

            var encPostPolicy = policyConds.GeneratePostPolicyJsonBase64(expiration);
            var signature     = HMACSHA.EncryptBase64(encPostPolicy, _aliConfig.AppSecret);
            var bucketConfig  = new BucketUploadPara();

            bucketConfig.paras.Add("key", key);
            bucketConfig.paras.Add("OSSAccessKeyId", _aliConfig.AppId);
            bucketConfig.paras.Add("policy", encPostPolicy);
            bucketConfig.paras.Add("signature", signature);
            //bucketConfig.paras.Add("callback", callBack);

            bucketConfig.upload_url = domain;

            return(new Resp <BucketUploadPara>(bucketConfig));
        }
Exemplo n.º 8
0
        /// <summary>
        ///  格式化 上传文件实体
        /// </summary>
        /// <param name="mo"></param>
        /// <param name="token"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool FormatUploadMo(UploadFileMo mo, string token, string key)
        {
            if (string.IsNullOrEmpty(token))
            {
                return(false);
            }

            var encryptKey = HMACSHA.EncryptBase64(key, OssTokenSecret);

            if (token != encryptKey)
            {
                return(false);
            }

            var splitKey = key.Split('/');
            var length   = splitKey.Length;

            if (length < 4)
            {
                return(false);
            }

            var splitUser = splitKey[length - 3].Split('-');

            if (splitUser.Length < 2)
            {
                return(false);
            }

            mo.owner_tid = splitKey[length - 4];

            //var appType = splitUser[0].ToInt32();
            mo.owner_tid = mo.owner_tid;
            mo.owner_uid = splitUser[1];
            mo.url       = string.Concat(GetDomainByBucket(mo.bucket), "/", key);
            if (mo.mime_type.StartsWith("image"))
            {
                mo.type = UploadFileType.Image;
            }

            return(true);
        }