配置
server {
listen 8080;
secure_link $arg_md5,$arg_expires;
secure_link_md5 "$secure_link_expires$uri secret_key";
if ($secure_link = ""){
return 403;
}
if ($secure_link = "0"){
return 410;
}
root E:\Collection\;
}
实现
阅读文章和文档。
var link = new Uri("http://localhost:8080/sample.png");
var expires = new DateTimeOffset(new DateTime(2021, 5, 11)).ToUnixTimeSeconds();
string str = $"{expires}{link.LocalPath} secret_key";
Console.WriteLine(str);
var md5 = MD5.Create();
var md5Str = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(str)))
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
Console.WriteLine($"{link}?md5={md5Str}&expires={expires}");
输出
1620662400/sample.png secret_key
http://localhost:8080/sample.png?md5=rBlDE51CM1v5dc6Wr7my9Q&expires=1620662400