2013-05-02 65 views
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」]不匹配。任何人都可以請幫我解決問題,讓我知道爲什麼兩個不同的簽名生成。

回答

1

我試圖通過考慮您上面的代碼複製你的錯誤,並發現從

context.UriTemplateMatch.RequestUri.OriginalString 

在身份驗證方法生成的URI得到確實產生完全合格的計算機名稱,而不是本地主機的URI。所以在你的wcf消費者端HttpWebRequest請求URI應包括完全合格的機器名稱以及域名。

例如, 如果您有機器名Jenish-PC和訪問abc.efg.com那麼URI應該像

http://Jenish-pc.abc.efg.com/[rest of your service path] 

,而不是

http://localhost/....