本文整理汇总了C#中SortedDictionary.ForEach方法的典型用法代码示例。如果您正苦于以下问题:C# SortedDictionary.ForEach方法的具体用法?C# SortedDictionary.ForEach怎么用?C# SortedDictionary.ForEach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SortedDictionary
的用法示例。
在下文中一共展示了SortedDictionary.ForEach方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenerateAuthHeader
private string GenerateAuthHeader(OAuthState oAuthState, HttpRequestMessage request)
{
SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>();
sortedDictionary.Add(Constants.OAuthNonce, OAuthUtil.PercentEncode(oAuthState.Nonce));
sortedDictionary.Add(Constants.OAuthSignatureMethod, OAuthUtil.PercentEncode(oAuthState.SignatureMethod));
sortedDictionary.Add(Constants.OAuthTimestamp, OAuthUtil.PercentEncode(oAuthState.Timestamp));
sortedDictionary.Add(Constants.OAuthConsumerKey, OAuthUtil.PercentEncode(oAuthState.Credential.ConsumerKey));
sortedDictionary.Add(Constants.OAuthVersion, OAuthUtil.PercentEncode(oAuthState.Version));
if (!string.IsNullOrEmpty(_oAuthState.Credential.Token))
sortedDictionary.Add(Constants.OAuthToken, OAuthUtil.PercentEncode(oAuthState.Credential.Token));
//don't encode it here again.
//we already did that and this auth header field doesn't require it to be encoded twice
if (!string.IsNullOrEmpty(_oAuthState.Credential.CallbackUrl))
sortedDictionary.Add(Constants.OAuthCallback, oAuthState.Credential.CallbackUrl);
StringBuilder strBuilder = new StringBuilder();
var valueFormat = "{0}=\"{1}\",";
sortedDictionary.ForEach(x => {
strBuilder.AppendFormat(valueFormat, x.Key, x.Value);
});
//oAuth parameters has to be sorted before sending, but signature has to be at the end of the authorization request
//http://stackoverflow.com/questions/5591240/acquire-twitter-request-token-failed
strBuilder.AppendFormat(valueFormat, Constants.OAuthSignature, OAuthUtil.PercentEncode(GenerateSignature(oAuthState, request)));
return strBuilder.ToString().TrimEnd(',');
}
示例2: GenerateSignature
private string GenerateSignature(OAuthState oAuthState, HttpRequestMessage request)
{
//https://dev.twitter.com/docs/auth/creating-signature
//http://garyshortblog.wordpress.com/2011/02/11/a-twitter-oauth-example-in-c/
//This dictionary will hold the twice-encoded values
SortedDictionary<string, string> signatureBaseCollection = new SortedDictionary<string, string>();
//Required for all requests
signatureBaseCollection.Add(Constants.OAuthConsumerKey, oAuthState.Credential.ConsumerKey);
signatureBaseCollection.Add(Constants.OAuthNonce, oAuthState.Nonce);
signatureBaseCollection.Add(Constants.OAuthVersion, oAuthState.Version);
signatureBaseCollection.Add(Constants.OAuthTimestamp, oAuthState.Timestamp);
signatureBaseCollection.Add(Constants.OAuthSignatureMethod, oAuthState.SignatureMethod);
//Parameters
if (oAuthState.Parameters != null) {
//these are already encoded. At the string building phase,
//they will be encoded one more time.
oAuthState.Parameters.ForEach(x => signatureBaseCollection.Add(x.Key, x.Value));
}
//Optionals
if (!string.IsNullOrEmpty(oAuthState.Credential.Token))
signatureBaseCollection.Add(Constants.OAuthToken, oAuthState.Credential.Token);
//this needs to be encoded twice. So, we leave it as it is to be encode one more time
if (!string.IsNullOrEmpty(oAuthState.Credential.CallbackUrl))
signatureBaseCollection.Add(Constants.OAuthCallback, oAuthState.Credential.CallbackUrl);
//Build the signature
StringBuilder strBuilder = new StringBuilder();
//these two ampersand chars needs not to be encoded
strBuilder.AppendFormat("{0}&", request.Method.Method.ToUpper());
strBuilder.AppendFormat("{0}&", OAuthUtil.PercentEncode(request.RequestUri.ToString()));
//encode the values for signature base
signatureBaseCollection.ForEach(x =>
strBuilder.Append(
OAuthUtil.PercentEncode(string.Format("{0}={1}&", x.Key, x.Value))
)
);
//Remove the trailing ambersand char from the signatureBase.
//Remember, it's been urlEncoded so you have to remove the
//last 3 chars - %26
string baseSignatureString = strBuilder.ToString();
baseSignatureString = baseSignatureString.Substring(0, baseSignatureString.Length - 3);
//Build the signing key
string signingKey = string.Format(
"{0}&{1}", OAuthUtil.PercentEncode(oAuthState.SignatureEntity.ConsumerSecret),
string.IsNullOrEmpty(oAuthState.SignatureEntity.TokenSecret) ? "" : OAuthUtil.PercentEncode(oAuthState.SignatureEntity.TokenSecret)
);
//Sign the request
using (HMACSHA1 hashAlgorithm = new HMACSHA1(new ASCIIEncoding().GetBytes(signingKey))) {
return Convert.ToBase64String(
hashAlgorithm.ComputeHash(
new ASCIIEncoding().GetBytes(baseSignatureString)
)
);
}
}