protected void Page_Load(object sender, EventArgs e) { //创建一个会话生成器,该生成器用来生成和认证服务器的会话 var sessionBuilder = new AlAuthenticationSessionBuilder(); //传递给认证服务器的协议参数,具体http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息 var setting = new AlRequestAuthSetting("http://weixinchat.ngrok.com/Response.aspx") { Key = "Key", Partner = "Partner" }; //因为该事例采用server-side方式来认证,所以直接引导用户到提供商认证服务器的页面处理器上去 var wxAuthenticationSession = sessionBuilder.Build(setting); if (wxAuthenticationSession != null) wxAuthenticationSession.HtmlRender.Render(Response); }
public static Stream GetHtmlText(AlRequestAuthSetting setting) { //需http://格式的完整路径,不允许加?id=123这类自定义参数 string returnUrl = setting.RedirectUri; var sParaTemp = new SortedDictionary<string, string> { {"partner", setting.Partner}, {"_input_charset",setting.InputCharset}, {"service", "alipay.auth.authorize"}, {"target_service", TargetService}, {"return_url", returnUrl} }; string sHtmlText = BuildRequest(setting,sParaTemp, "get", "确认"); return new MemoryStream(Encoding.Default.GetBytes (sHtmlText)); }
public static Stream GetHtmlText(AlRequestAuthSetting setting) { //需http://格式的完整路径,不允许加?id=123这类自定义参数 string returnUrl = setting.RedirectUri; var sParaTemp = new SortedDictionary <string, string> { { "partner", setting.Partner }, { "_input_charset", setting.InputCharset }, { "service", "alipay.auth.authorize" }, { "target_service", TargetService }, { "return_url", returnUrl } }; string sHtmlText = BuildRequest(setting, sParaTemp, "get", "确认"); return(new MemoryStream(Encoding.Default.GetBytes(sHtmlText))); }
/// <summary> /// 生成要请求给支付宝的参数数组 /// </summary> /// <param name="sParaTemp">请求前的参数数组</param> /// <returns>要请求的参数数组</returns> private static Dictionary <string, string> BuildRequestPara(AlRequestAuthSetting setting, SortedDictionary <string, string> sParaTemp) { //待签名请求参数数组 Dictionary <string, string> sPara = new Dictionary <string, string>(); //签名结果 string mysign = ""; //过滤签名参数数组 sPara = FilterPara(sParaTemp); //获得签名结果 mysign = BuildRequestMysign(setting, sPara); //签名结果与签名方式加入请求提交参数组中 sPara.Add("sign", mysign); sPara.Add("sign_type", setting.SignType); return(sPara); }
/// <summary> /// 生成要请求给支付宝的参数数组 /// </summary> /// <param name="sParaTemp">请求前的参数数组</param> /// <returns>要请求的参数数组</returns> private static Dictionary<string, string> BuildRequestPara(AlRequestAuthSetting setting,SortedDictionary<string, string> sParaTemp) { //待签名请求参数数组 Dictionary<string, string> sPara = new Dictionary<string, string>(); //签名结果 string mysign = ""; //过滤签名参数数组 sPara = FilterPara(sParaTemp); //获得签名结果 mysign = BuildRequestMysign(setting,sPara); //签名结果与签名方式加入请求提交参数组中 sPara.Add("sign", mysign); sPara.Add("sign_type", setting.SignType); return sPara; }
/// <summary> /// 生成请求时的签名 /// </summary> /// <param name="sPara">请求给支付宝的参数数组</param> /// <returns>签名结果</returns> private static string BuildRequestMysign(AlRequestAuthSetting setting, Dictionary <string, string> sPara) { //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 string prestr = CreateLinkString(sPara); //把最终的字符串签名,获得签名结果 string mysign = ""; switch (setting.SignType) { case "MD5": mysign = AlipayMD5.Sign(prestr, setting.Key, setting.InputCharset); break; default: mysign = ""; break; } return(mysign); }
/// <summary> /// 建立请求,以表单HTML形式构造(默认) /// </summary> /// <param name="setting"></param> /// <param name="sParaTemp">请求参数数组</param> /// <param name="strMethod">提交方式。两个值可选:post、get</param> /// <param name="strButtonValue">确认按钮显示文字</param> /// <returns>提交表单HTML文本</returns> public static string BuildRequest(AlRequestAuthSetting setting,SortedDictionary<string, string> sParaTemp, string strMethod, string strButtonValue) { //待请求参数数组 var dicPara = new Dictionary<string, string>(); dicPara = BuildRequestPara(setting,sParaTemp); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<form id='alipaysubmit' name='alipaysubmit' action='" + GatewayNew + "_input_charset=" + setting.InputCharset + "' method='" + strMethod.ToLower().Trim() + "'>"); foreach (KeyValuePair<string, string> temp in dicPara) { sbHtml.Append("<input type='hidden' name='" + temp.Key + "' value='" + temp.Value + "'/>"); } //submit按钮控件请不要含有name属性 sbHtml.Append("<input type='submit' value='" + strButtonValue + "' style='display:none;'></form>"); sbHtml.Append("<script>document.forms['alipaysubmit'].submit();</script>"); return sbHtml.ToString(); }
/// <summary> /// 建立请求,以表单HTML形式构造(默认) /// </summary> /// <param name="setting"></param> /// <param name="sParaTemp">请求参数数组</param> /// <param name="strMethod">提交方式。两个值可选:post、get</param> /// <param name="strButtonValue">确认按钮显示文字</param> /// <returns>提交表单HTML文本</returns> public static string BuildRequest(AlRequestAuthSetting setting, SortedDictionary <string, string> sParaTemp, string strMethod, string strButtonValue) { //待请求参数数组 var dicPara = new Dictionary <string, string>(); dicPara = BuildRequestPara(setting, sParaTemp); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<form id='alipaysubmit' name='alipaysubmit' action='" + GatewayNew + "_input_charset=" + setting.InputCharset + "' method='" + strMethod.ToLower().Trim() + "'>"); foreach (KeyValuePair <string, string> temp in dicPara) { sbHtml.Append("<input type='hidden' name='" + temp.Key + "' value='" + temp.Value + "'/>"); } //submit按钮控件请不要含有name属性 sbHtml.Append("<input type='submit' value='" + strButtonValue + "' style='display:none;'></form>"); sbHtml.Append("<script>document.forms['alipaysubmit'].submit();</script>"); return(sbHtml.ToString()); }
/// <summary> /// /// </summary> /// <param name="httpSupplier"></param> /// <param name="setting">渲染内容配置</param> /// <param name="render"></param> public AlAuthenticationSession(IHttpSupplier httpSupplier, AlRequestAuthSetting setting, IRenderable <AlRequestAuthSetting> render) : this(httpSupplier, render) { this.Render.Setting = setting; }
/// <summary> /// 生成请求时的签名 /// </summary> /// <param name="sPara">请求给支付宝的参数数组</param> /// <returns>签名结果</returns> private static string BuildRequestMysign(AlRequestAuthSetting setting,Dictionary<string, string> sPara) { //把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 string prestr = CreateLinkString(sPara); //把最终的字符串签名,获得签名结果 string mysign = ""; switch (setting.SignType) { case "MD5": mysign = AlipayMD5.Sign(prestr,setting.Key,setting.InputCharset); break; default: mysign = ""; break; } return mysign; }