本文整理汇总了C#中OAuthSignatureMethod类的典型用法代码示例。如果您正苦于以下问题:C# OAuthSignatureMethod类的具体用法?C# OAuthSignatureMethod怎么用?C# OAuthSignatureMethod使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OAuthSignatureMethod类属于命名空间,在下文中一共展示了OAuthSignatureMethod类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ForRequestToken
public static OAuth1Authenticator ForRequestToken(string consumerKey, string consumerSecret, OAuthSignatureMethod signatureMethod = OAuthSignatureMethod.HmacSha1)
{
OAuth1Authenticator authenticator = new OAuth1Authenticator
{
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
SignatureMethod = signatureMethod,
SignatureTreatment = OAuthSignatureTreatment.Escaped,
ConsumerKey = consumerKey,
ConsumerSecret = consumerSecret,
Type = OAuthType.RequestToken
};
return authenticator;
}
示例2: GetSignature
/// <summary>
/// Creates a signature value given a signature base and the consumer secret and a known token secret.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2"/>
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureTreatment">The treatment to use on a signature value</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod, OAuthSignatureTreatment signatureTreatment,
string signatureBase, string consumerSecret, string tokenSecret)
{
if (tokenSecret.IsNullOrBlank())
{
tokenSecret = string.Empty;
}
consumerSecret = UrlEncodeRelaxed(consumerSecret);
tokenSecret = UrlEncodeRelaxed(tokenSecret);
string signature;
switch (signatureMethod)
{
case OAuthSignatureMethod.HmacSha1:
{
#if !WINDOWS_UWP
HMACSHA1 crypto = new HMACSHA1();
string key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
crypto.Key = encoding.GetBytes(key);
signature = signatureBase.HashWith(crypto);
#else
signature = signatureBase.HashWith(HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1));
#endif
break;
}
case OAuthSignatureMethod.HmacSha256:
{
HMACSHA256 crypto = new HMACSHA256();
string key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
crypto.Key = encoding.GetBytes(key);
signature = signatureBase.HashWith(crypto);
break;
}
case OAuthSignatureMethod.PlainText:
{
signature = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
break;
}
default:
throw new NotImplementedException("Only HMAC-SHA1 and HMAC-SHA256 are currently supported.");
}
string result = signatureTreatment == OAuthSignatureTreatment.Escaped
? UrlEncodeRelaxed(signature)
: signature;
return result;
}
示例3: ForProtectedResource
public static OAuth1Authenticator ForProtectedResource(string consumerKey, string consumerSecret,
string accessToken, string accessTokenSecret, OAuthSignatureMethod signatureMethod = OAuthSignatureMethod.HmacSha1)
{
OAuth1Authenticator authenticator = new OAuth1Authenticator
{
Type = OAuthType.ProtectedResource,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
SignatureMethod = signatureMethod,
SignatureTreatment = OAuthSignatureTreatment.Escaped,
ConsumerKey = consumerKey,
ConsumerSecret = consumerSecret,
Token = accessToken,
TokenSecret = accessTokenSecret
};
return authenticator;
}
示例4: GetSignature
/// <summary>
/// Creates a signature value given a signature base and the consumer secret and a known token secret.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2"/>
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureTreatment">The treatment to use on a signature value</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod,
OAuthSignatureTreatment signatureTreatment,
string signatureBase,
string consumerSecret,
string tokenSecret)
{
if (tokenSecret.IsNullOrBlank())
{
tokenSecret = String.Empty;
}
consumerSecret = UrlEncodeRelaxed(consumerSecret);
tokenSecret = UrlEncodeRelaxed(tokenSecret);
string signature;
switch (signatureMethod)
{
#if !PocketPC
case OAuthSignatureMethod.HmacSha1:
{
var crypto = new HMACSHA1();
var key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
crypto.Key = _encoding.GetBytes(key);
signature = signatureBase.HashWith(crypto);
break;
}
#endif
case OAuthSignatureMethod.PlainText:
{
signature = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
break;
}
default:
#if PocketPC
throw new NotImplementedException("Only PlainText is currently supported.");
#else
throw new NotImplementedException("Only HMAC-SHA1 is currently supported.");
#endif
}
var result = signatureTreatment == OAuthSignatureTreatment.Escaped
? UrlEncodeRelaxed(signature)
: signature;
return result;
}
示例5: ForClientAuthentication
public static OAuth1Authenticator ForClientAuthentication(string consumerKey, string consumerSecret,
string username, string password, OAuthSignatureMethod signatureMethod = OAuthSignatureMethod.HmacSha1)
{
OAuth1Authenticator authenticator = new OAuth1Authenticator
{
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
SignatureMethod = signatureMethod,
SignatureTreatment = OAuthSignatureTreatment.Escaped,
ConsumerKey = consumerKey,
ConsumerSecret = consumerSecret,
ClientUsername = username,
ClientPassword = password,
Type = OAuthType.ClientAuthentication
};
return authenticator;
}
示例6: GetSignature
private string GetSignature(
Uri uri, string consumerSecret, string tokenSecret,
string joinedParam, OAuthSignatureMethod sigMethod,
string requestMethod)
{
switch (sigMethod)
{
case OAuthSignatureMethod.PlainText:
return Util.HttpUtility.UrlEncode(consumerSecret + "&" + tokenSecret);
case OAuthSignatureMethod.Hmac_Sha1:
if (String.IsNullOrEmpty(requestMethod))
throw new ArgumentNullException("requestMethod");
// formatting URI
var regularUrl = uri.Scheme + "://" + uri.Host;
if (!((uri.Scheme == "http" && uri.Port == 80) || (uri.Scheme == "https" && uri.Port == 443)))
regularUrl += ":" + uri.Port;
regularUrl += uri.AbsolutePath;
// Generate signature
StringBuilder SigSource = new StringBuilder();
SigSource.Append(UrlEncode(requestMethod.ToUpper(), Encoding.UTF8, true) + "&");
SigSource.Append(UrlEncode(regularUrl, Encoding.UTF8, true) + "&");
SigSource.Append(UrlEncode(joinedParam, Encoding.UTF8, true));
// Calcuate hash
using (HMACSHA1 hmacsha1 = new HMACSHA1())
{
hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret, Encoding.UTF8, true), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret, Encoding.UTF8, true)));
return UrlEncode(ComputeHash(hmacsha1, SigSource.ToString()), Encoding.UTF8, false);
}
case OAuthSignatureMethod.Rsa_Sha1:
throw new NotImplementedException();
default:
throw new ArgumentException("Unknown signature type", "signatureType");
}
}
示例7: ToRequestValue
public static string ToRequestValue(OAuthSignatureMethod signatureMethod)
{
var value = signatureMethod.ToString().ToUpper();
var shaIndex = value.IndexOf("SHA1");
return shaIndex > -1 ? value.Insert(shaIndex, "-") : value;
}
示例8: GetOAuthParams
private IEnumerable<KeyValuePair<string, string>> GetOAuthParams(
string consumerKey, string token, string timeStamp, string nonce,
OAuthSignatureMethod sigMethod, string verifier, bool gettingRequestToken)
{
if (String.IsNullOrEmpty(consumerKey))
throw new ArgumentNullException("consumerKey");
var np = new List<KeyValuePair<string, string>>();
// original parameter
np.Add(new KeyValuePair<string, string>(VersionKey, Version));
np.Add(new KeyValuePair<string, string>(NonceKey, nonce));
np.Add(new KeyValuePair<string, string>(TimestampKey, timeStamp));
np.Add(new KeyValuePair<string, string>(SignatureMethodKey, GetOAuthSignatureMethodValue(sigMethod)));
np.Add(new KeyValuePair<string, string>(ConsumerKeyKey, consumerKey));
if (!String.IsNullOrEmpty(verifier))
np.Add(new KeyValuePair<string, string>(VerifierKey, verifier));
if (gettingRequestToken)
np.Add(new KeyValuePair<string, string>(CallbackKey, "oob")); // out of band
else if (!String.IsNullOrEmpty(token))
np.Add(new KeyValuePair<string, string>(TokenKey, token));
return np;
}
示例9: GetOAuthSignatureMethodValue
private string GetOAuthSignatureMethodValue(OAuthSignatureMethod sigmethod)
{
switch (sigmethod)
{
case OAuthSignatureMethod.Hmac_Sha1:
return "HMAC-SHA1";
case OAuthSignatureMethod.PlainText:
return "PLAINTEXT";
case OAuthSignatureMethod.Rsa_Sha1:
return "RSA-SHA1";
default:
return null;
}
}
示例10: GetSignature
/// <summary>
/// Creates a signature value given a signature base and the consumer secret and a known token secret.
/// </summary>
/// <seealso href="http://oauth.net/core/1.0#rfc.section.9.2"/>
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureTreatment">The treatment to use on a signature value</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod,
OAuthSignatureTreatment signatureTreatment,
string signatureBase,
string consumerSecret,
string tokenSecret)
{
if (IsNullOrBlank(tokenSecret))
{
tokenSecret = String.Empty;
}
consumerSecret = UrlEncodeRelaxed(consumerSecret);
tokenSecret = UrlEncodeRelaxed(tokenSecret);
string signature;
switch (signatureMethod)
{
case OAuthSignatureMethod.HmacSha1:
{
var key = string.Concat(consumerSecret, "&", tokenSecret);
var crypto = new HMACSHA1(_encoding.GetBytes(key));
signature = HashWith(signatureBase, crypto);
break;
}
default:
throw new NotImplementedException("Only HMAC-SHA1 is currently supported.");
}
var result = signatureTreatment == OAuthSignatureTreatment.Escaped
? UrlEncodeRelaxed(signature)
: signature;
return result;
}
示例11: GetSignature
/// <summary>
/// Creates a signature value given a signature base and the consumer secret and a known token secret.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2"/>
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureTreatment">The treatment to use on a signature value</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod,
OAuthSignatureTreatment signatureTreatment,
string signatureBase,
string consumerSecret,
string tokenSecret)
{
if (tokenSecret.IsNullOrBlank())
{
tokenSecret = String.Empty;
}
consumerSecret = UrlEncodeRelaxed(consumerSecret);
tokenSecret = UrlEncodeRelaxed(tokenSecret);
string signature;
switch (signatureMethod)
{
case OAuthSignatureMethod.HmacSha1:
{
var key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
var keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
var hmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
var macKey = hmacSha1Provider.CreateKey(keyMaterial);
var dataToBeSigned = CryptographicBuffer.ConvertStringToBinary(signatureBase, BinaryStringEncoding.Utf8);
var signatureBuffer = CryptographicEngine.Sign(macKey, dataToBeSigned);
signature = CryptographicBuffer.EncodeToBase64String(signatureBuffer);
break;
}
case OAuthSignatureMethod.PlainText:
{
signature = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
break;
}
default:
throw new NotImplementedException("Only HMAC-SHA1 is currently supported.");
}
var result = signatureTreatment == OAuthSignatureTreatment.Escaped
? UrlEncodeRelaxed(signature)
: signature;
return result;
}
示例12: ToRequestValue
public static string ToRequestValue(OAuthSignatureMethod signatureMethod)
{
switch (signatureMethod) {
case OAuthSignatureMethod.HmacSha256:
return "HMAC-SHA2-256";
case OAuthSignatureMethod.HmacSha1:
return "HMAC-SHA1";
default:
return null;
}
}
示例13: GetSignature
/// <summary>
/// Creates a signature value given a signature base and the consumer secret and a known token secret.
/// </summary>
/// <seealso cref="http://oauth.net/core/1.0#rfc.section.9.2"/>
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod, string signatureBase,
string consumerSecret,
string tokenSecret)
{
if (tokenSecret.IsNullOrBlank())
{
tokenSecret = String.Empty;
}
consumerSecret = UrlEncode(consumerSecret);
tokenSecret = UrlEncode(tokenSecret);
string signature;
switch (signatureMethod)
{
case OAuthSignatureMethod.HmacSha1:
{
var crypto = new HMACSHA1();
var key = "{0}&{1}".FormatWith(consumerSecret, tokenSecret);
crypto.Key = _encoding.GetBytes(key);
signature = signatureBase.HashWith(crypto);
break;
}
default:
throw new NotImplementedException("Only HMAC-SHA1 is currently supported.");
}
return UrlEncode(signature);
}
示例14: GetSignature
/// <summary>
/// Creates a signature value given a signature base and the consumer secret and a known token secret.
/// </summary>
/// <seealso href="http://oauth.net/core/1.0#rfc.section.9.2"/>
/// <param name="signatureMethod">The hashing method</param>
/// <param name="signatureTreatment">The treatment to use on a signature value</param>
/// <param name="signatureBase">The signature base</param>
/// <param name="consumerSecret">The consumer secret</param>
/// <param name="tokenSecret">The token secret</param>
/// <returns></returns>
public static string GetSignature(OAuthSignatureMethod signatureMethod,
OAuthSignatureTreatment signatureTreatment,
string signatureBase,
string consumerSecret,
string tokenSecret)
{
if (IsNullOrBlank(tokenSecret))
{
tokenSecret = String.Empty;
}
consumerSecret = UrlEncodeRelaxed(consumerSecret);
tokenSecret = UrlEncodeRelaxed(tokenSecret);
string signature;
switch (signatureMethod)
{
case OAuthSignatureMethod.HmacSha1:
{
var key = String.Concat(consumerSecret, "&", tokenSecret);
#if NETFX_CORE
var crypto = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1);
var cryptoKey = crypto.CreateKey(CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8));
signature = HashWith(signatureBase, cryptoKey);
#endif
#if !NETFX_CORE
var crypto = new HMACSHA1();
crypto.Key = _encoding.GetBytes(key);
signature = HashWith(signatureBase, crypto);
#endif
break;
}
default:
throw new NotImplementedException("Only HMAC-SHA1 is currently supported.");
}
var result = signatureTreatment == OAuthSignatureTreatment.Escaped
? UrlEncodeRelaxed(signature)
: signature;
return result;
}