阿里云 oss 上传文件,js直传,.net 签名,回调

后台签名

添加引用

阿里云 oss 上传文件,js直传,.net 签名,回调

string dir =  string.Format("{0:yyyy-MM-dd}", date) + "/";
OssClient client = new OssClient(Endpoint, AccessKeyId, AccessKeySecret); DateTime ex = DateTime.Now.AddSeconds(); PolicyConditions policyConds = new PolicyConditions(); policyConds.AddConditionItem(PolicyConditions.CondContentLengthRange, 0L, 1048576000L);
policyConds.AddConditionItem(MatchMode.StartWith, PolicyConditions.CondKey, dir); string postPolicy = client.GeneratePostPolicy(ex, policyConds);
byte[] binaryData = Encoding.UTF8.GetBytes(postPolicy);
string policy = Convert.ToBase64String(binaryData);
var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(AccessKeySecret));
var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(policy));
var signature = Convert.ToBase64String(hashBytes);
OSSSign sign = new OSSSign
{
OSSAccessKeyId = AccessKeyId,
policy = policy,
signature = signature,
Host = "https://" + BucketName + "." + Endpoint,
success_action_status = "", Filename = "${filename}",
key = dir + "${filename}"
};
TimeSpan ts = ex.ToUniversalTime() - new DateTime(, , , , , , );
sign.expire = Convert.ToInt64(ts.TotalSeconds).ToString(); sign.callback = JsonConvert.SerializeObject(new
{
callbackUrl = CallbackUrl,
callbackHost = CallbackHost,
callbackBody = "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}",
callbackBodyType = "application/x-www-form-urlencoded"
});
//sign.callback = Convert.ToBase64String(Encoding.UTF8.GetBytes(sign.callback));
return sign;

回调  一般处理程序实现

 LogHelper.Warn("oss 回调" + DateTime.Now.ToLongTimeString());

            string autorizationInput = context.Request.Headers["Authorization"];
string pubKeyInput = context.Request.Headers["x-oss-pub-key-url"]; string bucket = context.Request.Headers["x-oss-bucket"];
LogHelper.Warn(autorizationInput + "==" + pubKeyInput);
byte[] pubKey = Convert.FromBase64String(pubKeyInput);
string pubKeyAddr = Encoding.Default.GetString(pubKey);//公钥的url地址
//x-oss-pub-key-url 头的值必须以 开始
if (!pubKeyAddr.StartsWith("http://gosspublic.alicdn.com/") && !pubKeyAddr.StartsWith("https://gosspublic.alicdn.com/"))
{
return;
}
#region 签名校验没有实现。。。。。。
//string publickey = executeGet(pubKeyAddr);//公钥
//publickey = publickey.Replace("-----BEGIN PUBLIC KEY-----", "");
//publickey = publickey.Replace("\n", "");
//publickey = publickey.Replace("-----END PUBLIC KEY-----", ""); //获取待签名字符串
//string queryString = context.Request.Url.Query;
//string uri = context.Request.Url.AbsolutePath;
//string decodeUri = HttpContext.Current.Server.UrlDecode(uri);
//string authStr = decodeUri;
//if (string.IsNullOrEmpty(queryString))
//{
//authStr += "?" + queryString;
//}
//authStr += "bucket=" + bucket; //using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
//{
// byte[] binKey = Convert.FromBase64String(publickey); // Base64解码
// RSAParameters rsap = new RSAParameters();
// rsap.Modulus = binKey;
// rsap.Exponent = new byte[] { 1, 0, 1 };
// rsa.ImportParameters(rsap); // MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
// var data = rsa.SignData(Convert.FromBase64String(authStr), md5);
// var flag = rsa.VerifyData(data, md5, Convert.FromBase64String(autorizationInput));
// if (flag)
// LogHelper.Warn("success");
//} #endregion string filename = context.Request.Params["filename"];
string companyid = context.Request.Params["companyid"];
string typeid = context.Request.Params["typeid"];
string size = context.Request.Params["size"];
string mimeType = context.Request.Params["mimeType"];
string height = context.Request.Params["height"];
string width = context.Request.Params["width"]; context.Response.ContentType = "application/json";
context.Response.StatusCode = ;
context.Response.Write("{\"Status\":\"OK\"}");
context.Response.End();

/// <summary>
/// 读取公钥
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public string executeGet(string url)
{
string responseStr;
Uri address = new Uri(url);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Accept = "application/xml";
request.ContentType = "application/xml;charset=utf-8";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
responseStr = reader.ReadToEnd();
}


return responseStr;
}

 
上一篇:Ubuntu下Eclipse安装与编译ns-3遇见的各种问题


下一篇:iOS 图片拉伸 resizableImageWithCapInsets