AWS API - 不接受我的签名

发布于 2024-10-30 21:37:33 字数 2411 浏览 1 评论 0原文

我已经尝试了这里的所有方法,但 Amazon Load Balancer API 就是不接受我的签名。

无论我尝试什么,我都会收到“403 SignatureDoesNotMatch”响应。我可能错过了一些明显的东西,但是花了几个小时试图弄清楚那是什么,我已经束手无策了。请帮忙!!谢谢。

这是我的代码:

Public Sub DeregisterInstanceFromLoadBalance(ByVal strServerID As String)
    Dim strURL As String
    strURL = "http://elasticloadbalancing.amazonaws.com/?"
    Dim strTimestamp As String = PercentEncodeRfc3986(DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"))

    Dim strParams As String
    strParams = "AWSAccessKeyId=<MY_API_KEY>" & _
    "&Action=DeregisterInstancesFromLoadBalancer" & _
    "&Instances.member.N=" & strServerID & _
    "&LoadBalancerName=ATTB" & _
    "&SignatureMethod=HmacSHA256" & _
    "&SignatureVersion=2" & _
    "&Timestamp=" & strTimestamp & _
    "&Version=2009-05-15"

    Dim strStringToSign As String = "GET\nelasticloadbalancing.amazonaws.com\n/\n" & strParams

    strURL = strURL & strParams & "&Signature=" & PercentEncodeRfc3986(HashString(strStringToSign))

    Dim wc As New WebClient()
    Dim strResponse As String
    strResponse = wc.DownloadString(strURL)

End Sub


Private Const PRIVATE_KEY As String = "<MY_PRIVATE_KEY>"

Private Function HashString(ByVal StringToHash As String) As String
    Dim Key() As Byte = Encoding.UTF8.GetBytes(PRIVATE_KEY)
    Dim XML() As Byte = Encoding.UTF8.GetBytes(StringToHash)
    Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
    Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
    Return Convert.ToBase64String(HashCode)
End Function


Private Function PercentEncodeRfc3986(ByVal str As String) As String

    str = HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8)
    str = str.Replace("'", "%27").Replace("(", "%28").Replace(")", "%29").Replace("*", "%2A").Replace("!", "%21").Replace("%7e", "~").Replace("+", "%20").Replace("%7E", "~")

    Dim sbuilder As StringBuilder = New StringBuilder(str)
    For i As Int32 = 0 To sbuilder.Length - 1

        If sbuilder(i) = "%" Then
            If (Char.IsLetter(sbuilder(i + 1)) OrElse Char.IsLetter(sbuilder(i + 2))) Then
                sbuilder(i + 1) = Char.ToUpper(sbuilder(i + 1))
                sbuilder(i + 2) = Char.ToUpper(sbuilder(i + 2))
            End If
        End If
    Next
    Return sbuilder.ToString()
End Function

I've tried everything here, but the Amazon Load Balancer API just will not accept my signature.

Whatever I try, I get a "403 SignatureDoesNotMatch" response. I'm probably missing something obvious, but having spent hours trying to figure out what that is, I'm at the end of my tether. Please help!! Thanks.

Here's my code:

Public Sub DeregisterInstanceFromLoadBalance(ByVal strServerID As String)
    Dim strURL As String
    strURL = "http://elasticloadbalancing.amazonaws.com/?"
    Dim strTimestamp As String = PercentEncodeRfc3986(DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"))

    Dim strParams As String
    strParams = "AWSAccessKeyId=<MY_API_KEY>" & _
    "&Action=DeregisterInstancesFromLoadBalancer" & _
    "&Instances.member.N=" & strServerID & _
    "&LoadBalancerName=ATTB" & _
    "&SignatureMethod=HmacSHA256" & _
    "&SignatureVersion=2" & _
    "&Timestamp=" & strTimestamp & _
    "&Version=2009-05-15"

    Dim strStringToSign As String = "GET\nelasticloadbalancing.amazonaws.com\n/\n" & strParams

    strURL = strURL & strParams & "&Signature=" & PercentEncodeRfc3986(HashString(strStringToSign))

    Dim wc As New WebClient()
    Dim strResponse As String
    strResponse = wc.DownloadString(strURL)

End Sub


Private Const PRIVATE_KEY As String = "<MY_PRIVATE_KEY>"

Private Function HashString(ByVal StringToHash As String) As String
    Dim Key() As Byte = Encoding.UTF8.GetBytes(PRIVATE_KEY)
    Dim XML() As Byte = Encoding.UTF8.GetBytes(StringToHash)
    Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
    Dim HashCode As Byte() = myHMACSHA256.ComputeHash(XML)
    Return Convert.ToBase64String(HashCode)
End Function


Private Function PercentEncodeRfc3986(ByVal str As String) As String

    str = HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8)
    str = str.Replace("'", "%27").Replace("(", "%28").Replace(")", "%29").Replace("*", "%2A").Replace("!", "%21").Replace("%7e", "~").Replace("+", "%20").Replace("%7E", "~")

    Dim sbuilder As StringBuilder = New StringBuilder(str)
    For i As Int32 = 0 To sbuilder.Length - 1

        If sbuilder(i) = "%" Then
            If (Char.IsLetter(sbuilder(i + 1)) OrElse Char.IsLetter(sbuilder(i + 2))) Then
                sbuilder(i + 1) = Char.ToUpper(sbuilder(i + 1))
                sbuilder(i + 2) = Char.ToUpper(sbuilder(i + 2))
            End If
        End If
    Next
    Return sbuilder.ToString()
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2024-11-06 21:37:33

好的,我已经了解了这一点。有一些问题;尤其是亚马逊的文档。

  • 在 VB.Net 中,我应该使用 ControlChars.Lf 而不是 "\n"
  • 文档中的 Instances.member.N 参数
    错了,应该是
    Instances.member.[N].InstanceId,其中 [N] 是实例索引,
    1开始。这是返回
    相当令人困惑的错误消息
    “负载均衡器未找到”。
  • 正确的域名是 elasticloadbalancing.[availability_zone].amazonaws.com;文档中使用的示例也没有使这一点特别清楚。 (尽管域使用情况已在其他地方记录)

Ok, I got to the bottom of this. There were a few problems; not least Amazon's documentation.

  • In VB.Net, I should have been using ControlChars.Lf rather than "\n"
  • The Instances.member.N parameter as documented
    is just wrong, it should be
    Instances.member.[N].InstanceId, where [N] is the instance index,
    starting at 1. This was returning
    the rather confusing error message
    "LoadBalancerNotFound".
  • The correct domain is elasticloadbalancing.[availability_zone].amazonaws.com; the examples used in the docs don't make this particularly clear either. (although domain usage is documented elsewhere)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文