Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 9db19273f2
Fetching contributors…

Cannot retrieve contributors at this time

executable file 127 lines (113 sloc) 3.602 kb
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OAuth
{
public class OAuthBase
{
public OAuthBase(string debugType)
{
this._debugType = debugType;
}
#region Debug
protected string _debugType;
protected void _debug(string message)
{
if (this._debugType == "console")
{
Console.WriteLine(message);
}
else
{
Debug.WriteLine(message);
}
}
#endregion
#region Url Utilities
protected static string unreservedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
/// <summary>
/// Url Encode implementation for OAuth
/// </summary>
/// <param name="value">The value to Url encode</param>
/// <returns>Returns a Url encoded string</returns>
public static string UrlEncode(string value)
{
if (value == null || value == String.Empty || value.Length == 0) return String.Empty;
StringBuilder result = new StringBuilder();
foreach (char symbol in value)
{
if (unreservedChars.IndexOf(symbol) != -1)
{
result.Append(symbol);
}
else
{
result.Append('%' + String.Format("{0:X2}", (int)symbol));
}
}
return result.ToString();
}
/// <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();
}
#endregion
}
#region IComparer<QueryParameter> Members
class QueryParameterComparer : IComparer<QueryParameter>
{
public int Compare(QueryParameter x, QueryParameter y)
{
if (x.Name == y.Name)
{
return string.Compare(x.Value, y.Value);
}
else
{
return string.Compare(x.Name, y.Name);
}
}
}
#endregion
#region Class QueryParameter
/// <summary>
/// Provides an internal structure to sort the query parameter
/// </summary>
public class QueryParameter
{
private string name = null;
private string value = null;
public QueryParameter(string name, string value)
{
this.name = name;
this.value = value;
}
public string Name
{
get { return name; }
}
public string Value
{
get { return value; }
}
}
#endregion
}
Jump to Line
Something went wrong with that request. Please try again.