WCF Restful service OAuthentication generating two different hash values

I have following code that calls GenerateSignature method of OAuth class

        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());

I have created following Authenticate method in my service that generates signature from incoming request

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 method generates signature that does not match as of incoming request parameter pa["oauth_signature"]. Can anyone please help me resolve the issue and let me know why two different Signature are generated.

Answers


I tried to duplicate your error by considering your above code and found that uri that gets generated from

context.UriTemplateMatch.RequestUri.OriginalString

in Authenticate method does generates Uri of fully qualified machine name instead of localhost. so at your wcf consumer side HttpWebRequest request uri should include fully qualified machine name along with domain name.

For e.g. If you are having machine name Jenish-pc and domain is abc.efg.com then uri should be like

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

instead of

http://localhost/....

Need Your Help

XDocument performance

c# xml

When you create a new XDocument using XDocument.Load, does it open the XMLfile and keep a local copy, or does it continuously read the document from the hard drive? If it does continuously read, is...

Is there any tools for check html quality?

html standards

I know about html, css validation and some rules in pagespeed about css performance.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.