我正在嘗試爲正在開發的新的tent.io協議創建一個客戶端,並且它們使用http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01所述的HTTP MAC Oauth2方案。使用C#的HTTP MAC身份驗證
我已經寫了一個簡單的方法在C#中創建授權標題,但是當我提交我的請求時,我得到一個簡單的「無效的MAC簽名」錯誤。
由於我沒有參考實現,我正在努力弄清楚我的代碼有什麼問題。我在這裏發佈它,希望有人能夠發現我的錯誤。
public string GetAuthorizationHeader(string macKeyIdentifier, string macKey, string macAlgorithm, string method, Uri uri)
{
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
string timestamp = ((int)t.TotalSeconds).ToString();
string nonce = new Random().Next().ToString();
string normalizedString = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n\n",
timestamp,
nonce,
method,
uri.PathAndQuery,
uri.Host,
uri.Port);
HashAlgorithm hashGenerator = null;
if (macAlgorithm == "hmac-sha-256")
{
hashGenerator = new HMACSHA256(Encoding.ASCII.GetBytes(macKey));
}
else if (macAlgorithm == "hmac-sha-1")
{
hashGenerator = new HMACSHA1(Encoding.ASCII.GetBytes(macKey));
}
else
{
throw new InvalidOperationException("Unsupported MAC algorithm");
}
string hash = System.Convert.ToBase64String(hashGenerator.ComputeHash(Encoding.ASCII.GetBytes(normalizedString)));
StringBuilder authorizationHeader = new StringBuilder();
authorizationHeader.AppendFormat(@"id=""{0}"",ts=""{1}"",nonce=""{2}"",mac=""{3}""",
macKeyIdentifier, timestamp, nonce, hash);
return authorizationHeader.ToString();
}
我創建使用返回值的完整標題和它看起來艾克這個
授權:MAC ID = 「A:dfsdfa2」,TS = 「1349277638」,現時= 「1469030797」,MAC =「ibZ/HXaoz2VgBer3CK7K9vu0po3K + E36K + TQ9Sgcw6o =」
我確定我錯過了一些小東西,但我看不到它。
任何幫助將非常感謝!
您需要重新格式化您的代碼 –