0
我具有以下調用的OAuth類的GenerateSignature方法代碼WCF RESTful服務OAuthentication產生兩個不同的散列值
Uri u = new Uri(urlToCall);
string url, param;
HttpWebRequest request = WebRequest.Create("http://localhost/RESTFUL_DEMO.Web/services/Calc.svc/dowork") as HttpWebRequest;
string consumerKey = "test";
string consumerSecret = "Jenish";
var oAuth = new OAuthBase();
var nonce = oAuth.GenerateNonce();
var timestamp = oAuth.GenerateTimeStamp();
var signature = oAuth.GenerateSignature(u,consumerKey,consumerSecret,string.Empty,string.Empty,"POST",timestamp,nonce,OAuthBase.SignatureTypes.HMACSHA1,out url, out param);
WebRequest request = WebRequest.Create(string.Format("{0}?{1}&oauth_signature={2}", urlToCall, param, signature));
request.Method = "POST";
Enroll a = new Enroll("2");
XmlSerializer ser = new XmlSerializer(a.GetType());
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, a);
byte[] bytes = ms.ToArray();
request.ContentType = "text/xml";
request.Timeout = 30000;
request.ContentLength = bytes.Length;
var requeststream = request.GetRequestStream();
requeststream.Write(bytes, 0, bytes.Length);
requeststream.Close();
WebResponse response = request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
我已經創建以下在我的服務身份驗證方法,該方法從傳入的請求生成簽名
private static bool Authenticate(IncomingWebRequestContext context)
{
bool Authenticated = false;
string normalizedUrl;
string normalizedRequestParameters;
//context.Headers
NameValueCollection pa = context.UriTemplateMatch.QueryParameters;
if (pa != null && pa["oauth_consumer_key"] != null)
{
// to get uri without oauth parameters
string uri = context.UriTemplateMatch.RequestUri.OriginalString.Replace
(context.UriTemplateMatch.RequestUri.Query, "");
string consumersecret = "Jenish";
OAuthBase oauth = new OAuthBase();
string hash = oauth.GenerateSignature(
new Uri(uri),
pa["oauth_consumer_key"],
consumersecret,
null, // totken
null, //token secret
"POST",
pa["oauth_timestamp"],
pa["oauth_nonce"],
out normalizedUrl,
out normalizedRequestParameters
);
Authenticated = pa["oauth_signature"] == hash;
}
return Authenticated;
}
Above Authenticate方法生成的簽名與傳入的請求參數pa [「oauth_signature」]不匹配。任何人都可以請幫我解決問題,讓我知道爲什麼兩個不同的簽名生成。