.net C# 阿里云 邮箱推送 签名验证

    public static string Sign(string method, string accessKeySecret,Dictionary<string, string> bodys)
    {
        string toSign = method + "&" + HttpUtility.UrlEncode("/", System.Text.Encoding.UTF8).ToUpper() + "&" + getUtf8Encoder(ParamStrURLEncoder(bodys));

        //HMACSHA1加密
        HMACSHA1 hmacsha1 = new HMACSHA1();
        hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(toSign);

        byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(accessKeySecret + "&");
        byte[] bytes = hmacsha1.ComputeHash(dataBuffer);
        return Convert.ToBase64String(bytes);
    }
    public static string getUtf8Encoder(string param)
    {
        string result = UrlEncode(param)
                        .Replace("+", "%20");
        result = result.Replace("*", "%2A");
        result = result.Replace("%7E", "~");
        return result;
    }
    /// <summary>
    /// Url编码转换大写
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string UrlEncode(string str)
    {
        StringBuilder builder = new StringBuilder();
        foreach (char c in str)
        {
            if (HttpUtility.UrlEncode(c.ToString()).Length > 1 || c == 0x20)
            {
                builder.Append(HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).ToUpper());
            }
            else
            {
                builder.Append(c);
            }
        }
        return builder.ToString();
    }
    /// <summary>
    /// 将默认url编码改为阿里云规定编码
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public static string ParamStrURLEncoder(Dictionary<string, string> param)
    {
        StringBuilder builder = new StringBuilder();
        foreach (var entry in param)
        {
            if (string.IsNullOrWhiteSpace(Convert.ToString(entry.Key)) || entry.Value == null)
            {
                continue;
            }
            builder.Append(getUtf8Encoder(entry.Key) + "=" + getUtf8Encoder(Convert.ToString(entry.Value)) + "&");
        }
        return builder.ToString().Substring(0, builder.ToString().LastIndexOf("&")); ;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值