CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

How to Encode for OAuth

387 Views
Copy Code Show/Hide Line Numbers
   1:          /// <summary>
   2:          /// URL-encodes a string for use with OAuth.
   3:          /// </summary>
   4:          /// <param name="toEncode">
   5:          /// The string to encode.
   6:          /// </param>
   7:          /// <returns>
   8:          /// A URL-encoded string where all of the encoded characters are upper-case.
   9:          /// </returns>
  10:          /// <remarks>
  11:          /// This is a different URL-encoding implementation since the default
  12:          /// .NET one outputs the percent encoding in lower case. While this is
  13:          /// not a problem with the percent encoding spec, it is used in
  14:          /// upper-case throughout OAuth (http://oauth.net/core/1.0a#encoding_parameters).
  15:          /// </remarks>
  16:          /// <exception cref="System.ArgumentNullException">
  17:          /// Thrown if <paramref name="toEncode" /> is <see langword="null" />.
  18:          /// </exception>
  19:          public static string UrlEncodeForOAuth(this string toEncode)
  20:          {
  21:              if (toEncode == null)
  22:              {
  23:                  throw new ArgumentNullException("toEncode");
  24:              }
  25:              StringBuilder result = new StringBuilder();
  26:              foreach (char symbol in toEncode)
  27:              {
  28:                  if (SafeUrlCharacters.IndexOf(symbol) != -1)
  29:                  {
  30:                      result.Append(symbol);
  31:                  }
  32:                  else
  33:                  {
  34:                      result.AppendFormat("%{0:X2}", (int)symbol);
  35:                  }
  36:              }
  37:              return result.ToString();
  38:          }
by Mehul Harry
  December 15, 2009 @ 3:01pm
Tags:
Description:
This is an extension method for encoding a string per OAuth 1.0 spec by Travis Illig!

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate