我正在尝试在我的 C# 服务器中验证传入的签名请求。请求来自社交平台(naver)。我从他们那里得到的只是一个证书(下)。
我正在使用 DevDefinedOAuth
http://code.google.com/p/devdefined-tools/
他们有一篇关于这个主题的维基文章。
http://code.google.com/p/devdefined-tools/wiki/OAuthSignatureValidation
这是我的代码:
public static bool isValidRequest2( HttpListenerRequest request )
{
X509Certificate2 cert = new X509Certificate2();
cert.Import( Encoding.ASCII.GetBytes(
@"-----BEGIN CERTIFICATE-----
MIICqDCCAhGgAwIBAgIJANDx5Es1s04zMA0GCSqGSIb3DQEBBQUAMG0xCzAJBgNV
BAYTAktSMQowCAYDVQQIDAEgMQowCAYDVQQHDAEgMQwwCgYDVQQKDANOSE4xEjAQ
BgNVBAsMCUNvbW11bml0eTESMBAGA1UEAwwJbmF2ZXIuY29tMRAwDgYJKoZIhvcN
AQkBFgEgMB4XDTEwMDYxNDA1MzAzNVoXDTExMDYxNDA1MzAzNVowbTELMAkGA1UE
BhMCS1IxCjAIBgNVBAgMASAxCjAIBgNVBAcMASAxDDAKBgNVBAoMA05ITjESMBAG
A1UECwwJQ29tbXVuaXR5MRIwEAYDVQQDDAluYXZlci5jb20xEDAOBgkqhkiG9w0B
CQEWASAwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANX++6LgORv6caQ8LCVh
RYTXi2Lko7zn4wPeqvdCqNZsxcry2mNHn/ic+0XbhNgor5L0l048f0iicW/Qu4vw
RvkZy2N8dNE3Tb5dbPLNo+S+cExv/DhbQVFKGiOOvr4vQ+2Lgw7If5g3sh6/S8Gu
ot47cOrUkiLKBKJt614bue9zAgMBAAGjUDBOMB0GA1UdDgQWBBSB1ReDAnl4lRyl
Rfpl0EZ13E5LzzAfBgNVHSMEGDAWgBSB1ReDAnl4lRylRfpl0EZ13E5LzzAMBgNV
HRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GBAEYdZfQjvk/wvlFP4l3mDqS4NMac
txx1lyYGa0gX4DGhb7aGwBb3qwCdSX7szuYNHHq5Clf9TGQMqc49RFC2TGNRrpSw
BZFRmyzhMsqx/dLcNIBLfz4B+SUw+yiwNKo3krYCJfqgNy0cW8sF121yWI3tPzqr
kD8kEbCa5GvxmsdT
-----END CERTIFICATE-----" ) );
DevDefined.OAuth.Framework.OAuthContext context = new DevDefined.OAuth.Framework.OAuthContext
{
//RawUri = request.Url,
RawUri = CleanUri( request.Url ),
Cookies = new NameValueCollection(),
Headers = request.Headers,
RequestMethod = request.HttpMethod,
FormEncodedParameters = new NameValueCollection(),
QueryParameters = new NameValueCollection( request.QueryString )
};
// do I need to set them manually?.. let's try..
context.ConsumerKey = request.QueryString[ "oauth_consumer_key" ];
context.Timestamp = request.QueryString[ "oauth_timestamp" ];
context.Nonce = request.QueryString[ "oauth_nonce" ];
context.Signature = request.QueryString[ "oauth_signature" ];
var signer = new DevDefined.OAuth.Framework.Signing.OAuthContextSigner();
DevDefined.OAuth.Framework.SigningContext signingContext = new DevDefined.OAuth.Framework.SigningContext();
// use context.ConsumerKey to fetch information required for signature validation for this consumer.
signingContext.Algorithm = cert.PublicKey.Key;
//signingContext.ConsumerSecret; // if there is a consumer secret
return ( signer.ValidateSignature( context, signingContext ) );
}
这是一个示例请求:(出于安全原因,我稍微更改了网址)
Url: http://www.maxmax.co.cc:7677/?oauth_signature=b58RqdQ0Atnrvvy6Qi81BRv1fhmXIQEl3hY++Wi7kiinSOhWq7mluhsapEi/GvCUG6RjrYVowwTcoi MqqmVCasFlON7zU7Yyi8nOUAoClpoft0BXbT5xyNUmLOxTk47tmjWVMLniaOieVUEQLwP2yycH6hiWkf2gFWGsu9LHZbE=&oauth_nonce=14611349243983507&oauth _version=1.0&oauth_body_hash=2jmj7l5rSw0yVb/vlWAYkK/YBwk=&oauth_signature_method=RSA-SHA1&oauth_consumer_key=naver.com&xoauth_sign ature_publickey=http://nsocialcontainer.com/server/naver_socialapp_public.cer&xoauth_public_key=http://nsocialcontainer.com/server /naver_socialapp_public.cer&opensocial_owner_id=1400000000010946792&opensocial_app_id=25992&oauth_timestamp=1296334075
Http方法:获取
用户代理:Jakarta Commons-HttpClient/3.1
请求参数
- oauth_signature:b58RqdQ0Atnrvvy6Qi81BRv1fhmXIQEl3hY++Wi7kiinSOhWq7mluhsapEi/GvCUG6RjrYVowwTcoiMqqmVCasFlON7zU7Yyi8nOUAoClpoft0BX bT5xyNUmLOxTk47tmjWVMLniaZhib66EQLwP2yLHc=
- oauth_nonce:14611349243983507 - oauth_version:1.0
- oauth_body_hash:2jmj7l5rSw0yVb/vlWAYkK/YBwk=
- oauth_signature_method:RSA-SHA1
- oauth_consumer_key:naver.com
- xoauth_signature_publickey:http://nsocialcontainer.com/server/naver_socialapp_public.cer
- xoauth_public_key:http://nsocialcontainer.com/server/naver_socialapp_public.cer
- opensocial_owner_id:1400000000010946792
- opensocial_app_id:25992
- oauth_timestamp:1296334075
标头
- X-Forwarded-For:61.75.38.242
- X-shindig-dos:on - Accept-Encoding:gzip, deflate
- 主办方:www.maxmax.co.cc:7677
- 身体: ””
我没有收到任何错误消息,也没有异常,但 isValidRequest2() 始终返回“false”,即使请求有效。
我一定是用错了。任何帮助将不胜感激!!如果您对这个问题有任何疑问,请提出。
谢谢!!!!