コード例 #1
0
        /// <summary>
        /// This method is meant for servers to validate incoming OAuth requests from MySpace typically made through an OpenSocial makeRequest call, or an iFrame src attribute. If the return value is true, it means that the signature contained in the request matches the actual request that was signed with the correct consumer secret for that consumer key.
        /// </summary>
        /// <param name="apiServerUri">Your server which is being requested by MySpace(e.g. http://localhost:9090/ or http://myserver.com/).</param>
        /// <param name="resourcePath">The relative path of the resource being requested.</param>
        /// <param name="httpRequest">The request</param>
        /// <param name="accessTokenSecret">The access token of the particular request. This will be empty for onsite apps and iFrame src attributes.</param>
        /// <returns>True if the signature in the request matches correctly.</returns>
        public bool ValidateSignature(string apiServerUri, string resourcePath, HttpRequest httpRequest, string accessTokenSecret)
        {
            OAuthParameter oAuthParameter = null;

            try
            {
                oAuthParameter = OAuthParameter.FromHttpContext(httpRequest.Headers, httpRequest.QueryString);
            }
            catch (ArgumentNullException)
            {
                var x = 1;
            }
            catch
            {
                var y = 2;
            }

            if (oAuthParameter == null)
            {
                //TODO: change to problem reporting
                throw new ArgumentException("no oauth parameters found");
            }

            if (oAuthParameter.HasError)
            {
                return(false);
            }

            var oAuthSigner         = new OAuthSigner();
            var signatureMethodType = GeneralUtil.StringToSignatureMethodType(oAuthParameter.SignatureMethod);
            var signatureMethod     = oAuthSigner.GetSignatureMethod(signatureMethodType);

            signatureMethod.RequestParameters.Add(oAuthParameter.ToCollection());
            signatureMethod.RequestParameters.Add(oAuthParameter.UnknownParameterCollection);

            //if (string.IsNullOrEmpty(oAuthParameter.Token))
            //{
            //    signatureMethod.RequestParameters.Add(OAuthParameter.OAUTH_TOKEN, string.Empty);
            //}

            var request = WebRequest.Create(apiServerUri + resourcePath);

            request.Method = httpRequest.HttpMethod;

            var consumer  = new OAuthConsumer(apiServerUri, oAuthParameter.ConsumerKey, consumerSecret);
            var token     = new ConsumerToken(consumer, oAuthParameter.Token, accessTokenSecret);
            var signature = signatureMethod.BuildSignature(request, consumer, token);

            return(oAuthParameter.Signature == signature);
        }