예제 #1
0
 /// <summary>
 /// 获得微信授权第一步获得code
 /// </summary>
 /// <param name="isbase">是否静默授权,</param>
 /// <param name="backurl">跳转地址</param>
 public static string GetAuthorizeCodeUrl(bool isbase, string backurl)
 {
     try
     {
         //第一步
         string redirect_uri = HttpUtility.UrlEncode(backurl);
         SortedDictionary <string, object> dic = new SortedDictionary <string, object>();
         dic.Add("appid", AppSettingHelper.AppId);
         dic.Add("redirect_uri", redirect_uri);
         dic.Add("response_type", "code");
         dic.Add("scope", isbase ? "snsapi_base" : "snsapi_userinfo");
         dic.Add("state", "STATE" + "#wechat_redirect");//任意值
         string tempurlstr = WXHelper.ToUrl(dic);
         string url        = AppSettingHelper.GetAuthorizeCodeFromUrl + tempurlstr;
         Container.LogService.Info("GetAuthorizeCodeUrl: " + url);
         return(url);
     }
     catch (Exception ex)
     {
         Container.LogService.Error("获取微信授权code错误。\n" + ex.ToString());
         return("");
     }
 }
예제 #2
0
        public static GetOpenIdResponse GetOpenidInfoFromCode(string code)
        {
            try
            {
                SortedDictionary <string, object> dic = new SortedDictionary <string, object>();
                dic["appid"]      = AppSettingHelper.AppId;
                dic["secret"]     = AppSettingHelper.SecretKey;
                dic["code"]       = code;
                dic["grant_type"] = "authorization_code";
                string tempurl = WXHelper.ToUrl(dic);
                string url     = AppSettingHelper.GetAccessTokeFromUrl + tempurl;
                WCFClient.LoggerService.Info("UrlInfo:" + url);
                string result = HttpService.Get(url);

                Container.LogService.Error("GetOpenidInfoFromCode: " + result);
                GetOpenIdResponse objResponse = PublicResource.JsonHelper.ConvertJsonStringToObject <GetOpenIdResponse>(result);
                return(objResponse);
            }
            catch (Exception ex)
            {
                Container.LogService.Error(ex.ToString());
                return(null);
            }
        }