private UrlEncode ( string value ) : string | ||
value | string | The value to Url encode |
return | string |
/// <summary> /// Normalizes the request parameters according to the OAuth spec /// </summary> /// <param name="parameters">The list of parameters already sorted</param> /// <returns>a string representing the normalized parameters</returns> public string NormalizeRequestParameters(IList <QueryParameter> parameters) { StringBuilder sb = new StringBuilder(); QueryParameter p = null; for (int i = 0; i < parameters.Count; i++) { p = parameters[i]; sb.AppendFormat("{0}={1}", p.Name, OAuthBase.UrlEncode(p.Value)); if (i < parameters.Count - 1) { sb.Append("&"); } } return(sb.ToString()); }
/// <summary> /// Normalizes the query parameters by sorting them according to the OAuth specification /// Generates the signature base /// </summary> /// <param name="url">the base url of the service called</param> /// <param name="parameters">the list of query parameters passed</param> /// <param name="consumerSecret">consumer secret</param> /// <param name="normalizedUrl">the normalized Url (returned)</param> /// <param name="normalizedRequestParameters">the normalized parameters (returned)</param> /// <returns>a string to be used as the request signature base</returns> private string _generateSignatureBase(Uri url, string httpMethod, List <QueryParameter> parameters, string consumerSecret, out string normalizedUrl, out string normalizedRequestParameters) { normalizedUrl = null; normalizedRequestParameters = null; // Sort the parameters parameters.Sort(new QueryParameterComparer()); normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host); if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443))) { normalizedUrl += ":" + url.Port; } normalizedUrl += url.AbsolutePath; normalizedRequestParameters = base.NormalizeRequestParameters(parameters); StringBuilder signatureBase = new StringBuilder(); signatureBase.AppendFormat("{0}&", httpMethod); signatureBase.AppendFormat("{0}&", OAuthBase.UrlEncode(normalizedUrl)); signatureBase.AppendFormat("{0}", OAuthBase.UrlEncode(normalizedRequestParameters)); return(signatureBase.ToString()); }
private string generateSignature(OAuthBase oauthBase, OAuthRequestViewModel vm, string sharedSecret) { var url = new StringBuilder(vm.TargetUrl); //if there were no querystring parameters and the last character of the url isn't a ?, add one //otherwise add an ampersand url.Append(url[url.Length - 1] != '?' ? '?' : '&'); #region append all properties to query string url.AppendFormat("{0}={1}&", "context_id", string.IsNullOrEmpty(vm.ContextId) ? null : oauthBase.UrlEncode(vm.ContextId)); url.AppendFormat("{0}={1}&", "context_label", string.IsNullOrEmpty(vm.ContextLabel) ? null : oauthBase.UrlEncode(vm.ContextLabel)); url.AppendFormat("{0}={1}&", "context_title", string.IsNullOrEmpty(vm.ContextTitle) ? null : oauthBase.UrlEncode(vm.ContextTitle)); url.AppendFormat("{0}={1}&", "context_type", string.IsNullOrEmpty(vm.ContextType) ? null : oauthBase.UrlEncode(vm.ContextType)); url.AppendFormat("{0}={1}&", "ext_lms", string.IsNullOrEmpty(vm.ExtLms) ? null : oauthBase.UrlEncode(vm.ExtLms)); url.AppendFormat("{0}={1}&", "launch_presentation_document_target", string.IsNullOrEmpty(vm.LaunchPresentationDocumentTarget) ? null : oauthBase.UrlEncode(vm.LaunchPresentationDocumentTarget)); url.AppendFormat("{0}={1}&", "launch_presentation_locale", string.IsNullOrEmpty(vm.LaunchPresentationLocale) ? null : oauthBase.UrlEncode(vm.LaunchPresentationLocale)); url.AppendFormat("{0}={1}&", "launch_presentation_return_url", string.IsNullOrEmpty(vm.LaunchPresentationReturnUrl) ? null : oauthBase.UrlEncode(vm.LaunchPresentationReturnUrl)); url.AppendFormat("{0}={1}&", "lis_course_offering_sourcedid", string.IsNullOrEmpty(vm.LisCourseOfferingSourcedid) ? null : oauthBase.UrlEncode(vm.LisCourseOfferingSourcedid)); url.AppendFormat("{0}={1}&", "lis_course_section_sourcedid", string.IsNullOrEmpty(vm.LisCourseSectionSourcedid) ? null : oauthBase.UrlEncode(vm.LisCourseSectionSourcedid)); url.AppendFormat("{0}={1}&", "lis_person_contact_email_primary", string.IsNullOrEmpty(vm.LisPersonContactEmailPrimary) ? null : oauthBase.UrlEncode(vm.LisPersonContactEmailPrimary)); url.AppendFormat("{0}={1}&", "lis_person_name_family", string.IsNullOrEmpty(vm.LisPersonNameFamily) ? null : oauthBase.UrlEncode(vm.LisPersonNameFamily)); url.AppendFormat("{0}={1}&", "lis_person_name_full", string.IsNullOrEmpty(vm.LisPersonNameFull) ? null : oauthBase.UrlEncode(vm.LisPersonNameFull)); url.AppendFormat("{0}={1}&", "lis_person_name_given", string.IsNullOrEmpty(vm.LisPersonNameGiven) ? null : oauthBase.UrlEncode(vm.LisPersonNameGiven)); url.AppendFormat("{0}={1}&", "lti_message_type", string.IsNullOrEmpty(vm.LtiMessageType) ? null : oauthBase.UrlEncode(vm.LtiMessageType)); url.AppendFormat("{0}={1}&", "lti_version", string.IsNullOrEmpty(vm.LtiVersion) ? null : oauthBase.UrlEncode(vm.LtiVersion)); url.AppendFormat("{0}={1}&", "oauth_callback", string.IsNullOrEmpty(vm.OauthCallback) ? null : oauthBase.UrlEncode(vm.OauthCallback)); url.AppendFormat("{0}={1}&", "oauth_consumer_key", string.IsNullOrEmpty(vm.OauthConsumerKey) ? null : oauthBase.UrlEncode(vm.OauthConsumerKey)); url.AppendFormat("{0}={1}&", "oauth_nonce", string.IsNullOrEmpty(vm.OauthNonce) ? null : oauthBase.UrlEncode(vm.OauthNonce)); url.AppendFormat("{0}={1}&", "oauth_signature", string.IsNullOrEmpty(vm.OauthSignature) ? null : oauthBase.UrlEncode(vm.OauthSignature)); url.AppendFormat("{0}={1}&", "oauth_signature_method", string.IsNullOrEmpty(vm.OauthSignatureMethod) ? null : oauthBase.UrlEncode(vm.OauthSignatureMethod)); url.AppendFormat("{0}={1}&", "oauth_timestamp", string.IsNullOrEmpty(vm.OauthTimestamp) ? null : oauthBase.UrlEncode(vm.OauthTimestamp)); url.AppendFormat("{0}={1}&", "oauth_version", string.IsNullOrEmpty(vm.OauthVersion) ? null : oauthBase.UrlEncode(vm.OauthVersion)); url.AppendFormat("{0}={1}&", "resource_link_id", string.IsNullOrEmpty(vm.ResourceLinkId) ? null : oauthBase.UrlEncode(vm.ResourceLinkId)); url.AppendFormat("{0}={1}&", "resource_link_title", string.IsNullOrEmpty(vm.ResourceLinkTitle) ? null : oauthBase.UrlEncode(vm.ResourceLinkTitle)); url.AppendFormat("{0}={1}&", "tool_consumer_info_product_family_code", string.IsNullOrEmpty(vm.ToolConsumerInfoProductFamilyCode) ? null : oauthBase.UrlEncode(vm.ToolConsumerInfoProductFamilyCode)); url.AppendFormat("{0}={1}&", "tool_consumer_info_version", string.IsNullOrEmpty(vm.ToolConsumerInfoVersion) ? null : oauthBase.UrlEncode(vm.ToolConsumerInfoVersion)); url.AppendFormat("{0}={1}&", "tool_consumer_instance_description", string.IsNullOrEmpty(vm.ToolConsumerInstanceDescription) ? null : oauthBase.UrlEncode(vm.ToolConsumerInstanceDescription)); url.AppendFormat("{0}={1}&", "tool_consumer_instance_guid", string.IsNullOrEmpty(vm.ToolConsumerInstanceGuid) ? null : oauthBase.UrlEncode(vm.ToolConsumerInstanceGuid)); url.AppendFormat("{0}={1}&", "tool_consumer_instance_name", string.IsNullOrEmpty(vm.ToolConsumerInstanceName) ? null : oauthBase.UrlEncode(vm.ToolConsumerInstanceName)); url.AppendFormat("{0}={1}&", "tool_consumer_instance_url", string.IsNullOrEmpty(vm.ToolConsumerInstanceUrl) ? null : oauthBase.UrlEncode(vm.ToolConsumerInstanceUrl)); url.AppendFormat("{0}={1}&", "user_id", string.IsNullOrEmpty(vm.UserId) ? null : oauthBase.UrlEncode(vm.UserId)); #endregion string normalizedUrl; string normalizedRequestParameters; return oauthBase.GenerateSignature(new Uri(url.ToString()), vm.OauthConsumerKey, sharedSecret, null, null, "POST", vm.OauthTimestamp, vm.OauthNonce, out normalizedUrl, out normalizedRequestParameters); }
public void GenerateSignature() { OAuthBase.SignatureTypes signatureType = OAuthBase.SignatureTypes.HMACSHA1; string normalizedUrl = null; string normalizedRequestParameters = null; var myOAuth = new OAuthBase(); try { var uri = new Uri(_view.Uri); string consumerKey = _view.ConsumerKey; string consumerSecret = _view.ConsumerSecret; string token = _view.Token; string tokenSecret = _view.TokenSecret; string httpMethod = _view.HttpMethod; string timeStamp = _view.TimeStamp; string nonce = _view.Nonce; if (String.IsNullOrEmpty(timeStamp)) { timeStamp = myOAuth.GenerateTimeStamp(); _view.TimeStamp = timeStamp; } if (String.IsNullOrEmpty(nonce)) { nonce = myOAuth.GenerateNonce(); _view.Nonce = nonce; } switch (_view.SignatureMethod) { case 0: signatureType = OAuthBase.SignatureTypes.HMACSHA1; break; case 1: signatureType = OAuthBase.SignatureTypes.PLAINTEXT; break; case 2: signatureType = OAuthBase.SignatureTypes.RSASHA1; break; } myOAuth.includeVersion = _view.IncludeVersion; string signature = ""; if (httpMethod == "POST") { Dictionary<string, string> dictionary = new OAuthPostRequest().GetFormVariables(_view.PostData); signature = myOAuth.GenerateSignature(uri, consumerKey, consumerSecret, token, tokenSecret, httpMethod, timeStamp, nonce, signatureType, out normalizedUrl, out normalizedRequestParameters, dictionary); } else { signature = myOAuth.GenerateSignature(uri, consumerKey, consumerSecret, token, tokenSecret, httpMethod, timeStamp, nonce, signatureType, out normalizedUrl, out normalizedRequestParameters, null); } _view.RawSignature = signature; _view.EncodedSignature = myOAuth.UrlEncode(signature); _view.GeneratedUrl = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + _view.EncodedSignature; } catch (Exception exception) { MessageBox.Show(exception.Message); } }