实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Text; using Aop.Api.Util; using Aop.Api.Request; using Aop.Api.Response; using Jayrock.Json; namespace Aop.Api.Test { class PublicTest { [STAThread] //static void Main() //{ // // 公众号菜单查询 // //MenuGet(); // // 公众号通知消息签名验证 // //CheckSign(); // // 公众号验签&解密 // CheckSignAndDecrypt(); // // 公众号加密&加签 // EncryptAndSign(); //} private static void CheckSign() { IDictionary<string, string> paramsMap = new Dictionary<string, string>(); paramsMap.Add("appId", "2013092500031084"); string privateKeyPem = GetCurrentPath() "aop-sandbox-RSA-private-c#.pem"; string sign = AlipaySignature.RSASign(paramsMap, privateKeyPem, null); paramsMap.Add("sign", sign); string publicKey = GetCurrentPath() "public-key.pem"; bool checkSign = AlipaySignature.RSACheckV2(paramsMap, publicKey); System.Console.Write("---------公众号通知消息签名验证--------" "\n\r"); System.Console.Write("checkSign:" checkSign "\n\r"); } private static void MenuGet() { IAopClient client = GetAlipayClient(); //AlipayMobilePublicMenuGetRequest req = new AlipayMobilePublicMenuGetRequest(); //AlipayMobilePublicMenuGetResponse res = client.Execute(req); System.Console.Write("-------------公众号菜单查询-------------" "\n\r"); // System.Console.Write("Body:" res.Body "\n\r"); } private static IAopClient GetAlipayClient() { //支付宝网关地址 // -----沙箱地址----- string serverUrl = "http://openapi.alipaydev.com/gateway.do"; // -----线上地址----- // string serverUrl = "https://openapi.alipay.com/gateway.do"; //应用ID string appId = "2013092500031084"; //商户私钥 string privateKeyPem = GetCurrentPath() "aop-sandbox-RSA-private-c#.pem"; IAopClient client = new DefaultAopClient(serverUrl, appId, privateKeyPem); return client; } private static string GetCurrentPath() { string basePath = System.IO.Directory.GetParent(System.Environment.CurrentDirectory).Parent.FullName; return basePath "/Test/"; } public static void CheckSignAndDecrypt() { // 参数构建 string charset = "UTF-8"; string bizContent = "<XML><AppId><![CDATA[2013082200024893]]></AppId><FromUserId><![CDATA[2088102122485786]]></FromUserId><CreateTime>1377228401913</CreateTime><MsgType><![CDATA[click]]></MsgType><EventType><![CDATA[event]]></EventType><ActionParam><![CDATA[authentication]]></ActionParam><AgreementId><![CDATA[201308220000000994]]></AgreementId><AccountNo><![CDATA[null]]></AccountNo><UserInfo><![CDATA[{\"logon_id\":\"15858179811\",\"user_name\":\"许旦辉\"}]]></UserInfo></XML>"; string publicKeyPem = GetCurrentPath() "public-key.pem"; string privateKeyPem = GetCurrentPath() "aop-sandbox-RSA-private-c#.pem"; IDictionary<string, string> paramsMap = new Dictionary<string, string>(); paramsMap.Add("biz_content", AlipaySignature.RSAEncrypt(bizContent, publicKeyPem, charset)); paramsMap.Add("charset", charset); paramsMap.Add("service", "alipay.mobile.public.message.notify"); paramsMap.Add("sign_type", "RSA"); paramsMap.Add("sign", AlipaySignature.RSASign(paramsMap, privateKeyPem,null)); // 验签&解密 string resultContent = AlipaySignature.CheckSignAndDecrypt(paramsMap, publicKeyPem, privateKeyPem, true, true); System.Console.Write("resultContent=" resultContent "\n\r"); } public static void EncryptAndSign() { // 参数构建 string bizContent = "<XML><ToUserId><![CDATA[2088102122494786]]></ToUserId><AppId><![CDATA[2013111100036093]]></AppId><AgreementId><![CDATA[20131111000001895078]]></AgreementId>" "<CreateTime>12334349884</CreateTime>" "<MsgType><![CDATA[image-text]]></MsgType>" "<ArticleCount>1</ArticleCount>" "<Articles>" "<Item>" "<Title><![CDATA[[回复测试加密解密]]></Title>" "<Desc><![CDATA[测试加密解密]]></Desc>" "<Url><![CDATA[http://m.taobao.com]]></Url>" "<ActionName><![CDATA[立即前往]]></ActionName>" "</Item>" "</Articles>" "<Push><![CDATA[false]]></Push>" "</XML>"; string publicKeyPem = GetCurrentPath() "public-key.pem"; string privateKeyPem = GetCurrentPath() "aop-sandbox-RSA-private-c#.pem"; string responseContent = AlipaySignature.encryptAndSign(bizContent, publicKeyPem, privateKeyPem,"UTF-8",true,true); System.Console.Write("resultContent=" responseContent "\n\r"); } } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)