private static string MakeSign(string appId, string secret, string nonceStr, string timeStamp) { string ticket = GetTicket(appId, secret); string sign = string.Empty; if (!General.IsNullable(ticket)) { WxParameters paras = new WxParameters(); paras.SetValue("noncestr", nonceStr); paras.SetValue("jsapi_ticket", ticket); paras.SetValue("timestamp", timeStamp); paras.SetValue("url", HttpContext.Current.Request.Url.AbsoluteUri); string make = paras.ToQueryString(); StringBuilder md = new StringBuilder(); byte[] buffer = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(make)); foreach (byte b in buffer) md.Append(b.ToString("x2")); sign = md.ToString(); } return sign; }
/// <summary> /// 使当前第三方站点跳转到微信授权URI地址,并获取Code /// </summary> /// <param name="appId">设置微信应用的唯一标识</param> /// <param name="redirectUri">设置授权后重定向的回调链接地址,如果不设置则默认使用当前第三方站点的当前URL</param> /// <param name="scope">设置应用授权作用域</param> /// <param name="state">设置自定义的状态参数,如果不设置,则默认情况下生成一个字符串</param> public static void RedirectConnectCode(string appId, string redirectUri, string scope, string state = null) { if (!General.IsNullable(appId)) { HttpResponse response = HttpContext.Current.Response; scope = General.ToNullable(scope, AuthorizationScope.USERINFO); string url = string.Format("https://open.weixin.qq.com/connect/{0}?", scope.Equals(AuthorizationScope.PCLOGIN) ? "qrconnect" : "oauth2/authorize"); if (General.IsNullable(redirectUri)) { Uri uri = HttpContext.Current.Request.Url; redirectUri = string.Format("{0}://{1}{2}", uri.Scheme, uri.Authority, uri.AbsolutePath); } WxParameters paras = new WxParameters(); paras.SetValue("appid", appId); paras.SetValue("redirect_uri", HttpUtility.UrlEncode(redirectUri, Encoding.UTF8)); paras.SetValue("response_type", "code"); paras.SetValue("scope", scope); paras.SetValue("state", General.ToNullable(state, General.UniqueString()) + "#wechat_redirect"); url += paras.ToQueryString(); response.Redirect(url); } }